Search in sources :

Example 1 with GridNioFilterAdapter

use of org.apache.ignite.internal.util.nio.GridNioFilterAdapter 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

ByteBuffer (java.nio.ByteBuffer)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 GridNioFilterAdapter (org.apache.ignite.internal.util.nio.GridNioFilterAdapter)1 GridNioFilterChain (org.apache.ignite.internal.util.nio.GridNioFilterChain)1 GridNioServerListenerAdapter (org.apache.ignite.internal.util.nio.GridNioServerListenerAdapter)1 GridNioSession (org.apache.ignite.internal.util.nio.GridNioSession)1 IgniteInClosure (org.apache.ignite.lang.IgniteInClosure)1 Nullable (org.jetbrains.annotations.Nullable)1