List of usage examples for io.netty.channel ChannelFuture channel
Channel channel();
From source file:com.sangupta.swift.netty.proxy.ProxyFrontendHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext channelHandlerContext) { final Channel inboundChannel = channelHandlerContext.channel(); // Start the connection attempt. Bootstrap bootstrap = new Bootstrap(); bootstrap.group(inboundChannel.eventLoop()).channel(channelHandlerContext.channel().getClass()) .handler(new ProxyBackendHandler(inboundChannel)).option(ChannelOption.AUTO_READ, false); ChannelFuture channelFuture = bootstrap.connect(remoteHost, remotePort); outboundChannel = channelFuture.channel(); channelFuture.addListener(new ChannelFutureListener() { @Override//from ww w. j av a2 s.co m public void operationComplete(ChannelFuture future) { 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.sangupta.swift.netty.proxy.ProxyFrontendHandler.java
License:Apache License
@Override public void channelRead(final ChannelHandlerContext channelHandlerContext, Object message) { if (outboundChannel.isActive()) { outboundChannel.writeAndFlush(message).addListener(new ChannelFutureListener() { @Override/* www . jav a 2 s. co m*/ public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // was able to flush out data, start to read the next chunk channelHandlerContext.channel().read(); } else { future.channel().close(); } } }); } }
From source file:com.seed.nettyechoserver.EchoServer.java
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(1); try {//from w w w . j av a 2s . c om 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 { ChannelPipeline p = ch.pipeline(); //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); ChannelFuture f = b.bind(PORT).sync(); f.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.sheldon.javaPrj.netty.DiscardServer.java
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w. j ava2 s . c o m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new DiscardServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.sheldon.javaPrj.netty.FileClient.java
public void start() { EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w . j a v a2 s . c o m*/ Bootstrap b = new Bootstrap(); b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ChunkedWriteHandler(), new FileClientHandler()); } }); ChannelFuture f = b.connect("localhost", port).sync(); f.channel().closeFuture().sync(); } catch (InterruptedException ex) { Logger.getLogger(FileClient.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); } }
From source file:com.sheldon.javaPrj.netty.FileServer.java
public void start() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w. jav a 2 s.com ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new FileServerHandler()); } }); ChannelFuture f = b.bind(port).sync(); System.out.println("server start"); f.channel().closeFuture().sync(); System.out.println("server down"); } catch (InterruptedException ex) { Logger.getLogger(FileServer.class.getName()).log(Level.SEVERE, null, ex); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.sheldon.javaPrj.netty.TimeClient.java
public static void main(String args[]) throws InterruptedException { String host = args.length > 0 ? args[0] : "localhost"; int port = Integer.parseInt(args.length > 1 ? args[1] : "8080"); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w .ja v a2s.co m*/ Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeClientHandler()); } }); ChannelFuture f = b.connect(host, port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); } }
From source file:com.sheldon.javaPrj.netty.TimeServer.java
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* w w w. ja v a2 s . c o m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.sitech.crmpd.idmm2.ble.EchoClient.java
License:Apache License
public static void consumer(String[] args) throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {/*from w w w . j av a 2 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(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.TRACE)); p.addLast(new FrameCodeC()); p.addLast(new ConsumerClientHandler(clientid, target_topic)); } }); // Start the client. ChannelFuture f = b.connect(ble_ip, ble_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.sitech.crmpd.idmm2.ble.EchoClient.java
License:Apache License
public static void producer(String[] args) throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {// w w w . ja va 2s . 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(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new LoggingHandler(LogLevel.TRACE)); p.addLast(new FrameCodeC()); p.addLast(new ProducerClientHandler(clientid, target_topic)); } }); // Start the client. ChannelFuture f = b.connect(ble_ip, ble_port).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }