/** Start a server that handles the supplied protocol. */ public boolean startServer(final int port) { if (server == null) { return false; // Did not initialize yet. } // Create a new connection & state manager. final CommManager manager = new CommManager(); // Create a new pipeline. This time use an anonymous class. server.setPipelineFactory( new ChannelPipelineFactory() { public ChannelPipeline getPipeline() { return Channels.pipeline( new PaxosMessageDecoder(), new PaxosMessageEncoder(), manager); } }); try { // Try to bind to the given port. serverChannel = server.bind(new InetSocketAddress(port)); return true; } catch (Exception e) { e.printStackTrace(); return false; } }
/** * The main listening method. Must be called for the node server to actually start listening for * traffic. */ public void initServer() { // Create a new channel factory using cached thread pools for the master & workers. serverFactory = new NioServerSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); // Bootstrap a new server. server = new ServerBootstrap(serverFactory); // Set some TCP options. server.setOption("child.tcpNoDelay", true); server.setOption("child.keepAlive", true); }