Search in sources :

Example 1 with GridNioServerListenerAdapter

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);
}
Also used : GridBufferedParser(org.apache.ignite.internal.util.nio.GridBufferedParser) GridNioFilter(org.apache.ignite.internal.util.nio.GridNioFilter) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridNioParser(org.apache.ignite.internal.util.nio.GridNioParser) UnknownHostException(java.net.UnknownHostException) ByteOrder(java.nio.ByteOrder) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) UnknownHostException(java.net.UnknownHostException) GridDelimitedParser(org.apache.ignite.internal.util.nio.GridDelimitedParser) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridNioCodecFilter(org.apache.ignite.internal.util.nio.GridNioCodecFilter) GridNioServerListenerAdapter(org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with GridNioServerListenerAdapter

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();
}
Also used : GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) GridNioServerListenerAdapter(org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter) IgniteLogger(org.apache.ignite.IgniteLogger) ByteBuffer(java.nio.ByteBuffer) Nullable(org.jetbrains.annotations.Nullable) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) UnknownHostException(java.net.UnknownHostException) GridTestLog4jLogger(org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger)

Example 3 with GridNioServerListenerAdapter

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());
}
Also used : GridNioFilterAdapter(org.apache.ignite.internal.util.nio.GridNioFilterAdapter) GridNioSession(org.apache.ignite.internal.util.nio.GridNioSession) AtomicReference(java.util.concurrent.atomic.AtomicReference) ByteBuffer(java.nio.ByteBuffer) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteException(org.apache.ignite.IgniteException) GridNioFilterChain(org.apache.ignite.internal.util.nio.GridNioFilterChain) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) GridNioServerListenerAdapter(org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter) IgniteInClosure(org.apache.ignite.lang.IgniteInClosure) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

IgniteCheckedException (org.apache.ignite.IgniteCheckedException)3 GridNioServerListenerAdapter (org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter)3 GridNioSession (org.apache.ignite.internal.util.nio.GridNioSession)3 Nullable (org.jetbrains.annotations.Nullable)3 UnknownHostException (java.net.UnknownHostException)2 ByteBuffer (java.nio.ByteBuffer)2 IgniteException (org.apache.ignite.IgniteException)2 ByteOrder (java.nio.ByteOrder)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IgniteLogger (org.apache.ignite.IgniteLogger)1 GridBufferedParser (org.apache.ignite.internal.util.nio.GridBufferedParser)1 GridDelimitedParser (org.apache.ignite.internal.util.nio.GridDelimitedParser)1 GridNioCodecFilter (org.apache.ignite.internal.util.nio.GridNioCodecFilter)1 GridNioFilter (org.apache.ignite.internal.util.nio.GridNioFilter)1 GridNioFilterAdapter (org.apache.ignite.internal.util.nio.GridNioFilterAdapter)1 GridNioFilterChain (org.apache.ignite.internal.util.nio.GridNioFilterChain)1 GridNioParser (org.apache.ignite.internal.util.nio.GridNioParser)1 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)1 GridTestLog4jLogger (org.apache.ignite.testframework.junits.logger.GridTestLog4jLogger)1