Package org.apache.mina.core.service

Examples of org.apache.mina.core.service.IoConnector


    @Ignore
    @Test
    public void testExceptionOnWrite() throws Exception {
        final Executor ex = Executors.newFixedThreadPool(1);

        IoConnector connector = new NioSocketConnector(
                new AbstractPollingIoProcessor<NioSession>(ex) {

                    private NioProcessor proc = new NioProcessor(ex);

                    @Override
                    protected Iterator<NioSession> allSessions() {
                        return proc.allSessions();
                    }

                    @Override
                    protected void destroy(NioSession session) throws Exception {
                        proc.destroy(session);
                    }

                    @Override
                    protected void dispose0() throws Exception {
                        proc.dispose0();
                    }

                    @Override
                    protected void init(NioSession session) throws Exception {
                        proc.init(session);
                    }

                    @Override
                    protected boolean isInterestedInRead(NioSession session) {
                        return proc.isInterestedInRead(session);
                    }

                    @Override
                    protected boolean isInterestedInWrite(NioSession session) {
                        return proc.isInterestedInWrite(session);
                    }

                    @Override
                    protected boolean isReadable(NioSession session) {
                        return proc.isReadable(session);
                    }

                    @Override
                    protected boolean isSelectorEmpty() {
                        return proc.isSelectorEmpty();
                    }

                    @Override
                    protected boolean isWritable(NioSession session) {
                        return proc.isWritable(session);
                    }

                    @Override
                    protected int read(NioSession session, IoBuffer buf)
                            throws Exception {
                        return proc.read(session, buf);
                    }

                    @Override
                    protected int select(long timeout) throws Exception {
                        return proc.select(timeout);
                    }

                    @Override
                    protected int select() throws Exception {
                        return proc.select();
                    }

                    @Override
                    protected Iterator<NioSession> selectedSessions() {
                        return proc.selectedSessions();
                    }

                    @Override
                    protected void setInterestedInRead(NioSession session,
                            boolean interested) throws Exception {
                        proc.setInterestedInRead(session, interested);
                    }

                    @Override
                    protected void setInterestedInWrite(NioSession session,
                            boolean interested) throws Exception {
                        proc.setInterestedInWrite(session, interested);
                    }

                    @Override
                    protected SessionState getState(NioSession session) {
                        return proc.getState(session);
                    }

                    @Override
                    protected int transferFile(NioSession session,
                            FileRegion region, int length) throws Exception {
                        return proc.transferFile(session, region, length);
                    }

                    @Override
                    protected void wakeup() {
                        proc.wakeup();
                    }

                    @Override
                    protected int write(NioSession session, IoBuffer buf,
                            int length) throws Exception {
                        throw new NoRouteToHostException(
                                "No Route To Host Test");
                    }

                    @Override
                    protected boolean isBrokenConnection() throws IOException {
                        return proc.isBrokenConnection();
                    }

                    @Override
                    protected void registerNewSelector() throws IOException {
                        proc.registerNewSelector();
                    }

                });
        connector.setHandler(new IoHandlerAdapter());

        IoAcceptor acceptor = new NioSocketAcceptor();
        acceptor.setHandler(new IoHandlerAdapter());

        InetSocketAddress addr = new InetSocketAddress("localhost",
                AvailablePortFinder.getNextAvailable(20000));

        acceptor.bind(addr);
        ConnectFuture future = connector.connect(addr);
        future.awaitUninterruptibly();
        IoSession session = future.getSession();
        WriteFuture wf = session.write(IoBuffer.allocate(1))
                .awaitUninterruptibly();
        assertNotNull(wf.getException());

        connector.dispose();
        acceptor.dispose();
    }
View Full Code Here


                .getInstance(false));
        connectorSSLFilter.setUseClientMode(true); // set client mode
    }

    public void testTCP() throws Exception {
        IoConnector connector = new NioSocketConnector();
        testConnector(connector);
    }
View Full Code Here

    }

    public void testTCPWithSSL() throws Exception {
        useSSL = true;
        // Create a connector
        IoConnector connector = new NioSocketConnector();

        // Add an SSL filter to connector
        connector.getFilterChain().addLast("SSL", connectorSSLFilter);
        testConnector(connector);
    }
