Java Code Examples for org.apache.mina.common.IoSession#getAttribute()

The following examples show how to use org.apache.mina.common.IoSession#getAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MinaChannel.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
static MinaChannel getOrAddChannel(IoSession session, URL url, ChannelHandler handler) {
    if (session == null) {
        return null;
    }
    MinaChannel ret = (MinaChannel) session.getAttribute(CHANNEL_KEY);
    if (ret == null) {
        ret = new MinaChannel(session, url, handler);
        if (session.isConnected()) {
            MinaChannel old = (MinaChannel) session.setAttribute(CHANNEL_KEY, ret);
            if (old != null) {
                session.setAttribute(CHANNEL_KEY, old);
                ret = old;
            }
        }
    }
    return ret;
}
 
Example 2
Source File: MinaChannel.java    From dubbox with Apache License 2.0 6 votes vote down vote up
static MinaChannel getOrAddChannel(IoSession session, URL url, ChannelHandler handler) {
    if (session == null) {
        return null;
    }
    MinaChannel ret = (MinaChannel) session.getAttribute(CHANNEL_KEY);
    if (ret == null) {
        ret = new MinaChannel(session, url, handler);
        if (session.isConnected()) {
            MinaChannel old = (MinaChannel) session.setAttribute(CHANNEL_KEY, ret);
            if (old != null) {
                session.setAttribute(CHANNEL_KEY, old);
                ret = old;
            }
        }
    }
    return ret;
}
 
Example 3
Source File: MinaChannel.java    From dubbox-hystrix with Apache License 2.0 6 votes vote down vote up
static MinaChannel getOrAddChannel(IoSession session, URL url, ChannelHandler handler) {
    if (session == null) {
        return null;
    }
    MinaChannel ret = (MinaChannel) session.getAttribute(CHANNEL_KEY);
    if (ret == null) {
        ret = new MinaChannel(session, url, handler);
        if (session.isConnected()) {
            MinaChannel old = (MinaChannel) session.setAttribute(CHANNEL_KEY, ret);
            if (old != null) {
                session.setAttribute(CHANNEL_KEY, old);
                ret = old;
            }
        }
    }
    return ret;
}
 
Example 4
Source File: MinaChannel.java    From dubbox with Apache License 2.0 6 votes vote down vote up
static MinaChannel getOrAddChannel(IoSession session, URL url, ChannelHandler handler) {
    if (session == null) {
        return null;
    }
    MinaChannel ret = (MinaChannel) session.getAttribute(CHANNEL_KEY);
    if (ret == null) {
        ret = new MinaChannel(session, url, handler);
        if (session.isConnected()) {
            MinaChannel old = (MinaChannel) session.setAttribute(CHANNEL_KEY, ret);
            if (old != null) {
                session.setAttribute(CHANNEL_KEY, old);
                ret = old;
            }
        }
    }
    return ret;
}
 
Example 5
Source File: MinaChannel.java    From dubbox with Apache License 2.0 6 votes vote down vote up
static MinaChannel getOrAddChannel(IoSession session, URL url, ChannelHandler handler) {
    if (session == null) {
        return null;
    }
    MinaChannel ret = (MinaChannel) session.getAttribute(CHANNEL_KEY);
    if (ret == null) {
        ret = new MinaChannel(session, url, handler);
        if (session.isConnected()) {
            MinaChannel old = (MinaChannel) session.setAttribute(CHANNEL_KEY, ret);
            if (old != null) {
                session.setAttribute(CHANNEL_KEY, old);
                ret = old;
            }
        }
    }
    return ret;
}
 
Example 6
Source File: CougarProtocolTest.java    From cougar with Apache License 2.0 6 votes vote down vote up
@Test
public void testConnect() throws IOException {
    boolean success = false;
    ExecutionVenueNioServer server = createServer(defaultServerConfig);
    try {
        server.start();
        server.setHealthState(true);

        IoSession session = createClient(defaultClientConfig, new IoHandlerAdapter() {
        });
        ClientHandshake handshake = (ClientHandshake) session.getAttribute(ClientHandshake.HANDSHAKE);
        handshake.await(10000);

        success = handshake.successful();

        session.close();
    }
    finally {
        server.stop();
    }

    assertEquals("connection was not successful", true, success);

}
 
