Re: [Simpleweb-Support] Shared services
Brought to you by:
niallg
From: Niall G. <nia...@an...> - 2004-09-01 09:19:09
|
Hi Christophe, > I try to share a service between multiple connections: > > Connection connection = ConnectionFactory.getConnection(handler > for (int i=_fromPort; i<=_toPort; ++i) { > _logger.debug("starting socket on port: ["+i+"]"); > connection.connect(new ServerSocket(i)); > } > > It seems to work fine. > I just wonder how I can know the port from the service. It's possible to get > the IPAddress: > > public void process(Request req, Response resp) throws Exception { > req.getInetAddress(); > ... > } > > But can I also get the port? To get the port number you can implement a PipelineFactory like so. public class MyPipelineFactory implements PipelineFactory { public Pipeline getInstance(Socket sock) { Pipeline pipe = new Pipeline(sock); pipe.setAttribute("port", new Integer(sock.getPort())); pipe.setAttribute("localPort", new Integer(sock.getLocalPort())); pipe.setAttribute("socket", sock); return pipe; } } Now to create the connection..... PipelineFactory fac = new MyPipelineFactory(); Connection con = ConnectionFactory.getConnection(handler, fac); Now to retrieve the port from the Request..... public class MyService extends BasicService { public void process(Request req, Response resp) throws Exception { Attributes attrs = req.getAttributes(); Integer port = (Integer)attrs.getAttribute("port"); Integer localPort = (Integer)attrs.getAttribute("localPort"); // etc ... } } |