Beispiel #1
0
    /**
     * 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);
    }
 /**
  * The code in this funciton sets up a ServerSocketChannel which accepts incoming TCP/IP
  * connections and acts as our ACCEPTOR.
  */
 private static void accept(int port) {
   ChannelFactory factory =
       new NioServerSocketChannelFactory(
           Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
   ServerBootstrap bootstrap = new ServerBootstrap(factory);
   bootstrap.setPipelineFactory(
       new ChannelPipelineFactory() {
         public ChannelPipeline getPipeline() {
           return Channels.pipeline(new EchoServerHandler());
         }
       });
   bootstrap.setOption("child.tcpNoDelay", true);
   bootstrap.setOption("child.keepAlive", true);
   bootstrap.bind(new InetSocketAddress(port));
 }