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

The following examples show how to use org.apache.mina.common.IoSession#write() . 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: CougarProtocolTest.java    From cougar with Apache License 2.0 6 votes vote down vote up
public ExecutionVenueNioServer createServer(TlsNioConfig cfg) {
    ExecutionVenueNioServer server = new ExecutionVenueNioServer();
    server.setNioConfig(cfg);
    NioLogger sessionLogger = new NioLogger("ALL");
    TransportCommandProcessor<SocketTransportCommand> processor = new SocketTransportCommandProcessor();
    CougarObjectIOFactory objectIOFactory = new HessianObjectIOFactory(false);
    ExecutionVenueServerHandler serverHandler = new ExecutionVenueServerHandler(sessionLogger, processor, objectIOFactory) {
        @Override
        public void messageReceived(IoSession session, Object message) throws Exception {
            session.write(message);
        }
    };
    server.setServerHandler(serverHandler);
    server.setServerExecutor(Executors.newCachedThreadPool());
    server.setSocketAcceptorProcessors(1);
    server.setTransportRegistry(new TransportRegistryImpl());
    final IoSessionManager sessionManager = new IoSessionManager();
    server.setSessionManager(sessionManager);
    sessionManager.setMaxTimeToWaitForRequestCompletion(5000);
    sessionManager.setNioLogger(sessionLogger);
    return server;
}
 
Example 2
Source File: CougarProtocol3.java    From cougar with Apache License 2.0 6 votes vote down vote up
@Override
public void sessionIdle(NextFilter nextFilter, IoSession session, IdleStatus status) throws Exception {
    try {
        if (status == IdleStatus.WRITER_IDLE) {
            nioLogger.log(PROTOCOL, session, "CougarProtocolCodecFilter: sending KEEP_ALIVE");
            session.write(KEEP_ALIVE);
            heartbeatsSent.incrementAndGet();
        } else {
            nioLogger.log(PROTOCOL, session, "CougarProtocolCodecFilter: KEEP_ALIVE timeout closing session");
            session.close();
            heartbeatsMissed.incrementAndGet();
        }
    } finally {
        nextFilter.sessionIdle(session, status);
    }
}
 
Example 3
Source File: ImageServerIoHandler.java    From javastruct with GNU Lesser General Public License v3.0 5 votes vote down vote up
public void messageReceived(IoSession session, Object message) throws Exception {
    ImageRequest request = (ImageRequest) message;
    String text1 = generateString(session, request.getNumberOfCharacters());
    String text2 = generateString(session, request.getNumberOfCharacters());
    BufferedImage image1 = createImage(request, text1);
    BufferedImage image2 = createImage(request, text2);
    ImageResponse response = new ImageResponse(image1, image2);
    session.write(response);
}
 
Example 4
Source File: NioUtils.java    From cougar with Apache License 2.0 5 votes vote down vote up
public static void writeEventMessageToSession(IoSession session, Object obj, CougarObjectIOFactory objectIOFactory) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    CougarObjectOutput out = objectIOFactory.newCougarObjectOutput(baos, CougarProtocol.getProtocolVersion(session));
    out.writeObject(obj);
    out.flush();

    session.write(new EventMessage(baos.toByteArray()));
}
 
Example 5
Source File: CougarProtocol3.java    From cougar with Apache License 2.0 5 votes vote down vote up
@Override
public void sessionOpened(NextFilter nextFilter, IoSession session) throws Exception {
    if (!isServer) {
        ClientHandshake clientHandshake = new ClientHandshake();
        session.setAttribute(ClientHandshake.HANDSHAKE, clientHandshake);
        session.write(new ConnectMessage(getClientAcceptableVersions()));
    }
    super.sessionOpened(nextFilter, session);
}
 
Example 6
Source File: TestServerHandler.java    From cougar with Apache License 2.0 4 votes vote down vote up
private void write(IoSession session, int msg) throws IOException {
	final ByteArrayOutputStream baos = new ByteArrayOutputStream();
	new DataOutputStream(baos).writeInt(msg + 1);
	session.write(baos);
}
 
Example 7
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);

}