List of usage examples for io.netty.channel ChannelPipeline addLast
ChannelPipeline addLast(EventExecutorGroup group, ChannelHandler... handlers);
From source file:bench.netty.HttpClientInitializer.java
License:Apache License
@Override public void initChannel(final SocketChannel ch) throws Exception { final ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("codec", new HttpClientCodec()); pipeline.addLast("packer", new HttpObjectAggregator(512 * 1024)); pipeline.addLast("handler", new HttpClientHandler()); }
From source file:bench.netty.HttpServerInitializer.java
License:Apache License
@Override public void initChannel(final SocketChannel ch) throws Exception { final ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("codec", new HttpServerCodec()); pipeline.addLast("packer", new HttpObjectAggregator(512 * 1024)); pipeline.addLast("handler", new HttpServerHandler()); }
From source file:books.netty.protocol.websocket.server.WebSocketServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w ww . j a v a 2 s. co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("http-codec", new HttpServerCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler()); pipeline.addLast("handler", new WebSocketServerHandler()); } }); Channel ch = b.bind(port).sync().channel(); System.out.println("Web socket server started at port " + port + '.'); System.out.println("Open your browser and navigate to http://localhost:" + port + '/'); ch.closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:books.netty.ssl.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. SSLEngine engine = null;/* w w w . j a v a 2 s. c om*/ if (SSLMODE.CA.toString().equals(tlsMode)) { engine = SecureChatSslContextFactory .getClientContext(tlsMode, null, System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/client/cChat.jks") .createSSLEngine(); } else if (SSLMODE.CSA.toString().equals(tlsMode)) { engine = SecureChatSslContextFactory .getClientContext(tlsMode, System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/cChat.jks", System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/cChat.jks") .createSSLEngine(); // engine = SecureChatSslContextFactory // .getClientContext( // tlsMode, // System.getProperty("user.dir") // + "/src/com/phei/netty/ssl/conf/client/cChat.jks", // System.getProperty("user.dir") // + "/src/com/phei/netty/ssl/conf/client/cChat.jks") // .createSSLEngine(); } else { System.err.println("ERROR : " + tlsMode); System.exit(-1); } engine.setUseClientMode(true); pipeline.addLast("ssl", new SslHandler(engine)); // On top of the SSL handler, add the text line codec. pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); // and then business logic. pipeline.addLast("handler", new SecureChatClientHandler()); }
From source file:books.netty.ssl.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. ////from ww w . j a va 2 s .co m // Read SecureChatSslContextFactory // if you need client certificate authentication. SSLEngine engine = null; if (SSLMODE.CA.toString().equals(tlsMode)) { engine = SecureChatSslContextFactory .getServerContext(tlsMode, System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/client/sChat.jks", null) .createSSLEngine(); } else if (SSLMODE.CSA.toString().equals(tlsMode)) { engine = SecureChatSslContextFactory .getServerContext(tlsMode, System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/sChat.jks", System.getProperty("user.dir") + "/src/com/phei/netty/ssl/conf/twoway/sChat.jks") .createSSLEngine(); // engine = SecureChatSslContextFactory // .getServerContext( // tlsMode, // System.getProperty("user.dir") // + "/src/com/phei/netty/ssl/conf/client/sChat.jks", // System.getProperty("user.dir") // + "/src/com/phei/netty/ssl/conf/client/sChat.jks") // .createSSLEngine(); } else { System.err.println("ERROR : " + tlsMode); System.exit(-1); } engine.setUseClientMode(false); // Client auth if (SSLMODE.CSA.toString().equals(tlsMode)) engine.setNeedClientAuth(true); pipeline.addLast("ssl", new SslHandler(engine)); // On top of the SSL handler, add the text line codec. pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); // and then business logic. pipeline.addLast("handler", new SecureChatServerHandler()); }
From source file:brave.netty.http.TestHttpInitializer.java
License:Apache License
@Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("decoder", new HttpRequestDecoder()); p.addLast("encoder", new HttpResponseEncoder()); p.addLast("aggregator", new HttpObjectAggregator(1048576)); //add brave tracing p.addLast("braveResponse", NettyHttpTracing.create(httpTracing).channelOutboundHandler()); p.addLast("braveRequest", NettyHttpTracing.create(httpTracing).channelInboundHandler()); p.addLast("handler", new TestHttpHandler(httpTracing)); }
From source file:c5db.client.C5ConnectionInitializer.java
License:Apache License
@Override protected void initChannel(SocketChannel ch) throws Exception { decoder = new WebsocketProtostuffDecoder(handShaker); final ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("http-client", new HttpClientCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(C5Constants.MAX_RESPONSE_SIZE)); pipeline.addLast("websec-codec", new WebsocketProtostuffEncoder(handShaker)); pipeline.addLast("websocket-aggregator", new WebSocketFrameAggregator(C5Constants.MAX_RESPONSE_SIZE)); pipeline.addLast("message-codec", decoder); pipeline.addLast("message-handler", new FutureBasedMessageHandler()); }
From source file:c5db.control.ControlService.java
License:Apache License
private void startHttpRpc() { try {/*from w w w. ja v a 2s. c om*/ ServerBootstrap serverBootstrap = new ServerBootstrap(); ServerBootstrap serverBootstrap1 = serverBootstrap.group(acceptConnectionGroup, ioWorkerGroup) .channel(NioServerSocketChannel.class).option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.SO_BACKLOG, 100).childOption(ChannelOption.TCP_NODELAY, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // pipeline.addLast("logger", new LoggingHandler(LogLevel.DEBUG)); pipeline.addLast("http-server", new HttpServerCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(C5ServerConstants.MAX_CALL_SIZE)); pipeline.addLast("encode", new ServerHttpProtostuffEncoder()); pipeline.addLast("decode", new ServerHttpProtostuffDecoder()); pipeline.addLast("translate", new ServerDecodeCommandRequest()); pipeline.addLast("inc-messages", new MessageHandler()); } }); serverBootstrap.bind(modulePort).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { // yay listenChannel = future.channel(); notifyStarted(); } else { LOG.error("Unable to bind to port {}", modulePort); notifyFailed(future.cause()); } } }); } catch (Exception e) { notifyFailed(e); } }
From source file:c5db.control.SimpleControlClient.java
License:Apache License
private void createClient() { client.group(ioWorkerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_REUSEADDR, true) .option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() { @Override//from ww w . ja v a 2 s.c o m protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // pipeline.addLast("logger", new LoggingHandler(LogLevel.WARN)); pipeline.addLast("http-client", new HttpClientCodec()); pipeline.addLast("aggregator", new HttpObjectAggregator(C5ServerConstants.MAX_CALL_SIZE)); pipeline.addLast("encode", new ClientHttpProtostuffEncoder()); pipeline.addLast("decode", new ClientHttpProtostuffDecoder()); pipeline.addLast("translate", new ClientEncodeCommandRequest()); } }); }
From source file:c5db.discovery.BeaconService.java
License:Apache License
@Override protected void doStart() { eventLoopGroup.next().execute(() -> { bootstrap = new Bootstrap(); bootstrap.group(eventLoopGroup).channel(NioDatagramChannel.class) .option(ChannelOption.SO_BROADCAST, true).option(ChannelOption.SO_REUSEADDR, true) .handler(new ChannelInitializer<DatagramChannel>() { @Override/*w w w . j a va2 s . c om*/ protected void initChannel(DatagramChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast("protobufDecoder", new UdpProtostuffDecoder<>(Availability.getSchema(), false)); p.addLast("protobufEncoder", new UdpProtostuffEncoder<>(Availability.getSchema(), false)); p.addLast("beaconMessageHandler", new BeaconMessageHandler()); } }); // Wait, this is why we are in a new executor... //noinspection RedundantCast bootstrap.bind(discoveryPort).addListener((ChannelFutureListener) future -> { if (future.isSuccess()) { broadcastChannel = future.channel(); } else { LOG.error("Unable to bind! ", future.cause()); notifyFailed(future.cause()); } }); try { localIPs = getLocalIPs(); } catch (SocketException e) { LOG.error("SocketException:", e); notifyFailed(e); return; } fiber = fiberSupplier.getNewFiber(this::notifyFailed); fiber.start(); // Schedule fiber tasks and subscriptions. incomingMessages.subscribe(fiber, this::processWireMessage); nodeInfoRequests.subscribe(fiber, this::handleNodeInfoRequest); moduleInformationProvider.moduleChangeChannel().subscribe(fiber, this::updateCurrentModulePorts); if (localIPs.isEmpty()) { LOG.warn( "Found no IP addresses to broadcast to other nodes; as a result, only sending to loopback"); } fiber.scheduleAtFixedRate(this::sendBeacon, BEACON_SERVICE_INITIAL_BROADCAST_DELAY_MILLISECONDS, BEACON_SERVICE_BROADCAST_PERIOD_MILLISECONDS, TimeUnit.MILLISECONDS); C5Futures.addCallback(moduleInformationProvider.getOnlineModules(), (ImmutableMap<ModuleType, Integer> onlineModuleToPortMap) -> { updateCurrentModulePorts(onlineModuleToPortMap); notifyStarted(); }, this::notifyFailed, fiber); }); }