lundi 30 octobre 2017

Asynchronous update of promise in Netty Nio

Having a server and client architecture which sends information. I wanted to return from the server the number of connected channels to the clients. Am returning the message of the server to the clients using promise. My code is:

public static void callBack () throws Exception{

   String host = "localhost";
   int port = 8080;

   try {
       Bootstrap b = new Bootstrap();
       b.group(workerGroup);
       b.channel(NioSocketChannel.class);
       b.option(ChannelOption.SO_KEEPALIVE, true);
       b.handler(new ChannelInitializer<SocketChannel>() {
        @Override
           public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(new RequestDataEncoder(), new ResponseDataDecoder(), new ClientHandler(promise));
           }
       });
       ChannelFuture f = b.connect(host, port).sync();
       //f.channel().closeFuture().sync();
   }
   finally {
    //workerGroup.shutdownGracefully();
   }
}

public static void main(String[] args) throws Exception {

  callBack();
  while (true) {

    Object msg = promise.get();
    System.out.println("The number if the connected clients is not two");
    int ret = Integer.parseInt(msg.toString());
    if (ret == 2){
        break;
    }
  }
  System.out.println("The number if the connected clients is two");
}

When I run one client it is always receiving the message The number if the connected clients is not two and the returned number is always one. When I run a second client it is receiving always as a ret value two, however, the first client still is receiving one. I cannot find which is the correct way to update the promise for the case of the first client.




Aucun commentaire:

Enregistrer un commentaire