List of usage examples for io.netty.channel ChannelInitializer ChannelInitializer
ChannelInitializer
From source file:com.kingmed.dp.lisclient.demo.DiscardServer.java
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from w w w . ja v a2 s. com*/ 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).option(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.kixeye.kixmpp.p2p.node.NodeClient.java
License:Apache License
public void initialize(final String host, int port, final EventLoopGroup workerGroup, final MessageRegistry messageRegistry, final ChannelInboundHandlerAdapter channelListener) throws InterruptedException { // prepare connection Bootstrap boot = new Bootstrap(); boot.group(workerGroup);//from ww w.j a va 2 s .c o m boot.channel(NioSocketChannel.class); boot.option(ChannelOption.SO_KEEPALIVE, true); boot.option(ChannelOption.TCP_NODELAY, true); boot.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); //p.addLast(new LoggingHandler()); // encoders p.addLast(new LengthFieldPrepender(4)); p.addLast(new ProtostuffEncoder(messageRegistry)); // decoders p.addLast(new LengthFieldBasedFrameDecoder(0x100000, 0, 4, 0, 4)); p.addLast(new ProtostuffDecoder(messageRegistry)); p.addLast(channelListener); } }); // connect channel = boot.connect(host, port).sync().channel(); }
From source file:com.kixeye.kixmpp.p2p.node.NodeServer.java
License:Apache License
public void initialize(final String host, final int port, final EventLoopGroup bossGroup, final EventLoopGroup workerGroup, final MessageRegistry messageRegistry, final ChannelInboundHandlerAdapter channelListener) { ServerBootstrap boot = new ServerBootstrap(); boot.group(bossGroup, workerGroup);/*from w ww.j a v a 2 s .c o m*/ boot.channel(NioServerSocketChannel.class); boot.option(ChannelOption.SO_BACKLOG, 32); boot.childOption(ChannelOption.SO_KEEPALIVE, true); boot.childOption(ChannelOption.TCP_NODELAY, true); boot.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); //p.addLast(new LoggingHandler()); // encoders p.addLast(new LengthFieldPrepender(4)); p.addLast(new ProtostuffEncoder(messageRegistry)); // decoders p.addLast(new LengthFieldBasedFrameDecoder(0x100000, 0, 4, 0, 4)); p.addLast(new ProtostuffDecoder(messageRegistry)); p.addLast(channelListener); } }); // start accepting connection try { logger.info("Starting NodeServer on [{}]...", port); if (host == null) { acceptChannel = boot.bind(port).sync().channel(); } else { acceptChannel = boot.bind(host, port).sync().channel(); } logger.info("NodeServer listening on [{}]...", port); } catch (InterruptedException e) { logger.error("Binding to port {} failed", port, e); } }
From source file:com.kixeye.kixmpp.server.KixmppServer.java
License:Apache License
/** * Creates a new {@link KixmppServer} with the given ssl engine. * // w w w .j a v a2s .co m * @param bindAddress * @param domain */ public KixmppServer(InetSocketAddress bindAddress, String domain, InetSocketAddress clusterAddress, NodeDiscovery clusterDiscovery, boolean useEpollIfAvailable) { if (useEpollIfAvailable && OS.indexOf("nux") >= 0) { this.bootstrap = new ServerBootstrap().group(new EpollEventLoopGroup(), new EpollEventLoopGroup()) .channel(EpollServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new KixmppCodec()); ch.pipeline().addLast(new KixmppServerMessageHandler()); } }); } else { this.bootstrap = new ServerBootstrap().group(new NioEventLoopGroup(), new NioEventLoopGroup()) .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new KixmppCodec()); ch.pipeline().addLast(new KixmppServerMessageHandler()); } }); } this.cluster = new ClusterClient(this, clusterAddress.getHostName(), clusterAddress.getPort(), clusterDiscovery, 300000, bootstrap.group()); this.cluster.getMessageRegistry().addCustomMessage(1, RoomBroadcastTask.class); this.cluster.getMessageRegistry().addCustomMessage(2, RoomPresenceBroadcastTask.class); this.cluster.getMessageRegistry().addCustomMessage(3, PrivateChatTask.class); this.cluster.getMessageRegistry().addCustomMessage(4, GetMucRoomNicknamesRequest.class); this.cluster.getMessageRegistry().addCustomMessage(5, GetMucRoomNicknamesResponse.class); this.mapReduce = new MapReduceTracker(this, bootstrap.group()); this.channels = new DefaultChannelGroup("All Channels", GlobalEventExecutor.INSTANCE); this.bindAddress = bindAddress; this.domain = domain.toLowerCase(); this.eventEngine = new KixmppEventEngine(); this.modulesToRegister.add(FeaturesKixmppServerModule.class.getName()); this.modulesToRegister.add(SaslKixmppServerModule.class.getName()); this.modulesToRegister.add(BindKixmppServerModule.class.getName()); this.modulesToRegister.add(SessionKixmppServerModule.class.getName()); this.modulesToRegister.add(PresenceKixmppServerModule.class.getName()); this.modulesToRegister.add(MucKixmppServerModule.class.getName()); this.modulesToRegister.add(RosterKixmppServerModule.class.getName()); this.modulesToRegister.add(DiscoKixmppServerModule.class.getName()); this.modulesToRegister.add(ChatKixmppServerModule.class.getName()); }
From source file:com.kixeye.kixmpp.server.KixmppServer.java
License:Apache License
/** * Enables the WebSocket port.//from w w w.ja v a 2s.co m * * @param webSocketAddress */ public KixmppServer enableWebSocket(InetSocketAddress webSocketAddress) { if (state.get() != State.STOPPED) { throw new IllegalStateException( String.format("The current state is [%s] but must be [STOPPED]", state.get())); } this.webSocketAddress = webSocketAddress; if (this.bootstrap.group() instanceof EpollEventLoopGroup && this.bootstrap.childGroup() instanceof EpollEventLoopGroup) { this.webSocketBootstrap = new ServerBootstrap() .group(this.bootstrap.group(), this.bootstrap.childGroup()) .channel(EpollServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HttpServerCodec()); ch.pipeline().addLast(new HttpObjectAggregator(65536)); ch.pipeline().addLast(new WebSocketServerHandler()); ch.pipeline().addLast(new KixmppWebSocketCodec()); ch.pipeline().addLast(new KixmppServerMessageHandler()); } }); } else { this.webSocketBootstrap = new ServerBootstrap() .group(this.bootstrap.group(), this.bootstrap.childGroup()) .channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new HttpServerCodec()); ch.pipeline().addLast(new HttpObjectAggregator(65536)); ch.pipeline().addLast(new WebSocketServerHandler()); ch.pipeline().addLast(new KixmppWebSocketCodec()); ch.pipeline().addLast(new KixmppServerMessageHandler()); } }); } return this; }
From source file:com.kradac.karview.netty.EchoServer.java
public void run() throws Exception { // Configure the server. EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* w ww . jav a 2 s .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 { ch.pipeline().addLast(new ReadTimeoutHandler(timeout * 60)); ch.pipeline().addLast(new MyDecoder()); ch.pipeline().addLast(new MyEncoder()); ch.pipeline().addLast( // new LoggingHandler(LogLevel.INFO), new EchoServerHandler(t)); } }); // Start the server. 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.lambdaworks.redis.server.MockTcpServer.java
License:Apache License
public void initialize(int port) throws InterruptedException { bossGroup = Resources.bossGroup; workerGroup = Resources.workerGroup; ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/* w w w .j a va 2 s .c om*/ public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // p.addLast(new LoggingHandler(LogLevel.INFO)); for (Supplier<? extends ChannelHandler> handler : handlers) { p.addLast(handler.get()); } } }); // Start the server. ChannelFuture f = b.bind(port).sync(); channel = f.channel(); }
From source file:com.lambdaworks.redis.server.RandomResponseServer.java
License:Apache License
public void initialize(int port) throws InterruptedException { bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100) .childHandler(new ChannelInitializer<SocketChannel>() { @Override//ww w .j a v a 2 s. c o m public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); // p.addLast(new LoggingHandler(LogLevel.INFO)); p.addLast(new RandomServerHandler()); } }); // Start the server. ChannelFuture f = b.bind(port).sync(); channel = f.channel(); }
From source file:com.lampard.netty4.protocol.http.fileServer.HttpFileServer.java
License:Apache License
public void run(final int port, final String url) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from ww w. j a v a2 s . co 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("http-decoder", new HttpRequestDecoder()); // ?? ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));// ???requestresponse ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());//?? ch.pipeline().addLast("http-chunked", new ChunkedWriteHandler());//? ch.pipeline().addLast("fileServerHandler", new HttpFileServerHandler(url));// } }); ChannelFuture future = b.bind("192.168.1.102", port).sync(); System.out.println( "HTTP??? : " + "http://192.168.1.102:" + port + url); future.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.lampard.netty4.protocol.netty.client.NettyClient.java
License:Apache License
public void connect(int port, String host) throws Exception { // ?NIO/*from w ww . j ava2s. c om*/ 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 { ch.pipeline().addLast(new NettyMessageDecoder(1024 * 1024, 4, 4)); ch.pipeline().addLast("MessageEncoder", new NettyMessageEncoder()); ch.pipeline().addLast("readTimeoutHandler", new ReadTimeoutHandler(50)); ch.pipeline().addLast("LoginAuthHandler", new LoginAuthReqHandler()); ch.pipeline().addLast("HeartBeatHandler", new HeartBeatReqHandler()); } }); // ?? ChannelFuture future = b.connect(new InetSocketAddress(host, port), new InetSocketAddress(NettyConstant.LOCALIP, NettyConstant.LOCAL_PORT)).sync(); // channelchannel // Returns the ChannelFuture which will be notified when this channel is closed. This method always returns the same future instance. future.channel().closeFuture().sync(); } finally { // ???????? executor.execute(new Runnable() { @Override public void run() { try { TimeUnit.SECONDS.sleep(1); try { connect(NettyConstant.PORT, NettyConstant.REMOTEIP);// ??? } catch (Exception e) { e.printStackTrace(); } } catch (InterruptedException e) { e.printStackTrace(); } } }); } }