org.gridgain.grid.util.ipc.GridIpcToNioAdapter Maven / Gradle / Ivy
/*
Copyright (C) GridGain Systems. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* _________ _____ __________________ _____
* __ ____/___________(_)______ /__ ____/______ ____(_)_______
* _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \
* / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / /
* \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/
*/
package org.gridgain.grid.util.ipc;
import org.gridgain.grid.*;
import org.gridgain.grid.logger.*;
import org.gridgain.grid.spi.communication.tcp.*;
import org.gridgain.grid.util.direct.*;
import org.gridgain.grid.util.nio.*;
import java.io.*;
import java.nio.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
/**
* Allows to re-use existing {@link GridNioFilter}s on IPC (specifically shared memory IPC)
* communications.
*
* Note that this class consumes an entire thread inside {@link #serve()} method
* in order to serve one {@link GridIpcEndpoint}.
*/
public class GridIpcToNioAdapter {
/** */
private final GridIpcEndpoint endp;
/** */
private final GridNioFilterChain chain;
/** */
private final GridNioSessionImpl ses;
/** */
private final AtomicReference latchRef = new AtomicReference<>();
/** */
private final ByteBuffer writeBuf;
/** */
private final GridNioMetricsListener metricsLsnr;
/** */
private final GridNioMessageWriter msgWriter;
/**
* @param metricsLsnr Metrics listener.
* @param log Log.
* @param endp Endpoint.
* @param msgWriter Message writer.
* @param lsnr Listener.
* @param filters Filters.
*/
public GridIpcToNioAdapter(GridNioMetricsListener metricsLsnr, GridLogger log, GridIpcEndpoint endp,
GridNioMessageWriter msgWriter, GridNioServerListener lsnr, GridNioFilter... filters) {
assert metricsLsnr != null;
assert msgWriter != null;
this.metricsLsnr = metricsLsnr;
this.endp = endp;
this.msgWriter = msgWriter;
chain = new GridNioFilterChain<>(log, lsnr, new HeadFilter(), filters);
ses = new GridNioSessionImpl(chain, null, null, true);
writeBuf = ByteBuffer.allocate(8 << 10);
writeBuf.order(ByteOrder.nativeOrder());
}
/**
* Serves given set of listeners repeatedly reading data from the endpoint.
*
* @throws InterruptedException If interrupted.
*/
public void serve() throws InterruptedException {
try {
chain.onSessionOpened(ses);
InputStream in = endp.inputStream();
ByteBuffer readBuf = ByteBuffer.allocate(8 << 10);
readBuf.order(ByteOrder.nativeOrder());
assert readBuf.hasArray();
while (!Thread.interrupted()) {
int pos = readBuf.position();
int read = in.read(readBuf.array(), pos, readBuf.remaining());
if (read > 0) {
metricsLsnr.onBytesReceived(read);
readBuf.position(0);
readBuf.limit(pos + read);
chain.onMessageReceived(ses, readBuf);
if (readBuf.hasRemaining())
readBuf.compact();
else
readBuf.clear();
CountDownLatch latch = latchRef.get();
if (latch != null)
latch.await();
}
else if (read < 0) {
endp.close();
break; // And close below.
}
}
// Assuming remote end closed connection - pushing event from head to tail.
chain.onSessionClosed(ses);
}
catch (Exception e) {
chain.onExceptionCaught(ses, new GridException("Failed to read from IPC endpoint.", e));
}
}
/**
* Handles write events on chain.
*
* @param msg Buffer to send.
* @return Send result.
*/
private GridNioFuture> send(GridTcpCommunicationMessageAdapter msg) {
assert writeBuf.hasArray();
try {
// This method is called only on handshake,
// so we don't need to provide node ID for
// rolling updates support.
int cnt = msgWriter.writeFully(null, msg, endp.outputStream(), writeBuf);
metricsLsnr.onBytesSent(cnt);
}
catch (IOException | GridException e) {
return new GridNioFinishedFuture
© 2015 - 2025 Weber Informatics LLC | Privacy Policy