How to find Channel request port

Richard Jackson richard.jackson at gmail.com
Mon Mar 23 00:07:19 EDT 2009


This may be covered somewhere but I can't seem to be able to find what
I'm looking for. So lets assume I have done something like this:

        ServerBootstrap bootstrap = new ServerBootstrap(factory);
        HttpServerPipelineFactory pipeline =
                new HttpServerPipelineFactory(new HttpRequestHandler());
          bootstrap.setPipelineFactory(pipeline);
          // Configure our server code goes here....
          ....
          bootstrap.bind(8080);
          bootstrap.bind(8081);
          .... More stuff...

As you can see I have bound two ports to the same server. So the
question is how do I find out in my handler which port the request
came in on? I do see that from the channel I can get a SocketAddress
with Channel.getLocalAddress() but that still will not tell me which
port the request came in on. Currently the only way I can think of
solving this is to create a unique pipeline factory for each port
where the factory will know which port it is bound to. So something
like:

        ChannelPipeline pipeline = new DefaultChannelPipeline();
        pipeline.addLast("decoder", new HttpRequestDecoder());
        pipeline.addLast("encoder", new HttpResponseEncoder());
        pipeline.addLast("handler", portInjector);
        pipeline.addLast("handler", handler);
        return pipeline;

Where each factory would have a unique portInjector but use the same
handler instance. Where the sole purpose of the portInjector would be
to add a port header to the HttpMessage but that just seams like a
hack to me there has to be a better way to do this.

Thanks
Richard



More information about the netty-users mailing list