use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project angel by Tencent.
the class MatrixTransportServer method start.
public void start() {
Configuration conf = context.getConf();
int workerNum = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_SERVER_EVENTGROUP_THREADNUM, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_SERVER_EVENTGROUP_THREADNUM);
int sendBuffSize = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_SERVER_SNDBUF, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_SERVER_SNDBUF);
int recvBuffSize = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_SERVER_RCVBUF, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_SERVER_RCVBUF);
final int maxMessageSize = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_MAX_MESSAGE_SIZE, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_MAX_MESSAGE_SIZE);
int ioRatio = conf.getInt(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_SERVER_IORATIO, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_SERVER_IORATIO);
String channelType = conf.get(AngelConf.ANGEL_NETTY_MATRIXTRANSFER_SERVER_CHANNEL_TYPE, AngelConf.DEFAULT_ANGEL_NETTY_MATRIXTRANSFER_SERVER_CHANNEL_TYPE);
// Use Epoll for linux
Class channelClass;
String os = System.getProperty("os.name");
if (os != null && os.toLowerCase().startsWith("linux") && channelType.equals("epoll")) {
LOG.info("Use epoll channel");
channelClass = EpollServerSocketChannel.class;
bossGroup = new EpollEventLoopGroup(1);
workerGroup = new EpollEventLoopGroup(workerNum);
((EpollEventLoopGroup) workerGroup).setIoRatio(ioRatio);
} else {
LOG.info("Use nio channel");
channelClass = NioServerSocketChannel.class;
bossGroup = new NioEventLoopGroup(1);
workerGroup = new NioEventLoopGroup(workerNum);
((NioEventLoopGroup) workerGroup).setIoRatio(70);
}
LOG.info("Server port = " + port);
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(channelClass).option(ChannelOption.SO_SNDBUF, sendBuffSize).option(ChannelOption.SO_RCVBUF, recvBuffSize).option(ChannelOption.SO_KEEPALIVE, true).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new LengthFieldBasedFrameDecoder(maxMessageSize, 0, 4, 0, 4));
p.addLast(new LengthFieldPrepender(4));
p.addLast(new MatrixTransportServerHandler(context));
}
});
channelFuture = b.bind(port);
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project rskj by rsksmart.
the class PeerServerImpl method start.
private void start(InetAddress host, int port) {
// TODO review listening use
listening = true;
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
EthereumChannelInitializer ethereumChannelInitializer = ethereumChannelInitializerFactory.newInstance("");
ethereumListener.trace("Listening on port " + port);
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.option(ChannelOption.SO_KEEPALIVE, true);
b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT);
b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout());
b.handler(new LoggingHandler());
b.childHandler(ethereumChannelInitializer);
// Start the client.
logger.info("Listening for incoming connections, host: {}, port: [{}] ", host, port);
logger.info("NodeId: [{}] ", ByteUtil.toHexString(config.nodeId()));
ChannelFuture f = b.bind(host, port).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
logger.debug("Connection is closed");
// TODO review listening use
listening = false;
} catch (Exception e) {
logger.debug("Exception: {} ({})", e.getMessage(), e.getClass().getName());
throw new Error("Server Disconnected");
} finally {
workerGroup.shutdownGracefully();
}
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project rskj by rsksmart.
the class Web3WebSocketServer method start.
@Override
public void start() {
logger.info("RPC WebSocket enabled");
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpServerCodec());
p.addLast(new HttpObjectAggregator(maxAggregatedFrameSize));
p.addLast(new WriteTimeoutHandler(serverWriteTimeoutSeconds, TimeUnit.SECONDS));
p.addLast(new RskWebSocketServerProtocolHandler("/websocket", maxFrameSize));
p.addLast(new WebSocketFrameAggregator(maxAggregatedFrameSize));
p.addLast(webSocketJsonRpcHandler);
p.addLast(web3ServerHandler);
p.addLast(new Web3ResultWebSocketResponseHandler());
}
});
webSocketChannel = b.bind(host, port);
try {
webSocketChannel.sync();
} catch (InterruptedException e) {
logger.error("The RPC WebSocket server couldn't be started", e);
Thread.currentThread().interrupt();
}
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project rskj by rsksmart.
the class Web3HttpServer method start.
@Override
public void start() {
logger.info("RPC HTTP enabled");
ServerBootstrap b = new ServerBootstrap();
b.option(ChannelOption.SO_LINGER, socketLinger);
b.option(ChannelOption.SO_REUSEADDR, reuseAddress);
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline p = ch.pipeline();
p.addLast(new HttpRequestDecoder());
p.addLast(new HttpResponseEncoder());
p.addLast(new HttpObjectAggregator(1024 * 1024 * 5));
p.addLast(new HttpContentCompressor());
if (corsConfiguration.hasHeader()) {
p.addLast(new CorsHandler(CorsConfig.withOrigin(corsConfiguration.getHeader()).allowedRequestHeaders(HttpHeaders.Names.CONTENT_TYPE).allowedRequestMethods(HttpMethod.POST).build()));
}
p.addLast(jsonRpcWeb3FilterHandler);
p.addLast(new Web3HttpMethodFilterHandler());
p.addLast(jsonRpcWeb3ServerHandler);
p.addLast(buildWeb3ResultHttpResponseHandler());
}
});
try {
b.bind(bindAddress, port).sync();
} catch (InterruptedException e) {
logger.error("The RPC HTTP server couldn't be started", e);
Thread.currentThread().interrupt();
}
}
use of org.apache.flink.shaded.netty4.io.netty.bootstrap.ServerBootstrap in project swift by luastar.
the class HttpServer method start.
public void start() {
// 设置线程名称
ThreadFactoryBuilder threadFactoryBuilder = new ThreadFactoryBuilder();
EventLoopGroup bossGroup = new NioEventLoopGroup(HttpConstant.SWIFT_BOSS_THREADS, threadFactoryBuilder.setNameFormat("boss-group-%d").build());
EventLoopGroup workerGroup = new NioEventLoopGroup(HttpConstant.SWIFT_WORKER_THREADS, threadFactoryBuilder.setNameFormat("worker-group-%d").build());
// EventExecutorGroup executorGroup = new DefaultEventExecutorGroup(HttpConstant.SWIFT_BUSINESS_THREADS, threadFactoryBuilder.setNameFormat("executor-group-%d").build());
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
if (ssl) {
SelfSignedCertificate ssc = new SelfSignedCertificate();
SslContext sslContext = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
pipeline.addLast(sslContext.newHandler(ch.alloc()));
}
// http request decode and response encode
pipeline.addLast(new HttpServerCodec());
// 将消息头和体聚合成FullHttpRequest和FullHttpResponse
pipeline.addLast(new HttpObjectAggregator(HttpConstant.SWIFT_MAX_CONTENT_LENGTH));
// 压缩处理
pipeline.addLast(new HttpContentCompressor(HttpConstant.SWIFT_COMPRESSION_LEVEL));
// 自定义http服务
// pipeline.addLast(executorGroup, "http-handler", new HttpChannelHandler(handlerMapping));
pipeline.addLast(new HttpChannelHandler(handlerMapping));
}
});
Channel channel = bootstrap.bind(port).sync().channel();
channel.closeFuture().sync();
} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
// executorGroup.shutdownGracefully();
HttpThreadPoolExecutor.shutdownGracefully();
}
}
Aggregations