Package com.biasedbit.nettytutorials.handshake.server

Examples of com.biasedbit.nettytutorials.handshake.server.Server


        final Map<ServerHandler, AtomicInteger> lastMap =
                new ConcurrentHashMap<ServerHandler, AtomicInteger>();

        // Create a new server with id "server1" with a listener that ensures
        // that for each handler, perfect message order is guaranteed.
        final Server s = new Server("server1", new ServerListener() {

            @Override
            public void messageReceived(ServerHandler handler,
                                        String message) {
                AtomicInteger last = lastMap.get(handler);
                int num = Integer.parseInt(message.trim());
                if (num != (last.get() + 1)) {
                    System.err.println("+++ SERVER-LISTENER(" +
                                       handler.getRemoteId() + ") :: " +
                                       "OUT OF ORDER!!! expecting " +
                                       (last.get() + 1) + " and got " +
                                       message);
                } else {
                    last.set(num);
                }

                handler.sendMessage(message);
            }

            @Override
            public void connectionOpen(ServerHandler handler) {
                System.err.println("+++ SERVER-LISTENER(" +
                                   handler.getRemoteId() +
                                   ") :: Connection with " +
                                   handler.getRemoteId() +
                                   " opened & ready to send/receive data.");
                AtomicInteger counter = new AtomicInteger();
                lastMap.put(handler, counter);
            }
        });

        if (!s.start()) {
            return;
        }

        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                s.stop();
            }
        });
    }
View Full Code Here

TOP

Related Classes of com.biasedbit.nettytutorials.handshake.server.Server

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.