List of usage examples for io.netty.channel ChannelFuture channel
Channel channel();
From source file:com.necla.simba.server.gateway.server.backend.BackendConnector.java
License:Apache License
public void connect(Properties props, List<String> backends, SubscriptionManager subscriptionManager, ClientAuthenticationManager cam) throws URISyntaxException, IOException { this.subscriptionManager = subscriptionManager; this.cam = cam; hasher = new ConsistentHash(backends); int nthreads = Integer.parseInt(props.getProperty("backend.server.thread_count", "-1")); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(nthreads == -1 ? new NioEventLoopGroup() : new NioEventLoopGroup(nthreads)) .handler(new BackendConnectorIntializer()).channel(NioSocketChannel.class); try {//from w ww.j a va2 s .c o m for (String s : backends) { URI uri = new URI("my://" + s); if (uri.getHost() == null || uri.getPort() == -1) throw new URISyntaxException(s, "Both host and port must be specified for host"); LOG.debug("Connecting to: " + uri.getHost() + ":" + uri.getPort()); ChannelFuture cf = bootstrap.connect(uri.getHost(), uri.getPort()).sync(); if (cf.isSuccess()) this.backends.put(s, cf.channel()); else throw new IOException("Could not connect to backend " + s + ":" + uri.getPort()); } } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.necla.simba.server.gateway.server.frontend.FrontendServer.java
License:Apache License
public FrontendServer(Properties props, BackendConnector backend, StatsCollector stats, SubscriptionManager subscriptionManager, ClientAuthenticationManager cam) throws Exception { this.backend = backend; this.stats = stats; assert this.stats != null : "stats must be there already"; this.subscriptionManager = subscriptionManager; this.cam = cam; ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class) .childHandler(new FrontendServerInitializer()).option(ChannelOption.SO_BACKLOG, 128); int port = Integer.parseInt(props.getProperty("port", "9000")); ChannelFuture f = bootstrap.bind(port).sync(); f.channel().closeFuture().sync(); }
From source file:com.netty.fileTest.file.FileReaderClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {/*w w w . j a v a2 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 FileReaderClientHandler()); } }); // Start the client. ChannelFuture f = b.connect("127.0.0.1", 8007).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.netty.fileTest.file.FileSenderServer.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 . ja v a 2 s. c o 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 { ChannelPipeline p = ch.pipeline(); p.addLast(new StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(8192), new StringDecoder(CharsetUtil.UTF_8), new ChunkedWriteHandler(), new FileSenderServerHandler()); } }); // 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.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java
License:Apache License
private void forward(final ChannelHandlerContext ctx, ByteBuf byteBuf) { if (inboundChannel.isActive()) { inboundChannel.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { ctx.channel().read(); } else { future.channel().close(); }/*from w w w . j a v a 2 s . co m*/ } }); } else { } }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java
License:Apache License
private void handleSettingFrame(final ChannelHandlerContext ctx, Http2Flags flags) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer();/*from w w w .j ava 2 s. c o m*/ if (!flags.ack()) { //00 00 0c 04 00 00 00 00 00 00 03 7f ff ff ff 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x0c); byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x03); byteBuf.writeByte(0x7f); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0x00); //04 00 10 00 00 byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x10); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } else { // System.out.println("********************* setting ack received ..."); //00 00 00 04 01 00 00 00 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x01); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // System.out.println(" ...operationComplete isSuccess"); ctx.channel().read(); } else { // System.out.println("...operationComplete failure"); future.channel().close(); } } }); }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java
License:Apache License
private void handleWindowsUpdateFrame(final ChannelHandlerContext ctx) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer();/*from ww w.j a v a2s . c om*/ // 00 00 04 08 00 00 00 00 00 00 0f 00 01 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x08); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x0f); byteBuf.writeByte(0x00); byteBuf.writeByte(0x01); ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { ctx.channel().read(); } else { future.channel().close(); } } }); }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) { final Channel inboundChannel = ctx.channel(); // ??/*from w ww. j a va 2s . com*/ Bootstrap b = new Bootstrap(); b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass()) .handler(new GrpcProxyBackendHandler(inboundChannel)).option(ChannelOption.AUTO_READ, false); for (int i = 0; i < remoteHosts.length; i++) { final ChannelFuture f = b.connect(remoteHosts[i], remotePorts[i]); outboundChannels[i] = f.channel(); f.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // connection complete start to read first data inboundChannel.read(); // System.out.println(f.channel().remoteAddress() + ", " + f.channel().localAddress()); } else { // Close the connection if the connection attempt has failed. // System.out.println("channelActive close" + inboundChannel.remoteAddress() + ", " + inboundChannel.localAddress()); inboundChannel.close(); } } }); } }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java
License:Apache License
private void handleSettingFrame(final ChannelHandlerContext ctx, Http2Flags flags) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer();//from w ww .j av a 2 s .co m if (!flags.ack()) { // System.out.println("********************* setting received ..."); //00 00 0c 04 00 00 00 00 00 00 03 7f ff ff ff 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x0c); byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x03); byteBuf.writeByte(0x7f); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0x00); //04 00 10 00 00 byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x10); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } else { // System.out.println("********************* setting ack received ..."); //00 00 00 04 01 00 00 00 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x01); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // System.out.println(" ...operationComplete isSuccess"); ctx.channel().read(); } else { // System.out.println("...operationComplete failure"); future.channel().close(); } } }); }
From source file:com.nettyhttpserver.server.NettyServer.java
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*w w w.jav a 2 s . c om*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new NettyServerInitializer()).option(ChannelOption.SO_BACKLOG, 1024) .childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture cf = b.bind(port).sync(); cf.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }