Package org.apache.mina.transport.nio

Examples of org.apache.mina.transport.nio.NioTcpServer


    /**
     * Starts a Server with the SSL Filter and a simple text line
     * protocol codec filter
     */
    private static int startServer() throws Exception {
        NioTcpServer server = new NioTcpServer();

        server.setReuseAddress(true);
        server.getSessionConfig().setSslContext(createSSLContext());
        server.setIoHandler(new TestHandler());
        server.bind(new InetSocketAddress(0));
        return server.getServerSocketChannel().socket().getLocalPort();
    }
View Full Code Here


    }

    @Test
    public void testBigMessage() throws IOException, GeneralSecurityException, InterruptedException {
        final CountDownLatch counter = new CountDownLatch(1);
        NioTcpServer server = new NioTcpServer();
        final int messageSize = 1 * 1024 * 1024;

        /*
         * Server
         */
        server.setReuseAddress(true);
        server.getSessionConfig().setSslContext(createSSLContext());
        server.setIoHandler(new AbstractIoHandler() {
            private int receivedSize = 0;

            /**
             * {@inheritedDoc}
             */
            @Override
            public void messageReceived(IoSession session, Object message) {
                receivedSize += ((ByteBuffer) message).remaining();
                if (receivedSize == messageSize) {
                    counter.countDown();
                }
            }
        });
        server.bind(new InetSocketAddress(0));
        int port = server.getServerSocketChannel().socket().getLocalPort();

        /*
         * Client
         */
        Socket socket = server.getSessionConfig().getSslContext().getSocketFactory().createSocket("localhost", port);
        socket.getOutputStream().write(new byte[messageSize]);
        socket.getOutputStream().flush();
        socket.close();
        assertTrue(counter.await(10, TimeUnit.SECONDS));

View Full Code Here

    private final CountDownLatch idleLatch = new CountDownLatch(CLIENT_COUNT);

    @Test
    public void generate_all_kind_of_server_event() throws IOException, InterruptedException {
        final NioTcpServer server = new NioTcpServer();
        server.setFilters();
        server.getSessionConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 1000);
        server.setIoHandler(new Handler());
        server.bind(0);

        // warm up
        Thread.sleep(100);

        final int port = server.getServerSocketChannel().socket().getLocalPort();

        final Socket[] clients = new Socket[CLIENT_COUNT];

        // connect some clients
        for (int i = 0; i < CLIENT_COUNT; i++) {
            clients[i] = new Socket("127.0.0.1", port);
        }

        // does the session open message was fired ?
        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // write some messages
        for (int i = 0; i < CLIENT_COUNT; i++) {
            clients[i].getOutputStream().write(("test:" + i).getBytes());
            clients[i].getOutputStream().flush();
        }

        // test is message was received by the server
        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // does response was wrote and sent ?
        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // read the echos
        final byte[] buffer = new byte[1024];

        for (int i = 0; i < CLIENT_COUNT; i++) {
            final int bytes = clients[i].getInputStream().read(buffer);
            final String text = new String(buffer, 0, bytes);
            assertEquals("test:" + i, text);
        }

        // does the session idle event was fired ?
        assertTrue(idleLatch.await(5 * WAIT_TIME, TimeUnit.MILLISECONDS));

        // close the session
        assertEquals(CLIENT_COUNT, closedLatch.getCount());
        for (int i = 0; i < CLIENT_COUNT; i++) {
            clients[i].close();
        }

        // does the session close event was fired ?
        assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        server.unbind();
    }
