Package org.apache.mina.core.service

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


    }

    @Test
    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


    }

    @Override
    protected ConnectFuture connect(int port, IoHandler handler)
            throws Exception {
        IoConnector connector = new VmPipeConnector();
        connector.setHandler(handler);
        return connector.connect(new VmPipeAddress(port));
    }
View Full Code Here

*/
public class VmPipeSessionCrossCommunicationTest {
    @Test
    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

        assertNull(support.getManagedSessions().get(session.getId()));
    }

    @Test
    public void testConnectorActivation() throws Exception {
        IoConnector connector = EasyMock.createStrictMock(IoConnector.class);

        IoServiceListenerSupport support = new IoServiceListenerSupport(
                connector);

        final DummySession session = new DummySession();
View Full Code Here

    @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 doDispose() throws Exception {
                proc.doDispose();
            }

            @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");
            }
        });
        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

    }

    @Override
    protected ConnectFuture connect(int port, IoHandler handler)
            throws Exception {
        IoConnector connector = new NioSocketConnector();
        connector.setHandler(handler);
        return connector.connect(new InetSocketAddress("localhost", port));
    }
View Full Code Here

    }

    @Override
    protected ConnectFuture connect(int port, IoHandler handler)
            throws Exception {
        IoConnector connector = new NioDatagramConnector();
        connector.setHandler(handler);
        return connector.connect(new InetSocketAddress("localhost", port));
    }
View Full Code Here

public class DatagramPortUnreachableTest {

    Object mutex = new Object();
   
    private void runTest(boolean closeOnPortUnreachable) throws Exception {
        IoConnector connector = new NioDatagramConnector();
        connector.setHandler(new IoHandlerAdapter() {

            @Override
            public void exceptionCaught(IoSession session, Throwable cause)
                    throws Exception {
                if (cause instanceof PortUnreachableException) {
                    synchronized(mutex) {
                        mutex.notify();
                    }
                }
            }
           
        });
        ConnectFuture future = connector.connect(new InetSocketAddress("localhost",
                AvailablePortFinder.getNextAvailable(20000)));
        future.awaitUninterruptibly();
        IoSession session = future.getSession();

        DatagramSessionConfig cfg = ((DatagramSessionConfig) session
                .getConfig());
        cfg.setUseReadOperation(true);
        cfg.setCloseOnPortUnreachable(closeOnPortUnreachable);
       
        synchronized(mutex) {
            session.write(IoBuffer.allocate(1)).awaitUninterruptibly().isWritten();
            session.read();
            mutex.wait();
        }
       
        Thread.sleep(500);
       
        assertEquals(closeOnPortUnreachable, session.isClosing());
        connector.dispose();
    }
View Full Code Here

           
        });
        acceptor.bind(new InetSocketAddress(port));

        try {
            IoConnector connector = new NioSocketConnector();
            connector.setHandler(new IoHandlerAdapter());
            ConnectFuture connectFuture = connector.connect(new InetSocketAddress("localhost", port));
            connectFuture.awaitUninterruptibly();
            if (connectFuture.getException() != null) {
                throw connectFuture.getException();
            }
            connectFuture.getSession().getConfig().setUseReadOperation(true);
View Full Code Here

        assertNull(support.getManagedSessions().get(session.getId()));
    }

    @Test
    public void testConnectorActivation() throws Exception {
        IoConnector connector = EasyMock.createStrictMock(IoConnector.class);

        IoServiceListenerSupport support = new IoServiceListenerSupport(connector);

        final DummySession session = new DummySession();
        session.setService(connector);
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.