Package org.apache.mina.common

Examples of org.apache.mina.common.IoAcceptor


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

    public void testClientToServer() throws Exception {
        IoAcceptor acceptor = new VmPipeAcceptor();
        acceptor.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
        //acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );

        IoConnector connector = new VmPipeConnector();
        connector.getDefaultConfig().setThreadModel(ThreadModel.MANUAL);
        //connector.getFilterChain().addLast( "logger", new LoggingFilter() );

        final StringBuffer actual = new StringBuffer();

        acceptor.bind(new VmPipeAddress(1), new IoHandlerAdapter() {

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

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

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

        });

        ConnectFuture future = connector.connect(new VmPipeAddress(1),
                new IoHandlerAdapter() {
                    public void sessionOpened(IoSession session)
                            throws Exception {
                        session.write("B");
                    }

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

        future.join();
        future.getSession().getCloseFuture().join();
        acceptor.unbindAll();

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


        final VmPipeAddress address = new VmPipeAddress( 1 );
        final IoConnector connector = new VmPipeConnector();
        final AtomicReference c1 = new AtomicReference();
        final CountDownLatch latch = new CountDownLatch( 1 );
        final CountDownLatch messageCount = new CountDownLatch( 2 );
        IoAcceptor acceptor = new VmPipeAcceptor();

        acceptor.bind( address, new IoHandlerAdapter() {
            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 );
                }
            }
        } );

        connector.getDefaultConfig().setThreadModel( ThreadModel.MANUAL );

        ConnectFuture future = connector.connect( address, new IoHandlerAdapter() {
            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() );

                    ConnectFuture c2Future = connector.connect( address, new IoHandlerAdapter() {
                        public void sessionOpened( IoSession session ) throws Exception {
                            session.write( "re-use c1" );
                        }

                        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();
                                ((IoSession) c1.get()).write( "please don't deadlock via c1" );
                            } else {
                                fail( "unexpected message received " + message );
                            }
                        }
                    } );

                    c2Future.join();

                    latch.await();

                    c2Future.getSession().write( "please don't deadlock via c2" );
                } else {
                    fail( "unexpeced message received " + message );
                }
            }
        } );

        future.join();

        c1.set( future.getSession() );
        ((IoSession) c1.get()).write( "start" );

        ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

        while ( !messageCount.await( 100, TimeUnit.MILLISECONDS ) ) {
            long[] threads = threadMXBean.findMonitorDeadlockedThreads();

            if ( null != threads ) {
                StringBuffer sb = new StringBuffer( 256 );
                ThreadInfo[] infos = threadMXBean.getThreadInfo( threads, Integer.MAX_VALUE );

                for (int i = 0; i < infos.length; i ++) {
                    ThreadInfo info = infos[i];
                    sb.append( info.getThreadName() )
                        .append( " blocked on " )
                        .append( info.getLockName() )
                        .append( " owned by " )
                        .append( info.getLockOwnerName() )
                        .append( "\n" );
                }

                for (int i = 0; i < infos.length; i ++) {
                    ThreadInfo info = infos[i];
                    sb.append( "\nStack for " ).append( info.getThreadName() ).append( "\n" );
                    StackTraceElement[] stackTrace = info.getStackTrace();
                    for (int j = 0; j < stackTrace.length; j ++) {
                        sb.append( "\t" ).append( stackTrace[j] ).append( "\n" );
                    }
                }

                fail( "deadlocked! \n" + sb );
            }
        }

        ( (IoAcceptorConfig) acceptor.getDefaultConfig() ).setDisconnectOnUnbind( false );
        acceptor.unbindAll();
    }
View Full Code Here

    /** Set this to true if you want to make the server SSL */
    private static final boolean USE_SSL = false;

    public static void main(String[] args) throws Exception {
        IoAcceptor acceptor = new SocketAcceptor();
        IoAcceptorConfig config = new SocketAcceptorConfig();
        DefaultIoFilterChainBuilder chain = config.getFilterChain();

        // Add SSL filter if SSL is enabled.
        if (USE_SSL) {
            addSSLSupport(chain);
        }

        addLogger(chain);

        // Bind
        acceptor.bind(new InetSocketAddress(PORT), new EchoProtocolHandler(),
                config);

        System.out.println("Listening on port " + PORT);
    }
