List of usage examples for io.netty.channel ChannelFuture channel
Channel channel();
From source file:com.hop.hhxx.example.sctp.multihoming.SctpMultiHomingEchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {/*from w w w . j a v a 2s. com*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSctpChannel.class).option(SctpChannelOption.SCTP_NODELAY, true) .handler(new ChannelInitializer<SctpChannel>() { @Override public void initChannel(SctpChannel ch) throws Exception { ch.pipeline().addLast( // new LoggingHandler(LogLevel.INFO), new SctpEchoClientHandler()); } }); InetSocketAddress localAddress = new InetSocketAddress(CLIENT_PRIMARY_HOST, CLIENT_PORT); InetAddress localSecondaryAddress = InetAddress.getByName(CLIENT_SECONDARY_HOST); InetSocketAddress remoteAddress = new InetSocketAddress(SERVER_REMOTE_HOST, SERVER_REMOTE_PORT); // Bind the client channel. ChannelFuture bindFuture = b.bind(localAddress).sync(); // Get the underlying sctp channel SctpChannel channel = (SctpChannel) bindFuture.channel(); // Bind the secondary address. // Please note that, bindAddress in the client channel should be done before connecting if you have not // enable Dynamic Address Configuration. See net.sctp.addip_enable kernel param channel.bindAddress(localSecondaryAddress).sync(); // Finish connect ChannelFuture connectFuture = channel.connect(remoteAddress).sync(); // Wait until the connection is closed. connectFuture.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.hop.hhxx.example.sctp.multihoming.SctpMultiHomingEchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w .j av a 2 s . c o m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioSctpServerChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SctpChannel>() { @Override public void initChannel(SctpChannel ch) throws Exception { ch.pipeline().addLast( // new LoggingHandler(LogLevel.INFO), new SctpEchoServerHandler()); } }); InetSocketAddress localAddress = new InetSocketAddress(SERVER_PRIMARY_HOST, SERVER_PORT); InetAddress localSecondaryAddress = InetAddress.getByName(SERVER_SECONDARY_HOST); // Bind the server to primary address. ChannelFuture bindFuture = b.bind(localAddress).sync(); //Get the underlying sctp channel SctpServerChannel channel = (SctpServerChannel) bindFuture.channel(); //Bind the secondary address ChannelFuture connectFuture = channel.bindAddress(localSecondaryAddress).sync(); // Wait until the connection is closed. connectFuture.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.hxr.javatone.concurrency.netty.official.applet.AppletDiscardServer.java
License:Apache License
@Override public void init() { bossGroup = new NioEventLoopGroup(); workerGroup = new NioEventLoopGroup(); try {// ww w. j av a2 s.c o m ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(final SocketChannel ch) throws Exception { ch.pipeline().addLast(new DiscardServerHandler()); } }); ChannelFuture f = bootstrap.bind(9999).sync(); f.channel().closeFuture().sync(); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } }
From source file:com.hxr.javatone.concurrency.netty.official.echo.EchoClient.java
License:Apache License
public void run() throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {//from w w w. java2 s .c o m Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(final SocketChannel ch) throws Exception { ch.pipeline().addLast( //new LoggingHandler(LogLevel.INFO), new EchoClientHandler(firstMessageSize)); } }); // Start the client. ChannelFuture f = b.connect(host, port).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.hxr.javatone.concurrency.netty.official.factorial.FactorialClient.java
License:Apache License
public void run() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {//www . j a v a 2 s . c o m Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new FactorialClientInitializer(count)); // Make a new connection. ChannelFuture f = b.connect(host, port).sync(); // Get the handler instance to retrieve the answer. FactorialClientHandler handler = (FactorialClientHandler) f.channel().pipeline().last(); // Print out the answer. System.err.format("Factorial of %,d is: %,d", count, handler.getFactorial()); } finally { group.shutdownGracefully(); } }
From source file:com.hxr.javatone.concurrency.netty.official.filetransfer.FileServer.java
License:Apache License
public void run() throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w . j a v a 2 s .co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(8192), new StringDecoder(CharsetUtil.UTF_8), new FileHandler()); } }); // Start the server. ChannelFuture f = b.bind(port).sync(); // Wait until the server socket is closed. f.channel().closeFuture().sync(); } finally { // Shut down all event loops to terminate all threads. bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.hxr.javatone.concurrency.netty.official.proxy.HexDumpProxyFrontendHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { final Channel inboundChannel = ctx.channel(); // Start the connection attempt. Bootstrap b = new Bootstrap(); b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass()) .handler(new HexDumpProxyBackendHandler(inboundChannel)).option(ChannelOption.AUTO_READ, false); ChannelFuture f = b.connect(remoteHost, remotePort); outboundChannel = f.channel(); f.addListener(new ChannelFutureListener() { @Override/*from ww w . j a v a2 s . co m*/ public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // connection complete start to read first data inboundChannel.read(); } else { // Close the connection if the connection attempt has failed. inboundChannel.close(); } } }); }
From source file:com.hxr.javatone.concurrency.netty.official.rxtx.RxtxClient.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup group = new OioEventLoopGroup(); try {/* w w w . j a v a 2 s . c om*/ Bootstrap b = new Bootstrap(); b.group(group).channel(RxtxChannel.class).handler(new ChannelInitializer<RxtxChannel>() { @Override public void initChannel(RxtxChannel ch) throws Exception { ch.pipeline().addLast(new LineBasedFrameDecoder(32768), new StringEncoder(), new StringDecoder(), new RxtxClientHandler()); } }); ChannelFuture f = b.connect(new RxtxDeviceAddress("/dev/ttyUSB0")).sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:com.ibasco.agql.core.transport.NettyTransport.java
License:Open Source License
/** * <p>A method to send data over the transport. Since the current netty version does not yet support {@link * CompletableFuture}, we need to convert the returned {@link ChannelFuture} to it's {@link CompletableFuture} * version.</p>/*from w w w . j a va 2 s .com*/ * * @param channel * The underlying {@link Channel} to be used for data transport. * @param data * An instance of {@link AbstractMessage} that will be sent through the transport * @param flushImmediately * True if transport should immediately flush the message after send. * * @return A {@link CompletableFuture} with return type of {@link Channel} (The channel used for the transport) */ private CompletableFuture<Void> writeToChannel(Channel channel, Msg data, boolean flushImmediately) { final CompletableFuture<Void> writeResultFuture = new CompletableFuture<>(); log.debug("Writing data '{}' to channel : {}", data, channel); final ChannelFuture writeFuture = (flushImmediately) ? channel.writeAndFlush(data) : channel.write(data); writeFuture.addListener((ChannelFuture future) -> { try { if (future.isSuccess()) writeResultFuture.complete(null); else writeResultFuture.completeExceptionally(future.cause()); } finally { cleanupChannel(future.channel()); } }); return writeResultFuture; }
From source file:com.ibasco.agql.core.transport.tcp.NettyBasicTcpTransport.java
License:Open Source License
@Override public CompletableFuture<Channel> getChannel(M address) { final CompletableFuture<Channel> channelFuture = new CompletableFuture<>(); ChannelFuture f = getBootstrap().connect(address.recipient()); //Acquire from pool and listen for completion f.addListener((ChannelFuture future) -> { if (future.isSuccess()) { channelFuture.complete(future.channel()); } else {/* ww w . j a v a2s. com*/ channelFuture.completeExceptionally(future.cause()); } }); return channelFuture; }