View Full Code Here

    }

    @Test
    public void generateAllKindOfServerEventOneSelector() throws IOException, InterruptedException {
        SelectorLoopPool selectorLoopPool = new FixedSelectorLoopPool("Server", 1);
        final NioTcpServer server = new NioTcpServer(selectorLoopPool.getSelectorLoop(), selectorLoopPool, null);
        server.setFilters();
        server.setIoHandler(new Handler());
        server.bind(0);

        // warm up
        Thread.sleep(100);

        final int port = server.getServerSocketChannel().socket().getLocalPort();

        final Socket[] clients = new Socket[CLIENT_COUNT];

        // connect some clients
        for (int i = 0; i < CLIENT_COUNT; i++) {
            clients[i] = new Socket("127.0.0.1", port);
        }

        // does the session open message was fired ?
        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // write some messages
        for (int i = 0; i < CLIENT_COUNT; i++) {
            clients[i].getOutputStream().write(("test:" + i).getBytes());
            clients[i].getOutputStream().flush();
        }

        // test is message was received by the server
        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // does response was wrote and sent ?
        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // read the echos
        final byte[] buffer = new byte[1024];

        for (int i = 0; i < CLIENT_COUNT; i++) {
            final int bytes = clients[i].getInputStream().read(buffer);
            final String text = new String(buffer, 0, bytes);
            assertEquals("test:" + i, text);
        }

        // close the session
        assertEquals(CLIENT_COUNT, closedLatch.getCount());
        for (int i = 0; i < CLIENT_COUNT; i++) {
            clients[i].close();
        }

        // does the session close event was fired ?
        assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        server.unbind();
    }
View Full Code Here

    private final CountDownLatch closedLatch = new CountDownLatch(CLIENT_COUNT);

    @Test
    public void generate_all_kind_of_server_event() throws IOException, InterruptedException {
        final NioTcpServer server = new NioTcpServer();
        server.setFilters(new MyCodec(), new Handler());
        server.bind(0);
        // warm up
        Thread.sleep(100);

        long t0 = System.currentTimeMillis();
        final int port = server.getServerSocketChannel().socket().getLocalPort();

        final Socket[] clients = new Socket[CLIENT_COUNT];

        // connect some clients
        for (int i = 0; i < CLIENT_COUNT; i++) {
            // System.out.println("Creation client " + i);
            try {
                clients[i] = new Socket("127.0.0.1", port);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Creation client " + i + " failed");
            }
        }

        // does the session open message was fired ?
        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // write some messages
        for (int i = 0; i < CLIENT_COUNT; i++) {
            clients[i].getOutputStream().write(("test:" + i).getBytes());
            clients[i].getOutputStream().flush();
        }

        // test is message was received by the server
        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // does response was wrote and sent ?
        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // read the echos
        final byte[] buffer = new byte[1024];

        for (int i = 0; i < CLIENT_COUNT; i++) {
            final int bytes = clients[i].getInputStream().read(buffer);
            final String text = new String(buffer, 0, bytes);
            assertEquals("test:" + i, text);
        }

        // close the session
        assertEquals(CLIENT_COUNT, closedLatch.getCount());
        for (int i = 0; i < CLIENT_COUNT; i++) {
            clients[i].close();
        }

        // does the session close event was fired ?
        assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        long t1 = System.currentTimeMillis();

        System.out.println("Delta = " + (t1 - t0));

        server.unbind();
    }
