Пример #1
0
  private ChannelFuture prepareServers() throws Exception {

    /* Accept's ... Robot acting as a server */
    for (ServerBootstrapResolver serverResolver : configuration.getServerResolvers()) {
      ServerBootstrap server = serverResolver.resolve();
      if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Binding to address " + server.getOption("localAddress"));
      }

      /* Keep track of the client channels */
      server.setParentHandler(
          new SimpleChannelHandler() {
            @Override
            public void childChannelOpen(ChannelHandlerContext ctx, ChildChannelStateEvent e)
                throws Exception {
              clientChannels.add(e.getChildChannel());
            }

            @Override
            public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
                throws Exception {
              Channel channel = ctx.getChannel();
              channel.close();
            }
          });

      // Bind Asynchronously
      ChannelFuture bindFuture = server.bindAsync();

      // Add to out serverChannel Group
      serverChannels.add(bindFuture.getChannel());

      // Add to our list of bindFutures so we can cancel them later on a possible abort
      bindFutures.add(bindFuture);

      // Listen for the bindFuture.
      RegionInfo regionInfo = (RegionInfo) server.getOption("regionInfo");
      bindFuture.addListener(
          createBindCompleteListener(regionInfo, serverResolver.getNotifyBarrier()));
    }

    return new CompositeChannelFuture<>(channel, bindFutures);
  }