use of org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter in project ignite by apache.
the class SocketStreamer method start.
/**
* Starts streamer.
*
* @throws IgniteException If failed.
*/
public void start() {
A.ensure(getSingleTupleExtractor() != null || getMultipleTupleExtractor() != null, "tupleExtractor (single or multiple)");
A.notNull(getStreamer(), "streamer");
A.notNull(getIgnite(), "ignite");
A.ensure(threads > 0, "threads > 0");
log = getIgnite().log();
GridNioServerListener<byte[]> lsnr = new GridNioServerListenerAdapter<byte[]>() {
@Override
public void onConnected(GridNioSession ses) {
assert ses.accepted();
if (log.isDebugEnabled())
log.debug("Accepted connection: " + ses.remoteAddress());
}
@Override
public void onDisconnected(GridNioSession ses, @Nullable Exception e) {
if (e != null)
log.error("Connection failed with exception", e);
}
@Override
public void onMessage(GridNioSession ses, byte[] msg) {
addMessage(converter.convert(msg));
}
};
ByteOrder byteOrder = ByteOrder.BIG_ENDIAN;
GridNioParser parser = F.isEmpty(delim) ? new GridBufferedParser(directMode, byteOrder) : new GridDelimitedParser(delim, directMode);
if (converter == null)
converter = new DefaultConverter<>(getIgnite().name());
GridNioFilter codec = new GridNioCodecFilter(parser, log, directMode);
GridNioFilter[] filters = new GridNioFilter[] { codec };
try {
srv = new GridNioServer.Builder<byte[]>().address(addr == null ? InetAddress.getLocalHost() : addr).serverName("sock-streamer").port(port).listener(lsnr).logger(log).selectorCount(threads).byteOrder(byteOrder).filters(filters).build();
} catch (IgniteCheckedException | UnknownHostException e) {
throw new IgniteException(e);
}
srv.start();
if (log.isDebugEnabled())
log.debug("Socket streaming server started on " + addr + ':' + port);
}
use of org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter in project ignite by apache.
the class GridNioBenchmarkTest method run.
/**
* Runs the benchmark.
*
* @throws UnknownHostException If can't connect to given hist,
* @throws IgniteCheckedException If NIO server initialisation failed.
*/
@SuppressWarnings("ConstantConditions")
public void run() throws UnknownHostException, IgniteCheckedException {
GridNioServerListener<ByteBuffer> lsnr = new GridNioServerListenerAdapter<ByteBuffer>() {
@Override
public void onConnected(GridNioSession ses) {
X.print("New connection accepted.");
}
@Override
public void onDisconnected(GridNioSession ses, @Nullable Exception e) {
// No-op.
}
@Override
public void onMessage(GridNioSession ses, ByteBuffer msg) {
ByteBuffer buf = ByteBuffer.allocate(msg.remaining()).put(msg);
buf.position(0);
ses.send(buf);
}
@Override
public void onSessionWriteTimeout(GridNioSession ses) {
X.error("Session write timeout. Closing.");
}
@Override
public void onSessionIdleTimeout(GridNioSession ses) {
X.error("Session idle timeout. Closing.");
}
};
IgniteLogger log = new GridTestLog4jLogger(U.resolveIgniteUrl("config/ignite-log4j.xml"));
GridNioServer.<ByteBuffer>builder().address(InetAddress.getByName("localhost")).port(port).listener(lsnr).logger(log).selectorCount(selectorCnt).igniteInstanceName("").tcpNoDelay(false).directBuffer(false).byteOrder(ByteOrder.nativeOrder()).socketSendBufferSize(0).socketReceiveBufferSize(0).sendQueueLimit(0).build().start();
}
use of org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter in project ignite by apache.
the class GridNioFilterChainSelfTest method testChainEvents.
/**
* @throws Exception If failed.
*/
public void testChainEvents() throws Exception {
final AtomicReference<String> connectedEvt = new AtomicReference<>();
final AtomicReference<String> disconnectedEvt = new AtomicReference<>();
final AtomicReference<String> msgEvt = new AtomicReference<>();
final AtomicReference<String> idleEvt = new AtomicReference<>();
final AtomicReference<String> writeTimeoutEvt = new AtomicReference<>();
final AtomicReference<String> sndEvt = new AtomicReference<>();
final AtomicReference<String> closeEvt = new AtomicReference<>();
final AtomicReference<ByteBuffer> rcvdMsgObj = new AtomicReference<>();
final AtomicReference<Object> sndMsgObj = new AtomicReference<>();
GridNioServerListener<Object> testLsnr = new GridNioServerListenerAdapter<Object>() {
@Override
public void onConnected(GridNioSession ses) {
connectedEvt.compareAndSet(null, ses.<String>meta(OPENED_META_NAME));
}
@Override
public void onDisconnected(GridNioSession ses, @Nullable Exception e) {
disconnectedEvt.compareAndSet(null, ses.<String>meta(CLOSED_META_NAME));
}
@Override
public void onMessage(GridNioSession ses, Object msg) {
msgEvt.compareAndSet(null, ses.<String>meta(MESSAGE_RECEIVED_META_NAME));
rcvdMsgObj.compareAndSet(null, (ByteBuffer) msg);
}
@Override
public void onSessionWriteTimeout(GridNioSession ses) {
writeTimeoutEvt.compareAndSet(null, ses.<String>meta(WRITE_TIMEOUT_META_NAME));
}
@Override
public void onSessionIdleTimeout(GridNioSession ses) {
idleEvt.compareAndSet(null, ses.<String>meta(IDLE_META_NAME));
}
};
GridNioFilterAdapter testHead = new GridNioFilterAdapter("TestHead") {
@Override
public void onSessionOpened(GridNioSession ses) throws IgniteCheckedException {
proceedSessionOpened(ses);
}
@Override
public void onSessionClosed(GridNioSession ses) throws IgniteCheckedException {
proceedSessionClosed(ses);
}
@Override
public void onExceptionCaught(GridNioSession ses, IgniteCheckedException ex) throws IgniteCheckedException {
proceedExceptionCaught(ses, ex);
}
@Override
public GridNioFuture<?> onSessionWrite(GridNioSession ses, Object msg, boolean fut, IgniteInClosure<IgniteException> ackC) {
sndEvt.compareAndSet(null, ses.<String>meta(MESSAGE_WRITE_META_NAME));
sndMsgObj.compareAndSet(null, msg);
return null;
}
@Override
public void onMessageReceived(GridNioSession ses, Object msg) throws IgniteCheckedException {
proceedMessageReceived(ses, msg);
}
@Override
public GridNioFuture<Boolean> onSessionClose(GridNioSession ses) {
closeEvt.compareAndSet(null, ses.<String>meta(CLOSE_META_NAME));
return null;
}
@Override
public void onSessionIdleTimeout(GridNioSession ses) throws IgniteCheckedException {
proceedSessionIdleTimeout(ses);
}
@Override
public void onSessionWriteTimeout(GridNioSession ses) throws IgniteCheckedException {
proceedSessionWriteTimeout(ses);
}
};
GridNioFilterChain<Object> chain = new GridNioFilterChain<>(log, testLsnr, testHead, new AppendingFilter("A"), new AppendingFilter("B"), new AppendingFilter("C"), new AppendingFilter("D"));
GridNioSession ses = new MockNioSession();
ByteBuffer snd = ByteBuffer.wrap(new byte[1]);
ByteBuffer rcvd = ByteBuffer.wrap(new byte[1]);
chain.onSessionOpened(ses);
chain.onSessionClosed(ses);
chain.onMessageReceived(ses, rcvd);
chain.onSessionIdleTimeout(ses);
chain.onSessionWriteTimeout(ses);
assertNull(chain.onSessionClose(ses));
assertNull(chain.onSessionWrite(ses, snd, true, null));
assertEquals("DCBA", connectedEvt.get());
assertEquals("DCBA", disconnectedEvt.get());
assertEquals("DCBA", msgEvt.get());
assertEquals("DCBA", idleEvt.get());
assertEquals("DCBA", writeTimeoutEvt.get());
assertEquals("ABCD", sndEvt.get());
assertEquals("ABCD", closeEvt.get());
assertTrue(snd == sndMsgObj.get());
assertTrue(rcvd == rcvdMsgObj.get());
}
Aggregations