View Full Code Here

        connector.getFilterChain().addLast("SSL", connectorSSLFilter);
        testConnector(connector);
    }

    public void testUDP() throws Exception {
        IoConnector connector = new NioDatagramConnector();
        testConnector(connector);
    }
View Full Code Here

        acceptor.setHandler(new IoHandlerAdapter());
        acceptor.bind(new InetSocketAddress(port));

        try {
            final StringBuffer buf = new StringBuffer();
            IoConnector connector = createConnector();
            connector.setHandler(new IoHandlerAdapter() {
                @Override
                public void sessionCreated(IoSession session) {
                    buf.append("1");
                }

                @Override
                public void sessionOpened(IoSession session) {
                    buf.append("2");
                }

                @Override
                public void exceptionCaught(IoSession session, Throwable cause) {
                    buf.append("X");
                }
            });
            ConnectFuture future = connector.connect(new InetSocketAddress(
                    "localhost", port));
            future.awaitUninterruptibly();
            buf.append("3");
            future.getSession().close(true);
            // sessionCreated() will fire before the connect future completes
View Full Code Here

    public void testConnectFutureFailureTiming() throws Exception {
        int port = AvailablePortFinder.getNextAvailable(3025);
        final StringBuffer buf = new StringBuffer();

        IoConnector connector = createConnector();
        connector.setHandler(new IoHandlerAdapter() {
            @Override
            public void sessionCreated(IoSession session) {
                buf.append("X");
            }

            @Override
            public void sessionOpened(IoSession session) {
                buf.append("Y");
            }

            @Override
            public void exceptionCaught(IoSession session, Throwable cause) {
                buf.append("Z");
            }
        });
       
        try {
            ConnectFuture future = connector.connect(new InetSocketAddress(
                    "localhost", port));
            future.awaitUninterruptibly();
            buf.append("1");
            try {
                future.getSession().close(true);
                fail();
            } catch (RuntimeIoException e) {
                // OK.
            }
            Assert.assertEquals("1", buf.toString());
        } finally {
            connector.dispose();
        }
    }
View Full Code Here

        IoAcceptor acceptor = createAcceptor();
        acceptor.setHandler(new IoHandlerAdapter());
        InetSocketAddress address = new InetSocketAddress(port);
        acceptor.bind(address);

        IoConnector connector = createConnector();
        connector.setHandler(new IoHandlerAdapter() {
           @Override
            public void sessionCreated(IoSession session) throws Exception {
                   assertions[sessionCreatedInvoked] = true;
                   assertions[sessionCreatedInvokedBeforeCallback] = !assertions[callbackInvoked];
                   latch.countDown();
            }
        });
       
        ConnectFuture future = connector.connect(new InetSocketAddress("127.0.0.1", port), new IoSessionInitializer<ConnectFuture>() {
            public void initializeSession(IoSession session, ConnectFuture future) {
                assertions[callbackInvoked] = true;
                callbackFuture[0] = future;
                latch.countDown();
            }
View Full Code Here

* @version $Rev: 713957 $, $Date: 2008-11-14 10:27:16 +0100 (Fri, 14 Nov 2008) $
*/
public class VmPipeEventOrderTest extends TestCase {
    public void testServerToClient() throws Exception {
        IoAcceptor acceptor = new VmPipeAcceptor();
        IoConnector connector = new VmPipeConnector();

        acceptor.setHandler(new IoHandlerAdapter() {
            @Override
            public void sessionOpened(IoSession session) throws Exception {
                session.write("B");
            }

            @Override
            public void messageSent(IoSession session, Object message)
                    throws Exception {
                session.close(true);
            }
        });

        acceptor.bind(new VmPipeAddress(1));

        final StringBuffer actual = new StringBuffer();

        connector.setHandler(new IoHandlerAdapter() {

            @Override
            public void messageReceived(IoSession session, Object message)
                    throws Exception {
                actual.append(message);
            }

            @Override
            public void sessionClosed(IoSession session) throws Exception {
                actual.append("C");
            }

            @Override
            public void sessionOpened(IoSession session) throws Exception {
                actual.append("A");
            }

        });

        ConnectFuture future = connector.connect(new VmPipeAddress(1));

        future.awaitUninterruptibly();
        future.getSession().getCloseFuture().awaitUninterruptibly();
        acceptor.dispose();

View Full Code Here

        Assert.assertEquals("ABC", actual.toString());
    }

    public void testClientToServer() throws Exception {
        IoAcceptor acceptor = new VmPipeAcceptor();
        IoConnector connector = new VmPipeConnector();

        final StringBuffer actual = new StringBuffer();

        acceptor.setHandler(new IoHandlerAdapter() {

            @Override
            public void messageReceived(IoSession session, Object message)
                    throws Exception {
                actual.append(message);
            }

            @Override
            public void sessionClosed(IoSession session) throws Exception {
                actual.append("C");
            }

            @Override
            public void sessionOpened(IoSession session) throws Exception {
                actual.append("A");
            }

        });

        acceptor.bind(new VmPipeAddress(1));

        connector.setHandler(new IoHandlerAdapter() {
            @Override
            public void sessionOpened(IoSession session) throws Exception {
                session.write("B");
            }

            @Override
            public void messageSent(IoSession session, Object message)
                    throws Exception {
                session.close(true);
            }
        });

        ConnectFuture future = connector.connect(new VmPipeAddress(1));

        future.awaitUninterruptibly();
        future.getSession().getCloseFuture().awaitUninterruptibly();
        acceptor.dispose();
        connector.dispose();

        // sessionClosed() might not be invoked yet
        // even if the connection is closed.
        while (actual.indexOf("C") < 0) {
            Thread.yield();
View Full Code Here

* @version $Rev: $, $Date:  $
*/
public class VmPipeSessionCrossCommunicationTest extends TestCase {
    public void testOneSessionTalkingBackAndForthDoesNotDeadlock() throws Exception {
        final VmPipeAddress address = new VmPipeAddress(1);
        final IoConnector connector = new VmPipeConnector();
        final AtomicReference<IoSession> c1 = new AtomicReference<IoSession>();
        final CountDownLatch latch = new CountDownLatch(1);
        final CountDownLatch messageCount = new CountDownLatch(2);
        IoAcceptor acceptor = new VmPipeAcceptor();

        acceptor.setHandler(new IoHandlerAdapter() {
            @Override
            public void messageReceived(IoSession session, Object message) throws Exception {
                //System.out.println(Thread.currentThread().getName() + ": " + message);

                if ("start".equals(message)) {
                    session.write("open new");
                } else if ("re-use c1".equals(message)) {
                    session.write("tell me something on c1 now");
                } else if (((String) message).startsWith("please don't deadlock")) {
                    messageCount.countDown();
                } else {
                    fail("unexpected message received " + message);
                }
            }
        });
        acceptor.bind(address);

        connector.setHandler(new IoHandlerAdapter() {
            @Override
            public void messageReceived(IoSession session, Object message) throws Exception {
                //System.out.println(Thread.currentThread().getName() + ": " + message);

                if ("open new".equals(message)) {
                    //System.out.println("opening c2 from " + Thread.currentThread().getName());

                    IoConnector c2 = new VmPipeConnector();
                    c2.setHandler(new IoHandlerAdapter() {
                        @Override
                        public void sessionOpened(IoSession session) throws Exception {
                            session.write("re-use c1");
                        }

                        @Override
                        public void messageReceived(IoSession session, Object message) throws Exception {
                            //System.out.println(Thread.currentThread().getName() + ": " + message);

                            if ("tell me something on c1 now".equals(message)) {
                                latch.countDown();
                                c1.get().write("please don't deadlock via c1");
                            } else {
                                fail("unexpected message received " + message);
                            }
                        }
                    });

                    ConnectFuture c2Future = c2.connect(address);

                    c2Future.await();

                    latch.await();

View Full Code Here

TOP

Related Classes of org.apache.mina.core.service.IoConnector

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.