public void run() { // Configure the client. ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // Set up the pipeline factory. bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( new EchoClientHandler(firstMessageSize)); } }); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection is closed or the connection attempt fails. future.getChannel().getCloseFuture().awaitUninterruptibly(); // Shut down thread pools to exit. bootstrap.releaseExternalResources(); }
InetSocketAddress getRemoteAddress() { return (InetSocketAddress) bootstrap.getOption("remoteAddress"); }
@Override public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { // Suspend incoming traffic until connected to the remote host. final Channel inboundChannel = e.getChannel(); inboundChannel.setReadable(false); // Start the connection attempt. ClientBootstrap cb = new ClientBootstrap(cf); cb.getPipeline().addLast("handler", new OutboundHandler(e.getChannel())); ChannelFuture f = cb.connect(new InetSocketAddress(remoteHost, remotePort)); outboundChannel = f.getChannel(); f.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // Connection attempt succeeded: // Begin to accept incoming traffic. inboundChannel.setReadable(true); } else { // Close the connection if the connection attempt has failed. inboundChannel.close(); } } }); }
public void run() { // Configure the client. ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // Set up the pipeline factory. bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( new ObjectEncoder(), new ObjectDecoder(), new ObjectEchoClientHandler(firstMessageSize)); } }); // Start the connection attempt. bootstrap.connect(new InetSocketAddress(host, port)); }
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); bootstrap.releaseExternalResources(); return;
ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), bootstrap.setPipelineFactory(new HttpUploadClientPipelineFactory(ssl)); bootstrap.releaseExternalResources();
public void run(Timeout timeout) throws Exception { println("Reconnecting to: " + getRemoteAddress()); bootstrap.connect(); } }, UptimeClient.RECONNECT_DELAY, TimeUnit.SECONDS);
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); bootstrap.releaseExternalResources(); return null; } catch (URISyntaxException e) { System.err.println("Error: " + e.getMessage()); bootstrap.releaseExternalResources(); return null;
public void run() { // Configure the client. ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // Set up the pipeline factory. bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( new DiscardClientHandler(firstMessageSize)); } }); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection is closed or the connection attempt fails. future.getChannel().getCloseFuture().awaitUninterruptibly(); // Shut down thread pools to exit. bootstrap.releaseExternalResources(); }
ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); bootstrap.releaseExternalResources(); return null;
public void run() { // Configure the client. ClientBootstrap bootstrap = new ClientBootstrap( new SctpClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); final ExecutionHandler executionHandler = new ExecutionHandler( new OrderedMemoryAwareThreadPoolExecutor(16, 1048576, 1048576)); // Set up the pipeline factory. bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline(executionHandler, new SctpClientHandler()); } }); bootstrap.setOption("sendBufferSize", 1048576); bootstrap.setOption("receiveBufferSize", 1048576); bootstrap.setOption("sctpNoDelay", true); // Start the connection attempt. ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection is closed or the connection attempt fails. future.getChannel().getCloseFuture().awaitUninterruptibly(); // Please check SctpClientHandler to see, how echo message is sent & received // Shut down thread pools to exit. bootstrap.releaseExternalResources(); }
public void run() { // Initialize the timer that schedules subsequent reconnection attempts. final Timer timer = new HashedWheelTimer(); // Configure the client. final ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // Configure the pipeline factory. bootstrap.setPipelineFactory(new ChannelPipelineFactory() { private final ChannelHandler timeoutHandler = new ReadTimeoutHandler(timer, READ_TIMEOUT); private final ChannelHandler uptimeHandler = new UptimeClientHandler(bootstrap, timer); public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( timeoutHandler, uptimeHandler); } }); bootstrap.setOption( "remoteAddress", new InetSocketAddress(host, port)); // Initiate the first connection attempt - the rest is handled by // UptimeClientHandler. bootstrap.connect(); }
public void run() { // Set up. ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // Configure the event pipeline factory. bootstrap.setPipelineFactory(new LocalTimeClientPipelineFactory()); // Make a new connection. ChannelFuture connectFuture = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection is made successfully. Channel channel = connectFuture.awaitUninterruptibly().getChannel(); // Get the handler instance to initiate the request. LocalTimeClientHandler handler = channel.getPipeline().get(LocalTimeClientHandler.class); // Request and get the response. List<String> response = handler.getLocalTimes(cities); // Close the connection. channel.close().awaitUninterruptibly(); // Shut down all thread pools to exit. bootstrap.releaseExternalResources(); // Print the response at last but not least. Iterator<String> i1 = cities.iterator(); Iterator<String> i2 = response.iterator(); while (i1.hasNext()) { System.out.format("%28s: %s%n", i1.next(), i2.next()); } }
public void run() { // Configure the client. ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), Executors.newCachedThreadPool())); // Set up the event pipeline factory. bootstrap.setPipelineFactory(new FactorialClientPipelineFactory(count)); // Make a new connection. ChannelFuture connectFuture = bootstrap.connect(new InetSocketAddress(host, port)); // Wait until the connection is made successfully. Channel channel = connectFuture.awaitUninterruptibly().getChannel(); // Get the handler instance to retrieve the answer. FactorialClientHandler handler = (FactorialClientHandler) channel.getPipeline().getLast(); // Print out the answer. System.err.format( "Factorial of %,d is: %,d", count, handler.getFactorial()); // Shut down all thread pools to exit. bootstrap.releaseExternalResources(); }
public void run() { final ExecutorService executorService = Executors.newCachedThreadPool(); final ClientBootstrap bootstrap = new ClientBootstrap(new IoStreamChannelFactory(executorService)); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { ChannelFuture connectFuture = bootstrap.connect(new IoStreamAddress(System.in, System.out)); bootstrap.releaseExternalResources();
public void run() throws IOException { ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), bootstrap.setPipelineFactory(new SecureChatClientPipelineFactory()); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); if (!future.isSuccess()) { future.getCause().printStackTrace(); bootstrap.releaseExternalResources(); return; bootstrap.releaseExternalResources();
public void run() throws IOException { ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), bootstrap.setPipelineFactory(new TelnetClientPipelineFactory()); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); if (!future.isSuccess()) { future.getCause().printStackTrace(); bootstrap.releaseExternalResources(); return; bootstrap.releaseExternalResources();
ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), bootstrap.setPipelineFactory(new HttpSnoopClientPipelineFactory(ssl)); ChannelFuture future = bootstrap.connect(new InetSocketAddress(host, port)); if (!future.isSuccess()) { future.getCause().printStackTrace(); bootstrap.releaseExternalResources(); return; bootstrap.releaseExternalResources();
ClientBootstrap cb = new ClientBootstrap( new DefaultLocalClientChannelFactory()); cb.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { return Channels.pipeline( ChannelFuture channelFuture = cb.connect(socketAddress); channelFuture.awaitUninterruptibly(); cb.releaseExternalResources(); sb.releaseExternalResources();
public void run() throws Exception { ClientBootstrap bootstrap = new ClientBootstrap( new NioClientSocketChannelFactory( Executors.newCachedThreadPool(), uri, WebSocketVersion.V13, null, false, customHeaders); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); bootstrap.connect( new InetSocketAddress(uri.getHost(), uri.getPort())); future.awaitUninterruptibly().rethrowIfFailed(); ch.close(); bootstrap.releaseExternalResources();