Example 7
Source File: CougarProtocolTest.java    From cougar with Apache License 2.0 6 votes vote down vote up
@Test
public void testRejectionServerUnhealthy() throws IOException {
    ExecutionVenueNioServer server = createServer(defaultServerConfig);
    server.start();
    server.setHealthState(false);

    IoSession session = createClient(defaultClientConfig, new IoHandlerAdapter());

    ClientHandshake handshake = (ClientHandshake) session.getAttribute(ClientHandshake.HANDSHAKE);
    handshake.await(10000);

    boolean success = handshake.successful();

    session.close();
    server.stop();

    assertEquals("connection shouln't have been successful", false, success);
}
 
Example 8
Source File: ImageServerIoHandler.java    From javastruct with GNU Lesser General Public License v3.0 5 votes vote down vote up
private String generateString(IoSession session, int length) {
    Integer index = (Integer) session.getAttribute(INDEX_KEY);
    StringBuffer buffer = new StringBuffer(length);
    while (buffer.length() < length) {
        buffer.append(characters.charAt(index));
        index++;
        if (index >= characters.length()) {
            index = 0;
        }
    }
    session.setAttribute(INDEX_KEY, index);
    return buffer.toString();
}
 
Example 9
Source File: CougarProtocolTest.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Test
public void testReject() throws IOException {
    // force version to an unsupported one (the next one)
    CougarProtocol.setMinClientProtocolVersion((byte) (CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MAX_SUPPORTED + 1));
    CougarProtocol.setMaxClientProtocolVersion((byte) (CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MAX_SUPPORTED + 1));
    try {
        TlsNioConfig nioConfig = new TlsNioConfig();
        nioConfig.setNioLogger(new NioLogger("ALL"));

        nioConfig.setListenAddress("127.0.0.1");
        nioConfig.setListenPort(2227);
        nioConfig.setReuseAddress(true);
        nioConfig.setTcpNoDelay(true);
        nioConfig.setKeepAliveInterval(Integer.MAX_VALUE);
        nioConfig.setKeepAliveTimeout(Integer.MAX_VALUE);
        ExecutionVenueNioServer server = createServer(nioConfig);
        server.start();
        server.setHealthState(true);

        IoSession ioSession = createClient(defaultClientConfig, new IoHandlerAdapter());

        ClientHandshake handshake = (ClientHandshake) ioSession.getAttribute(ClientHandshake.HANDSHAKE);
        handshake.await(10000);

        boolean success = handshake.successful();

        ioSession.close();
        server.stop();

        assertEquals("connection shouldn't have been successful", false, success);
    } finally {
        CougarProtocol.setMinClientProtocolVersion(CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MAX_SUPPORTED);
        CougarProtocol.setMaxClientProtocolVersion(CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MAX_SUPPORTED);
    }

}
 
Example 10
Source File: CougarProtocolTest.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Test
public void testDisconnect() throws IOException, InterruptedException {
    ExecutionVenueNioServer server = createServer(defaultServerConfig);
    boolean closed = false;
    try {
        server.start();
        server.setHealthState(true);

        final CountDownLatch cdl = new CountDownLatch(1);
        IoSession ioSession = createClient(defaultClientConfig, new IoHandlerAdapter() {
            @Override
            public void sessionClosed(IoSession session) throws Exception {
                cdl.countDown();
            }
        });

        ClientHandshake handshake = (ClientHandshake) ioSession.getAttribute(ClientHandshake.HANDSHAKE);
        handshake.await(1000);

        boolean success = handshake.successful();

        assertEquals("connection should have been successful", true, success);

        server.setHealthState(false);

        closed = cdl.await(50, TimeUnit.SECONDS);

        ioSession.close();
    }
    finally {
        server.stop();
    }


    assertEquals("expected session to close due to disconnection", true, closed);

}
 
Example 11
Source File: NioUtils.java    From cougar with Apache License 2.0 5 votes vote down vote up
public static String getSessionId(IoSession session) {
    String sessionId = (String) session.getAttribute("COUGAR_SESSION_ID");
    if (sessionId == null) {
        sessionId = String.format("%07d", sessionIdent.incrementAndGet());
        session.setAttribute("COUGAR_SESSION_ID", sessionId);
    }
    return sessionId;
}
 
Example 12
Source File: CougarProtocol3.java    From cougar with Apache License 2.0 5 votes vote down vote up
public static byte getProtocolVersion(IoSession session) {
    Byte b = (Byte) session.getAttribute(PROTOCOL_VERSION_ATTR_NAME);
    if (b == null) {
        throw new IllegalStateException("Protocol version requested for session before determined");
    }
    return b;
}
 
