List of usage examples for io.netty.bootstrap ServerBootstrap childHandler
ChannelHandler childHandler
To view the source code for io.netty.bootstrap ServerBootstrap childHandler.
Click Source Link
From source file:io.vertx.test.core.Http2ClientTest.java
License:Open Source License
private ServerBootstrap createH2CServer( BiFunction<Http2ConnectionDecoder, Http2ConnectionEncoder, Http2FrameListener> handler, boolean upgrade) { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.channel(NioServerSocketChannel.class); bootstrap.group(new NioEventLoopGroup()); bootstrap.childHandler(new ChannelInitializer<Channel>() { @Override//from ww w . j a va 2 s . c om protected void initChannel(Channel ch) throws Exception { if (upgrade) { HttpServerCodec sourceCodec = new HttpServerCodec(); HttpServerUpgradeHandler.UpgradeCodecFactory upgradeCodecFactory = protocol -> { if (AsciiString.contentEquals(Http2CodecUtil.HTTP_UPGRADE_PROTOCOL_NAME, protocol)) { return new Http2ServerUpgradeCodec(createHttpConnectionHandler(handler)); } else { return null; } }; ch.pipeline().addLast(sourceCodec); ch.pipeline().addLast(new HttpServerUpgradeHandler(sourceCodec, upgradeCodecFactory)); } else { Http2ConnectionHandler clientHandler = createHttpConnectionHandler(handler); ch.pipeline().addLast("handler", clientHandler); } } }); return bootstrap; }
From source file:me.bigteddy98.mcproxy.Main.java
License:Open Source License
public void run() throws Exception { ProxyLogger.info("Starting " + NAME + " version " + VERSION + " developed by " + AUTHOR + "!"); ProxyLogger.info(//from ww w. j a v a2 s.c o m "Starting server process using commandline " + Arrays.asList(processBuilder).toString() + "..."); ProcessBuilder builder = new ProcessBuilder(processBuilder); builder.redirectErrorStream(true); this.serverProcess = builder.start(); this.processPrintWriter = new PrintWriter(this.serverProcess.getOutputStream()); ProxyLogger.info("Server process started."); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { serverProcess.destroy(); } })); new Thread(new Runnable() { @Override public void run() { try (InputStream r = serverProcess.getInputStream()) { StringBuilder tmp = new StringBuilder(); byte[] consoleOutput = new byte[1024]; int read; while ((read = r.read(consoleOutput)) != -1) { String consoleLog = new String(consoleOutput, 0, read); String[] c = consoleLog.split("\n", -1); if (c.length != 0) { if (c.length == 1) { tmp.append(c[0]); } else { for (int i = 0; i < c.length - 1; i++) { tmp.append(c[i]); ProxyLogger.info(tmp.toString()); tmp.setLength(0); } tmp.append(c[c.length - 1]); } } } } catch (IOException e) { e.printStackTrace(); } ProxyLogger.warn("Server thread ended!"); System.exit(0); } }, "Server Output Reader").start(); new Thread(new Runnable() { @Override public void run() { try (Scanner in = new Scanner(System.in)) { while (in.hasNextLine()) { String newLine = in.nextLine(); executeCommand(newLine); } } ProxyLogger.warn("COMMAND LOOP ENDED, this shouldn't happen!"); } }, "CommandReader").start(); final ThreadGroup nettyListeners = new ThreadGroup(Thread.currentThread().getThreadGroup(), "Netty Listeners"); new Thread(nettyListeners, new Runnable() { @Override public void run() { ProxyLogger.info("Started Netty Server at port " + fromPort + "..."); final ThreadGroup group = new ThreadGroup(nettyListeners, "Listener-" + toPort); EventLoopGroup bossGroup = new NioEventLoopGroup(MAX_NETTY_BOSS_THREADS, new ThreadFactory() { private int threadCount = 0; private String newName = group.getName() + "\\boss"; @Override public Thread newThread(Runnable r) { Thread t = new Thread(group, r, newName + "\\" + threadCount++); t.setPriority(Thread.NORM_PRIORITY - 1); t.setDaemon(true); return t; } }); EventLoopGroup workerGroup = new NioEventLoopGroup(MAX_NETTY_WORKER_THREADS, new ThreadFactory() { private int threadCount = 0; private String newName = group.getName() + "\\worker"; @Override public Thread newThread(Runnable r) { Thread t = new Thread(group, r, newName + "\\" + threadCount++); t.setPriority(Thread.NORM_PRIORITY - 1); t.setDaemon(true); return t; } }); try { ServerBootstrap bootstrab = new ServerBootstrap(); bootstrab.group(bossGroup, workerGroup); bootstrab.channel(NioServerSocketChannel.class); bootstrab.childHandler(new ClientboundConnectionInitializer("localhost", toPort)); bootstrab.childOption(ChannelOption.AUTO_READ, false); bootstrab.bind(fromPort).sync().channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }).start(); }
From source file:me.bigteddy98.movingmotd.Main.java
License:Open Source License
@Override public void run() { nettyThreadGroup = new ThreadGroup(Thread.currentThread().getThreadGroup(), "NettyThreadGroup"); new Thread(this.nettyThreadGroup, new Runnable() { @Override/* ww w . j a v a 2 s .c o m*/ public void run() { final ThreadGroup group = new ThreadGroup(Main.this.nettyThreadGroup, "PortListener"); EventLoopGroup bossGroup = new NioEventLoopGroup(MAX_NETTY_BOSS_THREADS, new ThreadFactory() { private int counter = 0; private String newName = group.getName() + "\\nettyboss"; @Override public Thread newThread(Runnable r) { Thread t = new Thread(group, r, newName + "\\" + counter++); t.setPriority(Thread.NORM_PRIORITY - 1); t.setDaemon(true); return t; } }); EventLoopGroup workerGroup = new NioEventLoopGroup(MAX_NETTY_WORKER_THREADS, new ThreadFactory() { private int counter = 0; private String newName = group.getName() + "\\nettyworker"; @Override public Thread newThread(Runnable r) { Thread t = new Thread(group, r, newName + "\\" + counter++); t.setPriority(Thread.NORM_PRIORITY - 1); t.setDaemon(true); return t; } }); try { ServerBootstrap bootstrab = new ServerBootstrap(); bootstrab.group(bossGroup, workerGroup); bootstrab.channel(NioServerSocketChannel.class); bootstrab.childHandler(new ClientSideConnectionInitialization("localhost", toPort)); bootstrab.childOption(ChannelOption.AUTO_READ, false); bootstrab.bind(fromPort).sync().channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }).start(); }
From source file:me.bigteddy98.slimeportal.Main.java
License:Open Source License
public void run() throws Exception { SlimeLogger.info("Starting " + NAME + " version " + VERSION + " developed by " + AUTHOR + "!"); SlimeLogger.info(/* w ww. j av a2s .co m*/ "Starting server process using commandline " + Arrays.asList(processBuilder).toString() + "..."); ProcessBuilder builder = new ProcessBuilder(processBuilder); builder.redirectErrorStream(true); this.serverProcess = builder.start(); this.processPrintWriter = new PrintWriter(this.serverProcess.getOutputStream()); SlimeLogger.info("Server process started."); Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { @Override public void run() { serverProcess.destroy(); } })); new Thread(new Runnable() { @Override public void run() { try (InputStream r = serverProcess.getInputStream()) { StringBuilder tmp = new StringBuilder(); byte[] consoleOutput = new byte[1024]; int read; while ((read = r.read(consoleOutput)) != -1) { String consoleLog = new String(consoleOutput, 0, read); String[] c = consoleLog.split("\n", -1); if (c.length != 0) { if (c.length == 1) { tmp.append(c[0]); } else { for (int i = 0; i < c.length - 1; i++) { tmp.append(c[i]); SlimeLogger.info(tmp.toString()); tmp.setLength(0); } tmp.append(c[c.length - 1]); } } } } catch (IOException e) { e.printStackTrace(); } SlimeLogger.warn("Server thread ended!"); System.exit(0); } }, "Server Output Reader").start(); new Thread(new Runnable() { @Override public void run() { try (Scanner in = new Scanner(System.in)) { while (in.hasNextLine()) { String newLine = in.nextLine(); executeCommand(newLine); } } SlimeLogger.warn("COMMAND LOOP ENDED, this shouldn't happen!"); } }, "CommandReader").start(); final ThreadGroup nettyListeners = new ThreadGroup(Thread.currentThread().getThreadGroup(), "Netty Listeners"); new Thread(nettyListeners, new Runnable() { @Override public void run() { SlimeLogger.info("Started Netty Server at port " + fromPort + "..."); final ThreadGroup group = new ThreadGroup(nettyListeners, "Listener-" + toPort); EventLoopGroup bossGroup = new NioEventLoopGroup(MAX_NETTY_BOSS_THREADS, new ThreadFactory() { private int threadCount = 0; private String newName = group.getName() + "\\boss"; @Override public Thread newThread(Runnable r) { Thread t = new Thread(group, r, newName + "\\" + threadCount++); t.setPriority(Thread.NORM_PRIORITY - 1); t.setDaemon(true); return t; } }); EventLoopGroup workerGroup = new NioEventLoopGroup(MAX_NETTY_WORKER_THREADS, new ThreadFactory() { private int threadCount = 0; private String newName = group.getName() + "\\worker"; @Override public Thread newThread(Runnable r) { Thread t = new Thread(group, r, newName + "\\" + threadCount++); t.setPriority(Thread.NORM_PRIORITY - 1); t.setDaemon(true); return t; } }); try { ServerBootstrap bootstrab = new ServerBootstrap(); bootstrab.group(bossGroup, workerGroup); bootstrab.channel(NioServerSocketChannel.class); bootstrab.childHandler(new ClientboundConnectionInitializer("localhost", toPort)); bootstrab.childOption(ChannelOption.AUTO_READ, false); bootstrab.bind(fromPort).sync().channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }).start(); }
From source file:me.ferrybig.p2pnetwork.Main.java
private void startIncomingConnectionThread(int port) { ServerBootstrap server = new ServerBootstrap(); server.group(group);/* w ww. ja v a 2 s .co m*/ server.channel(NioServerSocketChannel.class); server.option(ChannelOption.SO_BACKLOG, 128); server.childOption(ChannelOption.SO_KEEPALIVE, true); server.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { if (blocked.containsKey(((InetSocketAddress) ch.remoteAddress()).getAddress())) { LOG.info(ch + "Rejected at socket level"); ch.close(); return; } ch.pipeline().addLast(new LengthFieldPrepender(4)); ch.pipeline().addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4)); ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO)); ch.pipeline().addLast(new PacketEncoder()); ch.pipeline().addLast(new PacketDecoder()); ch.pipeline().addLast(new LoggingHandler(LogLevel.INFO)); ch.pipeline().addLast(new ServerBootstrapConnector(addr, incomingListener)); clientsIn.add(ch); ch.closeFuture().addListener(e1 -> { clientsIn.remove(ch); }); } }); ChannelFuture f = server.bind(port); f.addListener(e -> { this.servers.add((ServerChannel) f.channel()); f.channel().closeFuture().addListener(e1 -> { this.servers.remove((ServerChannel) f.channel()); }); }); }
From source file:me.ferrybig.p2pnetwork.Peer.java
public ChannelFuture startIncomingConnectionThread(int port) { ServerBootstrap server = new ServerBootstrap(); server.group(group);//from w w w .j a va 2 s.c o m server.channel(NioServerSocketChannel.class); server.option(ChannelOption.SO_BACKLOG, 128); server.childOption(ChannelOption.SO_KEEPALIVE, true); server.childHandler(new ChannelConstructor(incomingListener, clientsIn)); ChannelFuture f = server.bind(port); return f.addListener(e -> { if (e.isSuccess()) { this.servers.add((ServerChannel) f.channel()); f.channel().closeFuture().addListener(e1 -> { this.servers.remove((ServerChannel) f.channel()); }); } }); }
From source file:me.jtalk.socketconnector.io.TCPManager.java
License:Open Source License
private ServerBootstrap instantiateServer(TCPActivationSpec spec) throws ResourceException { ServerBootstrap newServer = new ServerBootstrap(); newServer.group(this.listeners, this.workers); newServer.channel(NioServerSocketChannel.class); newServer.childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from w w w .j a v a 2s . co m*/ protected void initChannel(SocketChannel c) throws Exception { c.pipeline().addLast(new Receiver(TCPManager.this, TCPManager.this.ids.incrementAndGet())) .addLast(new Sender()); } }); newServer.option(ChannelOption.SO_KEEPALIVE, true); newServer.option(ChannelOption.SO_BACKLOG, spec.getBacklog()); newServer.option(ChannelOption.TCP_NODELAY, true); return newServer; }
From source file:me.smoe.adar.netty.EchoNetty5.java
License:Apache License
public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap = bootstrap.group(bossGroup, workerGroup); bootstrap = bootstrap.channel(NioServerSocketChannel.class); bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override/*from w w w . j a va2 s . c o m*/ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new EchoServerHandler()); } }); bootstrap.option(ChannelOption.SO_BACKLOG, 200); ChannelFuture future = bootstrap.bind(10080).sync(); future.channel().closeFuture().sync(); }
From source file:net.anyflow.lannister.server.MqttServer.java
License:Apache License
private void executeBootstrap(ScheduledExecutor scheduledExecutor, int port, boolean useWebSocket, boolean useSsl) throws InterruptedException { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap = bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class); if (scheduledExecutor != null) { bootstrap.handler(scheduledExecutor); }/* w ww . j a va2 s .co m*/ bootstrap.childHandler(new MqttChannelInitializer(useWebSocket, useSsl)); bootstrap.bind(port).sync(); }
From source file:net.hasor.rsf.bootstrap.RsfBootstrap.java
License:Apache License
public RsfContext sync() throws Throwable { LoggerHelper.logInfo("initialize rsfBootstrap"); if (this.rsfStart == null) { LoggerHelper.logInfo("create RsfStart."); this.rsfStart = new InnerRsfStart(); }/*from ww w . ja v a 2 s . c om*/ if (this.settings == null) { this.settings = new DefaultRsfSettings(new StandardContextSettings(DEFAULT_RSF_CONFIG)); this.settings.refresh(); } if (this.settings.getXmlNode("hasor.rsfConfig") == null) { throw new IOException("settings is not load."); } // //RsfContext LoggerHelper.logInfo("agent shutdown method on DefaultRsfContext.", DEFAULT_RSF_CONFIG); final DefaultRsfContext rsfContext = new DefaultRsfContext(this.settings) { public void shutdown() { LoggerHelper.logInfo("shutdown rsf."); super.shutdown(); doShutdown(); } }; if (this.workMode == WorkMode.Customer) { return doBinder(rsfContext); } // //localAddress & bindSocket InetAddress localAddress = this.localAddress; if (localAddress == null) { localAddress = finalBindAddress(rsfContext); } int bindSocket = (this.bindSocket < 1) ? this.settings.getBindPort() : this.bindSocket; LoggerHelper.logInfo("bind to address = %s , port = %s.", localAddress, bindSocket); //Netty final URL hostAddress = URLUtils.toURL(localAddress.getHostAddress(), bindSocket); final NioEventLoopGroup bossGroup = new NioEventLoopGroup(this.settings.getNetworkListener(), new NameThreadFactory("RSF-Listen-%s")); ServerBootstrap boot = new ServerBootstrap(); boot.group(bossGroup, rsfContext.getLoopGroup()); boot.channel(NioServerSocketChannel.class); boot.childHandler(new ChannelInitializer<SocketChannel>() { public void initChannel(SocketChannel ch) throws Exception { Channel channel = ch.pipeline().channel(); NetworkConnection.initConnection(hostAddress, channel); // ch.pipeline().addLast(new RSFCodec(), new RsfProviderHandler(rsfContext)); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture future = boot.bind(localAddress, bindSocket); final Channel serverChannel = future.channel(); LoggerHelper.logInfo("rsf Server started at :%s:%s", localAddress, bindSocket); //add this.shutdownHook = new Runnable() { public void run() { LoggerHelper.logInfo("shutdown rsf server."); bossGroup.shutdownGracefully(); serverChannel.close(); } }; // return doBinder(rsfContext); }