Re: Using ChannelDuplexHandler to connect to another server

2016-09-06 Thread L_DisplayName
Thank you Norman, 
  That was the first thing I did, however HexDumpProxy doesn't use 
ChannelDuplexHandler , HexDumpProxyFrontendHandler extends 
ChannelInboundHandlerAdapter 
and so does the HexDumpProxyBackendHandler. In addition, the socksProxy 
classes doesn't utilize channelDuplexHandler. I do see the functionality I 
was aiming for, however I could not find any examples of how to utilize the 
ChannelDuplexHandler.

But, Again, I appreciate your response

On Tuesday, September 6, 2016 at 12:59:22 PM UTC-4, L_DisplayName wrote:
>
> Greetings All,
>   I wanted to know how I can use ChannelDuplexHandler from a server to 
> connect to another server? Also I would like to know if my understanding of 
> channelDuplex handler is correct and what's the difference between 
> bootstrapping a client and having it connect to a server verses having the 
> ChannelDuplexHandler connect to the server?
>
>   For example: If I have a *client A*, a *Server B *and a *Server C*. 
>  and I want *client A* to connect to *Server B* and *Server B *connect to 
> *Server 
> C.  *where pictorially*  I have:*
>  * A-->B-->C *
>
> *Below I show Server's B Handler code, as soon as client A connects to 
> Server B (The code for this is below B's Server Handler code), I want 
> Server B to connect to Server C, can you tell me if the code below is 
> correct or if any thing needs  to be changed and if so what and how? Thank 
> you!*
>
> *Attempting to connect Server B to Server C using ChannelDuplexHandler 
> (Note I am showing Server B's handler class)*
> public class Server_B_Handler extends ChannelDuplexHandler {
>
>  private String ipAddressToConnectTo, localIpAddress;
>  private int portToConnectTo, localPort;
>  private Logger logger;
>  private ChannelHandlerContext ctx;
>  private Channel channelFrom_A_to_B;
>  private Channel channelFrom_B_to_C;
>
>  public Server_B_Handler(String ipAddress, int port, localIpAddress, 
> localPort) {
>   ipAddressToConnectTo = ipAddress;
>  portToConnectTo = port
>  this.localIpAddress = localIpAddress;
>  this.localPort = localPort; 
>  logger = Logger.getLogger(this.getClass().getName());
>  this.ctx = null;
>  channelFrom_A_to_B = null; //Inbound channel
>  channelFrom_B_to_C = null; //Outbound Channel
>  
>  }
>
>  @Override
>  public void channelActive(ChannelHandlerContext ctx) throws Exception{
>  //THIS CTX (Channel context) is for the channel connecting Client A to 
> this Server (Server B)
>  //Please see below for code connecting client A to Server B
>  this.ctx = ctx;
>  channelFrom_A_to_B = this.ctx().channel();
>  
>  //QUESTION HOW CAN I CONNECT TO SERVER C using this server's CONNECT 
> METHOD?
>  
>  final Promise myPromise = this.ctx.executor().newPromise();
>  ctx.connect(new InetSocketAddress(InetAddress.getByName(
> ipAddressToConnectTo), portToConnectTo),new InetSocketAddress(this.
> localIpAddress,this.localPort),myPromise);
>  myPromise.addListener(new FutureListener(){
>  @override
>  public void operationComplete(final Future future) throws 
> Exception {
>  if (future.isSuccess()){
>  //Connection attempt successful
>  channelFrom_B_to_C = future.get();
>  logger.info("Server B successfully connected to Server C")
>  }
>  else {
>  //Connection attempt was not successful
>  channelFrom_B_to_C = future.get().close();
>  logger.info("Server B did not successfully connect to Server C. 
> closing...")
>  }
>  });
>  
>  }
>
>
>  @Override
>  public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws 
> Exception {
>  //What ever the inbound channel (channelFrom_A_to_B) reads write it to 
> the outbound channel
>  if ( channelFrom_B_to_C.isActive()){
>  channelFrom_B_to_C.write(msg);
>  }
>  }
>
>  @Override
>  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
>  cause.printStackTrace();
>
>  if (ctx.channel().isActive()) {
>  ctx.writeAndFlush("ERR: " +
>  cause.getClass().getSimpleName() + ": " +
>  cause.getMessage() + '\n').addListener(ChannelFutureListener.CLOSE);
>  }
>  }
> }
>
>
>
> *I know I can connect Client A to Client B by the following code*
>
> public final class Client_A {
>
>  static final boolean SSL = System.getProperty("ssl") != null;
>  static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));
>  static final String HOST = System.getProperty("host", "129.456.7.9");
>  static final int PORT = Integer.parseInt(System.getProperty("port","4959"));
>
>  public static void main(String[] args) throws Exception {
>  final SslContext sslCtx;
>  if (SSL) {
>  sslCtx = SslContextBuilder.forClient()
>  .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
>  } else {
>  sslCtx = null;
>  }
>
>  // Configure the client.
>  EventLoopGroup group = new NioEventLoopGroup();
>  try {
>  Bootstrap b = new Bootstrap();
>  b.group(group)
>  .channel(NioSocketChannel.class)
>  .option(ChannelOption.TCP_NODELAY, true)
>  .handler(new ChannelInitializer() {
>  @Override
>  public void initChannel(SocketChannel 

Re: Using ChannelDuplexHandler to connect to another server

2016-09-06 Thread 'Norman Maurer' via Netty discussions
Just check our hex dump example… This does exactly what you want to do:

https://github.com/netty/netty/tree/4.1/example/src/main/java/io/netty/example/proxy
 


> On 06 Sep 2016, at 18:59, L_DisplayName  wrote:
> 
> Greetings All,
>   I wanted to know how I can use ChannelDuplexHandler from a server to 
> connect to another server? Also I would like to know if my understanding of 
> channelDuplex handler is correct and what's the difference between 
> bootstrapping a client and having it connect to a server verses having the 
> ChannelDuplexHandler connect to the server?
> 
>   For example: If I have a client A, a Server B and a Server C.  and I want 
> client A to connect to Server B and Server B connect to Server C.  where 
> pictorially  I have:
>   A-->B-->C 
> 
> Below I show Server's B Handler code, as soon as client A connects to Server 
> B (The code for this is below B's Server Handler code), I want Server B to 
> connect to Server C, can you tell me if the code below is correct or if any 
> thing needs  to be changed and if so what and how? Thank you!
> 
> Attempting to connect Server B to Server C using ChannelDuplexHandler (Note I 
> am showing Server B's handler class)
> public class Server_B_Handler extends ChannelDuplexHandler {
> 
>  private String ipAddressToConnectTo, localIpAddress;
>  private int portToConnectTo, localPort;
>  private Logger logger;
>  private ChannelHandlerContext ctx;
>  private Channel channelFrom_A_to_B;
>  private Channel channelFrom_B_to_C;
> 
>  public Server_B_Handler(String ipAddress, int port, localIpAddress, 
> localPort) {
>   ipAddressToConnectTo = ipAddress;
>  portToConnectTo = port
>  this.localIpAddress = localIpAddress;
>  this.localPort = localPort; 
>  logger = Logger.getLogger(this.getClass().getName());
>  this.ctx = null;
>  channelFrom_A_to_B = null; //Inbound channel
>  channelFrom_B_to_C = null; //Outbound Channel
>  
>  }
> 
>  @Override
>  public void channelActive(ChannelHandlerContext ctx) throws Exception{
>  //THIS CTX (Channel context) is for the channel connecting Client A to this 
> Server (Server B)
>  //Please see below for code connecting client A to Server B
>  this.ctx = ctx;
>  channelFrom_A_to_B = this.ctx().channel();
>  
>  //QUESTION HOW CAN I CONNECT TO SERVER C using this server's CONNECT METHOD?
>  
>  final Promise myPromise = this.ctx.executor().newPromise();
>  ctx.connect(new 
> InetSocketAddress(InetAddress.getByName(ipAddressToConnectTo), 
> portToConnectTo),new 
> InetSocketAddress(this.localIpAddress,this.localPort),myPromise);
>  myPromise.addListener(new FutureListener(){
>  @override
>  public void operationComplete(final Future future) throws Exception 
> {
>  if (future.isSuccess()){
>  //Connection attempt successful
>  channelFrom_B_to_C = future.get();
>  logger.info("Server B successfully connected to Server C")
>  }
>  else {
>  //Connection attempt was not successful
>  channelFrom_B_to_C = future.get().close();
>  logger.info("Server B did not successfully connect to Server C. closing...")
>  }
>  });
>  
>  }
> 
> 
>  @Override
>  public void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws 
> Exception {
>  //What ever the inbound channel (channelFrom_A_to_B) reads write it to the 
> outbound channel
>  if ( channelFrom_B_to_C.isActive()){
>  channelFrom_B_to_C.write(msg);
>  }
>  }
> 
>  @Override
>  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
>  cause.printStackTrace();
> 
>  if (ctx.channel().isActive()) {
>  ctx.writeAndFlush("ERR: " +
>  cause.getClass().getSimpleName() + ": " +
>  cause.getMessage() + '\n').addListener(ChannelFutureListener.CLOSE);
>  }
>  }
> }
> 
> 
> I know I can connect Client A to Client B by the following code
> public final class Client_A {
> 
>  static final boolean SSL = System.getProperty("ssl") != null;
>  static final int SIZE = Integer.parseInt(System.getProperty("size", "256"));
>  static final String HOST = System.getProperty("host", "129.456.7.9");
>  static final int PORT = Integer.parseInt(System.getProperty("port","4959"));
> 
>  public static void main(String[] args) throws Exception {
>  final SslContext sslCtx;
>  if (SSL) {
>  sslCtx = SslContextBuilder.forClient()
>  .trustManager(InsecureTrustManagerFactory.INSTANCE).build();
>  } else {
>  sslCtx = null;
>  }
> 
>  // Configure the client.
>  EventLoopGroup group = new NioEventLoopGroup();
>  try {
>  Bootstrap b = new Bootstrap();
>  b.group(group)
>  .channel(NioSocketChannel.class)
>  .option(ChannelOption.TCP_NODELAY, true)
>  .handler(new ChannelInitializer() {
>  @Override
>  public void initChannel(SocketChannel ch) throws Exception {
>  ChannelPipeline p = ch.pipeline();
>  if (sslCtx != null) {
>  p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT));
>  }
>  //p.addLast(new LoggingHandler(LogLevel.INFO));
>  p.addLast(
>  new