On Nov 22, 2007 3:45 AM, Frédéric Brégier <[EMAIL PROTECTED]> wrote:
> Now the client code... As previously, any comment and correction ...
>
> NioSocketConnector connector = new NioSocketConnector(myNumberOfCpuPlusOne);
> // Set connect timeout.
> connector.setConnectTimeout(myTIMEOUTCON);
>
> SocketSessionConfig config = (SocketSessionConfig)
> connector.getSessionConfig();
> config.setReuseAddress(true); // XXX was false but is true better for client ?
> //XXX VISTA BUG ? => setSoLinger and KeepAlive(true)
> config.setSoLinger(1);
> config.setKeepAlive(false); // true?
> // No delay
> config.setTcpNoDelay(true);
> // set idle timeout
> config.setIdleTime(IdleStatus.BOTH_IDLE, myTIMEOUT);
> config.setReceiveBufferSize(myTCPBLOCKSIZE);
> config.setSendBufferSize(myTCPBLOCKSIZE);
>
> DefaultIoFilterChainBuilder chain = connector.getFilterChain();
> CompressionFilter compression =
> new CompressionFilter(serverCompressed,
> clientCompressed,
> CompressionFilter.COMPRESSION_MAX);
> chain.addFirst("Compression",compression);
> ExecutorService executorChainService = new
> OrderedThreadPoolExecutor(myNbClient_THREAD);
> // was Executors.newCachedThreadPool();
> ExecutorFilter executorChainFilter = new ExecutorFilter(executorChainService);
> chain.addLast("SerializePool", executorChainFilter);
> ProtocolCodecFactory codec = new ObjectSerializationCodecFactory();
> chain.addLast("Serialize", new ProtocolCodecFilter(codec));
> connector.setHandler(new MyIoHandlerAdapter());
>
> // IoBuffer
> IoBuffer.setUseDirectBuffer(false);
> ioBufferAllocator = new SimpleBufferAllocator();
> IoBuffer.setAllocator(ioBufferAllocator);
>
> // Connection
> IoSession ioSession = null;
> try {
> ConnectFuture future = connector.connect(inetSocketAddress);
> if (future != null) {
> future.awaitUninterruptibly();
> IoSession ioSession = future.getSession();
> if (ioSession == null) {
> // Problem of connection !
> }
> }
> } catch( RuntimeIoException e ) {// Connector is ko=>retry later on
> ioSession = null;
> }
> if (ioSession != null) {
> // set idle timeout
> // Change from Trunk 6 month ago : it seems to be now in
> SocketSessionConfig directly
> /*ioSession.setIdleTime(IdleStatus.BOTH_IDLE,
> LSDConstants.TIMEOUT);*/
> }
>
>
> And shutting down :
> if (ioBufferAllocator != null)
> ioBufferAllocator.dispose();
> ioBufferAllocator = null;
> if (executorChainFilter != null) {
> executorChainFilter.shutdownNow();
> executorChainFilter = null;
> }
Again, you need to call connector.dispose() to release all resources
such as Selector and Thread. Except that, it looks perfect!
You could also use SocketAcceptor and SocketConnector interface
instead of NioSocketAcceptor and NioSocketConnector concrete class
because you might want to switch to AprSocketAcceptor and
AprSocketConnector in the future.
HTH,
Trustin
--
what we call human nature is actually human habit
--
http://gleamynode.net/
--
PGP Key ID: 0x0255ECA6