List of usage examples for io.netty.channel ChannelInitializer ChannelInitializer
ChannelInitializer
From source file:com.seed.nettyechoserver.EchoServer.java
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(1); try {/*w ww . ja va 2 s . co 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 LoggingHandler(LogLevel.INFO)); p.addLast(new EchoServerHandler()); } }); ChannelFuture f = b.bind(PORT).sync(); f.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.seventh_root.ld33.client.LD33Client.java
License:Apache License
public void connect(String address, int port) { EventLoopGroup group = new NioEventLoopGroup(); try {/*from w w w. j av a2 s. c om*/ Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel channel) throws Exception { channel.pipeline().addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), //new LD33ServerBoundPacketEncoder(), //new LD33ClientBoundPacketDecoder(LD33Client.this), new LD33ClientHandler(LD33Client.this)); } }); channel = bootstrap.connect(address, port).sync().channel(); start(); channel.closeFuture().sync(); } catch (InterruptedException exception) { getLogger().log(SEVERE, "Event loop group interrupted", exception); } finally { group.shutdownGracefully(); } }
From source file:com.seventh_root.ld33.server.LD33Server.java
License:Apache License
private void start() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w w w. j av a2 s . c o m ServerBootstrap bootstrap = new ServerBootstrap(); handler = new LD33ServerHandler(this); bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel channel) throws Exception { channel.pipeline().addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), //new LD33ClientBoundPacketEncoder(), //new LD33ServerBoundPacketDecoder(LD33Server.this), handler); } }); Channel channel = bootstrap.bind(getConfig().getInt("port", 37896)).sync().channel(); setRunning(true); long beforeTime, timeDiff, sleep; beforeTime = currentTimeMillis(); while (isRunning()) { doTick(); timeDiff = currentTimeMillis() - beforeTime; sleep = DELAY - timeDiff; if (sleep > 0) { try { Thread.sleep(sleep); } catch (InterruptedException exception) { getLogger().log(SEVERE, "Thread interrupted", exception); } } beforeTime = currentTimeMillis(); } channel.closeFuture().sync(); } catch (InterruptedException exception) { getLogger().log(SEVERE, "Event loop group interrupted", exception); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.sheldon.javaPrj.netty.DiscardServer.java
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from w ww . ja v a 2 s .c om 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).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.sheldon.javaPrj.netty.FileClient.java
public void start() { EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// www. j a v a 2 s .c o m Bootstrap b = new Bootstrap(); b.group(workerGroup).channel(NioSocketChannel.class).option(ChannelOption.SO_KEEPALIVE, true) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new ChunkedWriteHandler(), new FileClientHandler()); } }); ChannelFuture f = b.connect("localhost", port).sync(); f.channel().closeFuture().sync(); } catch (InterruptedException ex) { Logger.getLogger(FileClient.class.getName()).log(Level.SEVERE, null, ex); } catch (Exception e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); } }
From source file:com.sheldon.javaPrj.netty.FileServer.java
public void start() { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// w w w. j a va 2s. c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new FileServerHandler()); } }); ChannelFuture f = b.bind(port).sync(); System.out.println("server start"); f.channel().closeFuture().sync(); System.out.println("server down"); } catch (InterruptedException ex) { Logger.getLogger(FileServer.class.getName()).log(Level.SEVERE, null, ex); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.sheldon.javaPrj.netty.TimeClient.java
public static void main(String args[]) throws InterruptedException { String host = args.length > 0 ? args[0] : "localhost"; int port = Integer.parseInt(args.length > 1 ? args[1] : "8080"); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/* w w w. j ava 2 s .c om*/ Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new TimeClientHandler()); } }); ChannelFuture f = b.connect(host, port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); } }
From source file:com.sheldon.javaPrj.netty.TimeServer.java
public void run() throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//from www. ja v a 2 s .c o 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(new TimeServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture f = b.bind(port).sync(); f.channel().closeFuture().sync(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.sitech.crmpd.idmm2.ble.EchoClient.java
License:Apache License
public static void consumer(String[] args) throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {/*from w w w .j a v a2s. co 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 LoggingHandler(LogLevel.TRACE)); p.addLast(new FrameCodeC()); p.addLast(new ConsumerClientHandler(clientid, target_topic)); } }); // Start the client. ChannelFuture f = b.connect(ble_ip, ble_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.sitech.crmpd.idmm2.ble.EchoClient.java
License:Apache License
public static void producer(String[] args) throws Exception { // Configure the client. EventLoopGroup group = new NioEventLoopGroup(); try {//from w w w . j a va2s. co 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 LoggingHandler(LogLevel.TRACE)); p.addLast(new FrameCodeC()); p.addLast(new ProducerClientHandler(clientid, target_topic)); } }); // Start the client. ChannelFuture f = b.connect(ble_ip, ble_port).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); } finally { // Shut down the event loop to terminate all threads. group.shutdownGracefully(); } }