List of usage examples for io.netty.channel ChannelInitializer ChannelInitializer
ChannelInitializer
From source file:com.ning.http.client.providers.netty_4.NettyAsyncHttpProvider.java
License:Apache License
void constructSSLPipeline(final NettyConnectListener<?> cl) { secureBootstrap.handler(new ChannelInitializer() { /* @Override */ protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); try { pipeline.addLast(SSL_HANDLER, new SslHandler(createSSLEngine())); } catch (Throwable ex) { abort(cl.future(), ex);/*from ww w . j a va 2s . c o m*/ } pipeline.addLast(HTTP_HANDLER, newHttpClientCodec()); if (config.isCompressionEnabled()) { pipeline.addLast("inflater", new HttpContentDecompressor()); } pipeline.addLast("chunkedWriter", new ChunkedWriteHandler()); pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this); } }); secureWebSocketBootstrap.handler(new ChannelInitializer() { /* @Override */ protected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); try { pipeline.addLast(SSL_HANDLER, new SslHandler(createSSLEngine())); } catch (Throwable ex) { abort(cl.future(), ex); } pipeline.addLast("ws-decoder", new HttpResponseDecoder()); pipeline.addLast("ws-encoder", new HttpRequestEncoder()); pipeline.addLast("httpProcessor", NettyAsyncHttpProvider.this); } }); }
From source file:com.nus.mazegame.client.GameClient.java
public static void init(String host, final int port, final String type, SocketAddress localAddr) throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override/*from ww w .java 2s. com*/ protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // Decoders p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4)); p.addLast("bytesDecoder", new ByteArrayDecoder()); // Encoder p.addLast("frameEncoder", new LengthFieldPrepender(4)); p.addLast("bytesEncoder", new ByteArrayEncoder()); p.addLast(GameClientHandler.instance); GameInfo.instance.setType(type); GameInfo.instance.setHostPort(port); } }); // Make the connection attempt. SocketAddress address = new InetSocketAddress(host, port); ChannelFuture f; if (localAddr == null) f = b.connect(address).sync(); else f = b.connect(address, localAddr).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); }
From source file:com.nus.mazegame.server.GameServer.java
public static void init(int port, int x, int y, int treasure, boolean isSlave) throws Exception { GameService.gameService = new GameService(x, y, treasure, isSlave); // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w . java 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(); // Decoders p.addLast("frameDecoder", new LengthFieldBasedFrameDecoder(1048576, 0, 4, 0, 4)); p.addLast("bytesDecoder", new ByteArrayDecoder()); // Encoder p.addLast("frameEncoder", new LengthFieldPrepender(4)); p.addLast("bytesEncoder", new ByteArrayEncoder()); // p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new GameServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(port).sync(); Logger.getLogger(GameServerHandler.class.getName()).log(Level.INFO, "Server started..."); // 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.openddal.server.NettyServer.java
License:Apache License
private ServerBootstrap configServer() { bossGroup = new NioEventLoopGroup(args.bossThreads, new DefaultThreadFactory("NettyBossGroup", true)); workerGroup = new NioEventLoopGroup(args.workerThreads, new DefaultThreadFactory("NettyWorkerGroup", true)); userExecutor = createUserThreadExecutor(); Authenticator authenticator = createAuthenticator(); ProcessorFactory processorFactory = createProcessorFactory(); RequestFactory requestFactory = createRequestFactory(); ResponseFactory responseFactory = createResponseFactory(); final ProtocolHandler protocolHandler = new ProtocolHandler(authenticator, processorFactory, requestFactory, responseFactory, userExecutor); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childOption(ChannelOption.SO_REUSEADDR, true).childOption(ChannelOption.SO_KEEPALIVE, true) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childOption(ChannelOption.TCP_NODELAY, true); if (args.socketTimeoutMills > 0) { b.childOption(ChannelOption.SO_TIMEOUT, args.socketTimeoutMills); }/*from w w w . j av a 2s. co m*/ if (args.recvBuff > 0) { b.childOption(ChannelOption.SO_RCVBUF, args.recvBuff); } if (args.sendBuff > 0) { b.childOption(ChannelOption.SO_SNDBUF, args.sendBuff); } b.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(createProtocolDecoder(), /* createProtocolEncoder(), */ protocolHandler); } }); return b; }
From source file:com.opentable.logging.RedisServerRule.java
License:Apache License
@Override protected void before() throws Throwable { try (ServerSocket ss = new ServerSocket(0)) { port = ss.getLocalPort();//from w ww . j a va2 s .c om } // Only execute the command handler in a single thread final RedisCommandHandler commandHandler = new RedisCommandHandler(new SimpleRedisServer()); b = new ServerBootstrap(); group = new DefaultEventExecutorGroup(1); b.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).localAddress(port) .childOption(ChannelOption.TCP_NODELAY, true).childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // p.addLast(new ByteLoggingHandler(LogLevel.INFO)); p.addLast(new RedisCommandDecoder()); p.addLast(new RedisReplyEncoder()); p.addLast(group, commandHandler); } }); // Start the server. b.bind().sync(); }
From source file:com.palace.seeds.net.netty.EchoClient.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 av a 2 s . com 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 EchoClientHandler()); } }); // 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.papteco.client.netty.LoginClientBuilder.java
License:Apache License
public void validateUser() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {/* w w w. ja va2s . c o m*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ObjectEncoder(), new NewObjectDecoder(ClassResolvers.cacheDisabled(null)), new LoginClientHandler(username, password)); } }); b.connect(envsetting.getProperty("pims_ip"), PortTranslater(envsetting.getProperty("login_sym_port"))) .sync().channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:com.papteco.client.netty.ObjectEchoBuilder.java
License:Apache License
public void runInitinal() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {//from ww w .j a v a 2 s.com Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ObjectEncoder(), new NewObjectDecoder(ClassResolvers.cacheDisabled(null)), new InitinalClientHandler(username)); } }); b.connect(envsetting.getProperty("pims_ip"), PortTranslater(envsetting.getProperty("comm_nett_port"))) .sync().channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:com.papteco.client.netty.ObjectEchoBuilder.java
License:Apache License
public void runSelProjectEcho() throws InterruptedException { EventLoopGroup group = new NioEventLoopGroup(); try {/*from ww w .j a va 2s . c o m*/ Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ObjectEncoder(), new NewObjectDecoder(ClassResolvers.cacheDisabled(null)), new SelProjectClientHandler(inPrjCde, SharedBoard.LOGIN_USER)); } }); b.connect(envsetting.getProperty("pims_ip"), PortTranslater(envsetting.getProperty("comm_nett_port"))) .sync().channel().closeFuture().sync(); } finally { group.shutdownGracefully(); } }
From source file:com.papteco.client.netty.ObjectEchoBuilder.java
License:Apache License
public void runDownFileEcho() throws Exception { EventLoopGroup group = new NioEventLoopGroup(); try {//from ww w. j a v a 2 s . c om Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ObjectEncoder(), new NewObjectDecoder(ClassResolvers.cacheDisabled(null)), new DownFileClientHandler(qItem)); } }); b.connect(envsetting.getProperty("pims_ip"), PortTranslater(envsetting.getProperty("comm_nett_port"))) .sync().channel().closeFuture().sync(); } catch (Throwable e) { e.printStackTrace(); } finally { group.shutdownGracefully(); } }