View Full Code Here

     * selector to process all the OP events.
     */
    @Test
    public void generateAllKindOfServerEventOneSelector() throws IOException, InterruptedException {
        SelectorLoopPool selectorLoopPool = new FixedSelectorLoopPool("Server", 1);
        final NioTcpServer server = new NioTcpServer(selectorLoopPool.getSelectorLoop(), selectorLoopPool, null);
        server.setFilters(new MyCodec(), new Handler());
        server.bind(0);
        // warm up
        Thread.sleep(100);

        long t0 = System.currentTimeMillis();
        final int port = server.getServerSocketChannel().socket().getLocalPort();

        final Socket[] clients = new Socket[CLIENT_COUNT];

        // connect some clients
        for (int i = 0; i < CLIENT_COUNT; i++) {
            // System.out.println("Creation client 2 " + i);
            clients[i] = new Socket("127.0.0.1", port);
        }

        // does the session open message was fired ?
        assertTrue(openLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // write some messages
        for (int i = 0; i < CLIENT_COUNT; i++) {
            clients[i].getOutputStream().write(("test:" + i).getBytes());
            clients[i].getOutputStream().flush();
        }

        // test is message was received by the server
        assertTrue(msgReadLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // does response was wrote and sent ?
        assertTrue(msgSentLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        // read the echos
        final byte[] buffer = new byte[1024];

        for (int i = 0; i < CLIENT_COUNT; i++) {
            final int bytes = clients[i].getInputStream().read(buffer);
            final String text = new String(buffer, 0, bytes);
            assertEquals("test:" + i, text);
        }

        // close the session
        assertEquals(CLIENT_COUNT, closedLatch.getCount());
        for (int i = 0; i < CLIENT_COUNT; i++) {
            clients[i].close();
        }

        // does the session close event was fired ?
        assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));

        long t1 = System.currentTimeMillis();

        System.out.println("Delta = " + (t1 - t0));

        server.unbind();
    }
View Full Code Here

        // BasicConfigurator.configure();
    }

    @Test
    public void readIdleTest() throws IOException {
        final NioTcpServer server = new NioTcpServer();

        final CountDownLatch idleLatch = new CountDownLatch(CLIENT_COUNT);

        // 3 seconds idle time
        server.getSessionConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 2000);

        // start the server
        server.bind(new InetSocketAddress(0));

        final int boundPort = server.getServerSocketChannel().socket().getLocalPort();
        server.setFilters(new IdleHandler(idleLatch));

        // fire the clients and let them idle
        final Socket[] clients = new Socket[CLIENT_COUNT];

        for (int i = 0; i < CLIENT_COUNT; i++) {
View Full Code Here

     */
    @Test
    public void checkSessionsAreClosedWhenClientIsDisconnected() throws IOException, InterruptedException,
            ExecutionException {

        NioTcpServer server = new NioTcpServer();
        server.setIoHandler(new Handler());
        server.bind(0);

        NioTcpClient client = new NioTcpClient();
        client.setIoHandler(new AbstractIoHandler() {
        });
        for (int i = 0; i < CLIENT_COUNT; ++i) {
            client.connect(new InetSocketAddress(server.getServerSocketChannel().socket().getLocalPort())).get();
        }
        client.disconnect();
        assertTrue(closedLatch.await(WAIT_TIME, TimeUnit.MILLISECONDS));
    }
View Full Code Here

    static final private Logger LOG = LoggerFactory.getLogger(NioEchoServer.class);

    public static void main(final String[] args) {
        LOG.info("starting echo server");

        final NioTcpServer acceptor = new NioTcpServer();

        // create the filter chain for this service
        acceptor.setFilters(new LoggingFilter("LoggingFilter1"));

        acceptor.setIoHandler(new AbstractIoHandler() {
            @Override
            public void sessionOpened(final IoSession session) {
                LOG.info("session opened {}", session);

                final String welcomeStr = "welcome\n";
                final ByteBuffer bf = ByteBuffer.allocate(welcomeStr.length());
                bf.put(welcomeStr.getBytes());
                bf.flip();
                session.write(bf);
            }

            @Override
            public void messageReceived(IoSession session, Object message) {
                if (message instanceof ByteBuffer) {
                    LOG.info("echoing");
                    session.write(message);
                }
            }
        });
        try {
            final SocketAddress address = new InetSocketAddress(9999);
            acceptor.bind(address);
            LOG.debug("Running the server for 25 sec");
            Thread.sleep(25000);
            LOG.debug("Unbinding the TCP port");
            acceptor.unbind();
        } catch (final InterruptedException e) {
            LOG.error("Interrupted exception", e);
        }
    }
View Full Code Here

    /**
     * {@inheritDoc}
     */
    public void start(int port) throws IOException {
        tcpServer = new NioTcpServer(new FixedSelectorLoopPool("Server", 1), null);
        tcpServer.getSessionConfig().setReadBufferSize(128 * 1024);
        tcpServer.getSessionConfig().setTcpNoDelay(true);
        tcpServer.setIoHandler(new IoHandler() {
            public void sessionOpened(IoSession session) {
                session.setAttribute(STATE_ATTRIBUTE, State.WAIT_FOR_FIRST_BYTE_LENGTH);
View Full Code Here

TOP

Related Classes of org.apache.mina.transport.nio.NioTcpServer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.