View Full Code Here

    public void testDisconnectOnUnbind() throws Exception {
        final IoServiceListenerSupport support = new IoServiceListenerSupport();

        MockControl acceptorControl = MockControl
                .createStrictControl(IoAcceptor.class);
        IoAcceptor acceptor = (IoAcceptor) acceptorControl.getMock();

        final TestSession session = new TestSession(acceptor, ADDRESS);

        MockControl configControl = MockControl
                .createStrictControl(IoAcceptorConfig.class);
View Full Code Here

*/
public class Main {
    private static final int PORT = 8080;

    public static void main(String[] args) throws Exception {
        IoAcceptor acceptor = new SocketAcceptor();

        // Prepare the configuration
        SocketAcceptorConfig cfg = new SocketAcceptorConfig();
        cfg.setReuseAddress(true);
        cfg.getFilterChain().addLast("logger", new LoggingFilter());
        cfg.getFilterChain().addLast(
                "codec",
                new ProtocolCodecFilter(new TextLineCodecFactory(Charset
                        .forName("UTF-8"))));

        // Bind
        acceptor.bind(new InetSocketAddress(PORT),
                new ReverseProtocolHandler(), cfg);

        System.out.println("Listening on port " + PORT);
    }
View Full Code Here

    // Set this to false to use object serialization instead of custom codec.
    private static final boolean USE_CUSTOM_CODEC = true;

    public static void main(String[] args) throws Throwable {
        IoAcceptor acceptor = new SocketAcceptor();

        // Prepare the service configuration.
        SocketAcceptorConfig cfg = new SocketAcceptorConfig();
        cfg.setReuseAddress(true);
        if (USE_CUSTOM_CODEC) {
            cfg.getFilterChain()
                    .addLast(
                            "codec",
                            new ProtocolCodecFilter(
                                    new SumUpProtocolCodecFactory(true)));
        } else {
            cfg.getFilterChain().addLast(
                    "codec",
                    new ProtocolCodecFilter(
                            new ObjectSerializationCodecFactory()));
        }
        cfg.getFilterChain().addLast("logger", new LoggingFilter());

        acceptor.bind(new InetSocketAddress(SERVER_PORT),
                new ServerSessionHandler(), cfg);

        System.out.println("Listening on port " + SERVER_PORT);
    }
View Full Code Here

        }

        try
        {
            // Create an acceptor
            IoAcceptor acceptor = new SocketAcceptor();
           
            // Create a service configuration
            SocketAcceptorConfig cfg = new SocketAcceptorConfig();
            cfg.setReuseAddress( true );
            cfg.getFilterChain().addLast(
                    "protocolFilter",
                    new ProtocolCodecFilter( new HttpServerProtocolCodecFactory() ) );
            cfg.getFilterChain().addLast( "logger", new LoggingFilter() );

            acceptor.bind( new InetSocketAddress( port ), new ServerHandler(), cfg );

            System.out.println( "Server now listening on port " + port );
        }
        catch( Exception ex )
        {
View Full Code Here

    /** Set this to true if you want to make the server SSL */
    private static final boolean USE_SSL = false;

    public static void main( String[] args ) throws Exception
    {
        IoAcceptor acceptor = new SocketAcceptor();
        IoAcceptorConfig config = new SocketAcceptorConfig();
        DefaultIoFilterChainBuilder chain = config.getFilterChain();
       
        // Add SSL filter if SSL is enabled.
        if( USE_SSL )
        {
            addSSLSupport( chain  );
        }
       
        addLogger( chain );
       
        // Bind
        acceptor.bind(
                new InetSocketAddress( PORT ),
                new EchoProtocolHandler(),
                config );

        System.out.println( "Listening on port " + PORT );
View Full Code Here

    public void testDisconnectOnUnbind() throws Exception
    {
        final IoServiceListenerSupport support = new IoServiceListenerSupport();
   
        MockControl acceptorControl = MockControl.createStrictControl( IoAcceptor.class );
        IoAcceptor acceptor = ( IoAcceptor ) acceptorControl.getMock();

        final TestSession session = new TestSession( acceptor, ADDRESS );

        MockControl configControl = MockControl.createStrictControl( IoAcceptorConfig.class );
        IoAcceptorConfig config = ( IoAcceptorConfig ) configControl.getMock();
View Full Code Here

public class Main
{

    public static void main( String[] args ) throws Exception
    {
        IoAcceptor acceptor = new VmPipeAcceptor();
        VmPipeAddress address = new VmPipeAddress( 8080 );

        // Set up server
        acceptor.bind( address, new TennisPlayer() );

        // Connect to the server.
        VmPipeConnector connector = new VmPipeConnector();
        ConnectFuture future = connector.connect( address,
                                                  new TennisPlayer() );
        future.join();
        IoSession session = future.getSession();

        // Send the first ping message
        session.write( new TennisBall( 10 ) );

        // Wait until the match ends.
        session.getCloseFuture().join();
       
        acceptor.unbind( address );
    }
View Full Code Here

TOP

Related Classes of org.apache.mina.common.IoAcceptor

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.