/** * Creates a new instance that talks to a {@link DefaultServer} on the specified host and port. * * @param clientId * A unique id. No two clients with the same id can access the same * {@link DefaultServer} * @param host * The host the {@link DefaultServer} runs on * @param port * The port the {@link DefaultServer} runs on */ public DefaultClient(final String clientId, final String host, final int port) { this.clientId = clientId; this.host = host; this.port = port; // Configure the client. bootstrap = new ClientBootstrap( new OioClientSocketChannelFactory(Executors.newCachedThreadPool(new DaemonThreadFactory()))); // Set up the pipeline factory. bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( new ObjectEncoder(ENCODER_ESTIMATED_LENGTH), new ObjectDecoder(DECODER_ESTIMATED_LENGTH, ClassResolvers.weakCachingResolver(null)), new ClientHandshakeHandler(new Handshake(clientId), HANDSHAKE_TIMEOUT_MILLIS), clientHandler); } }); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", true); bootstrap.setOption("soTimeout", 10000L); }
private static ClientSocketChannelFactory getClientSocketChannelFactory(boolean nio) { if ( nio) return new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); else return new OioClientSocketChannelFactory( Executors.newCachedThreadPool()); }