Example 13
Source File: CougarProtocolTest.java    From cougar with Apache License 2.0 4 votes vote down vote up
@Test
public void testGracefulDisconnect() throws IOException, InterruptedException {
    ExecutionVenueNioServer server = createServer(defaultServerConfig);
    server.start();
    server.setHealthState(true);

    final CountDownLatch cdl = new CountDownLatch(1);
    IoSession ioSession = createClient(defaultClientConfig, new IoHandlerAdapter() {
        @Override
        public void sessionClosed(IoSession session) throws Exception {
            cdl.countDown();
        }
    });

    ClientHandshake handshake = (ClientHandshake) ioSession.getAttribute(ClientHandshake.HANDSHAKE);
    handshake.await(1000);

    boolean success = handshake.successful();

    assertEquals("connection should have been successful", true, success);

    // write some dummy request
    ioSession.write(new RequestMessage(1, "request".getBytes()));

    server.setHealthState(false);

    boolean closed = cdl.await(50, TimeUnit.SECONDS);

    // Suspend message should have been recieved
    assertTrue(ioSession.containsAttribute(ProtocolMessage.ProtocolMessageType.SUSPEND.name()));
    // Disconnect message should have been recieved
    assertTrue(ioSession.containsAttribute(ProtocolMessage.ProtocolMessageType.DISCONNECT.name()));
    // Session should have been disconnected
    assertFalse(ioSession.isConnected());

    // teardown
    ioSession.close();
    server.stop();

    assertEquals("expected session to close due to disconnection", true, closed);

}
 
Example 14
Source File: CougarProtocolEncoder.java    From cougar with Apache License 2.0 4 votes vote down vote up
public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws Exception {
    final ByteBuffer buffer;
    if (message instanceof ProtocolMessage) {
        ProtocolMessage pm = (ProtocolMessage) message;
        nioLogger.log(PROTOCOL, session, "CougarProtocolEncoder: Writing protocol message %s", pm.getProtocolMessageType());

        Byte version = (Byte) session.getAttribute(CougarProtocol.PROTOCOL_VERSION_ATTR_NAME);
        // go for lowest likely common denominator, since this will likely only occur for RejectMessages
        if (version == null) {
            version = CougarProtocol.TRANSPORT_PROTOCOL_VERSION_MIN_SUPPORTED;
        }
        buffer = pm.getSerialisedForm(version);
        if (buffer == null) {
            badMessagesRequested.incrementAndGet();
            throw new IllegalArgumentException("Couldn't serialise ProtocolMessage [" + ((ProtocolMessage) message).getProtocolMessageType() + "]");
        }

        switch (pm.getProtocolMessageType()) {
            case ACCEPT:
                acceptsSent.incrementAndGet();
                break;

            case CONNECT:
                connectsSent.incrementAndGet();
                break;

            case REJECT:
                rejectsSent.incrementAndGet();
                break;

            case KEEP_ALIVE:
                keepAlivesSent.incrementAndGet();
                break;
            case DISCONNECT:
                disconnectsSent.incrementAndGet();
                break;

            case MESSAGE_REQUEST:
                messageRequestsSent.incrementAndGet();
                nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing message of length %s", (((RequestMessage) pm).getPayload().length + 8));
                break;
            case MESSAGE_RESPONSE:
                messageRequestsSent.incrementAndGet();
                nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing message of length %s", ((ResponseMessage) pm).getPayload().length);
                break;

            case EVENT:
                eventsSent.incrementAndGet();
                nioLogger.log(ALL, session, "CougarProtocolEncoder: Writing event of length %s", ((EventMessage) pm).getPayload().length);
                break;
            case SUSPEND:
                suspendsSent.incrementAndGet();
                break;

            case START_TLS_REQUEST:
                tlsRequestsSent.incrementAndGet();
                break;
            case START_TLS_RESPONSE:
                tlsResponsesSent.incrementAndGet();
                break;

            default:
                badMessagesRequested.incrementAndGet();
                throw new IllegalArgumentException("Unknown ProtocolMessage [" + ((ProtocolMessage) message).getProtocolMessageType() + "] received");

        }
    } else {
        throw new IllegalArgumentException("Unknown message type " + message);
    }
    buffer.flip();
    out.write(buffer);
    out.flush();
}
 
Example 15
Source File: NioUtils.java    From cougar with Apache License 2.0 4 votes vote down vote up
public static boolean isSecure(IoSession session) {
    TLSResult result = (TLSResult) session.getAttribute(CougarProtocol.NEGOTIATED_TLS_LEVEL_ATTR_NAME);
    return result != null && result == TLSResult.SSL;
}