List of usage examples for io.netty.channel ChannelPipeline addLast
ChannelPipeline addLast(ChannelHandler... handlers);
From source file:com.couchbase.client.core.io.endpoint.AbstractEndpoint.java
License:Open Source License
/** * Create a new {@link AbstractEndpoint} and supply essential params. * * @param addr the socket address to connect to. * @param env the environment to attach to. * @param group the {@link EventLoopGroup} to use. *///w ww . j a va2 s . c om protected AbstractEndpoint(final InetSocketAddress addr, final Environment env, final EventLoopGroup group) { this.env = env; endpointStateDeferred = Streams.defer(env, defaultPromiseEnv); endpointStateStream = endpointStateDeferred.compose(); connectionBootstrap = new BootstrapAdapter(new Bootstrap().group(group).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(final SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (LOGGER.isTraceEnabled()) { pipeline.addLast(new LoggingHandler(LogLevel.TRACE)); } customEndpointHandlers(pipeline); pipeline.addLast(new GenericEndpointHandler<REQ, RES>()); } }).option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.TCP_NODELAY, false).option(ChannelOption.WRITE_BUFFER_LOW_WATER_MARK, 1500) .remoteAddress(addr)); }
From source file:com.couchbase.client.core.io.endpoint.design.DesignEndpoint.java
License:Open Source License
@Override protected void customEndpointHandlers(final ChannelPipeline pipeline) { pipeline.addLast(new HttpClientCodec()).addLast(new HttpObjectAggregator(Integer.MAX_VALUE)) .addLast(new DesignCodec()); }
From source file:com.couchbase.client.core.io.endpoint.memcache.MemcacheEndpoint.java
License:Open Source License
@Override protected void customEndpointHandlers(final ChannelPipeline pipeline) { pipeline.addLast(new BinaryMemcacheClientCodec()) .addLast(new BinaryMemcacheObjectAggregator(Integer.MAX_VALUE)).addLast(new MemcacheCodec()); }
From source file:com.creamsugardonut.HttpStaticFileServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new CustomHttpServerCodec()); pipeline.addLast(new HttpStaticFileServerHandler2()); }
From source file:com.crystal.chat.SecureChatClientInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Add SSL handler first to encrypt and decrypt everything. // In this example, we use a bogus certificate in the server side // and accept any invalid certificates in the client side. // You will need something more complicated to identify both // and server in the real world. // pipeline.addLast(sslCtx.newHandler(ch.alloc(), SecureChatClient.HOST, Port.PORT)); // On top of the SSL handler, add the text line codec. pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); // and then business logic. pipeline.addLast(new SecureChatClientHandler()); }
From source file:com.crystal.chat.SecureChatServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Add SSL handler first to encrypt and decrypt everything. // In this example, we use a bogus certificate in the server side // and accept any invalid certificates in the client side. // You will need something more complicated to identify both // and server in the real world. // pipeline.addLast(sslCtx.newHandler(ch.alloc())); // On top of the SSL handler, add the text line codec. pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast(new StringDecoder()); pipeline.addLast(new StringEncoder()); // and then business logic. pipeline.addLast(new SecureChatServerHandler()); }
From source file:com.ctrip.xpipe.redis.console.health.netty.EchoClient.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL.git final SslContext sslCtx; if (SSL) {/*from ww w . j a v a2 s . c om*/ sslCtx = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE).build(); } else { sslCtx = null; } // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try { 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(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc(), HOST, PORT)); } // p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoClientHandler()); } }); // Start the client. ChannelFuture f = b.connect(HOST, PORT); Channel c = f.channel(); while (true) { ByteBuf buf = Unpooled.buffer(8); buf.writeLong(System.nanoTime()); c.writeAndFlush(buf); Thread.sleep(1000); } } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }
From source file:com.ctrip.xpipe.redis.console.health.netty.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/*from ww w . j a va2 s. com*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).option(ChannelOption.TCP_NODELAY, true) .handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); // 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.dch.app.nwmq.netty.NettyServerInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); if (sslCtx != null) { pipeline.addLast(sslCtx.newHandler(ch.alloc())); }/*from w w w.j a v a2 s.co m*/ pipeline.addLast(new HttpServerCodec()); pipeline.addLast(new HttpObjectAggregator(65536)); // pipeline.addLast(new WebSocketServerCompressionHandler()); pipeline.addLast(new NettyServerHandler()); }
From source file:com.demo.netty.echo.EchoServer.java
License:Apache License
public static void main(String[] args) throws Exception { // Configure SSL. final SslContext sslCtx; if (SSL) {/* w w w . j av a 2 s. co m*/ SelfSignedCertificate ssc = new SelfSignedCertificate(); sslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build(); } else { sslCtx = null; } //EventLoopGroup is abstraction of Notification and Thread loop EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { 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(); if (sslCtx != null) { p.addLast(sslCtx.newHandler(ch.alloc())); } //p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); // 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(); } }