public ChannelFuture connectAsync(String host, int port, String remoteId, boolean discoveryMode) { ethereumListener.trace("Connecting to: " + host + ":" + port); EthereumChannelInitializer ethereumChannelInitializer = ctx.getBean(EthereumChannelInitializer.class, remoteId); ethereumChannelInitializer.setPeerDiscoveryMode(discoveryMode); Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout()); b.remoteAddress(host, port); b.handler(ethereumChannelInitializer); // Start the client. return b.connect(); }
public ChannelFuture connectAsync(String host, int port, String remoteId, boolean discoveryMode) { ethereumListener.trace("Connecting to: " + host + ":" + port); EthereumChannelInitializer ethereumChannelInitializer = ethereumChannelInitializerFactory.newInstance(remoteId); ethereumChannelInitializer.setPeerDiscoveryMode(discoveryMode); Bootstrap b = new Bootstrap(); b.group(workerGroup); b.channel(NioSocketChannel.class); b.option(ChannelOption.SO_KEEPALIVE, true); b.option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT); b.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, config.peerConnectionTimeout()); b.remoteAddress(host, port); b.handler(ethereumChannelInitializer); // Start the client. return b.connect(); }
@Override public void initialise(NetworkChannelHandler channelHandler) { this.channelHandler = channelHandler; final boolean useEpoll = this.configuration.getBoolean("epoll") && Epoll.isAvailable(); EventLoopGroup acceptGroup = useEpoll ? new EpollEventLoopGroup(this.configuration.getInt("acceptGroup")) : new NioEventLoopGroup(this.configuration.getInt("acceptGroup")); EventLoopGroup ioGroup = useEpoll ? new EpollEventLoopGroup(this.configuration.getInt("ioGroup")) : new NioEventLoopGroup(this.configuration.getInt("ioGroup")); EventLoopGroup channelGroup = useEpoll ? new EpollEventLoopGroup(this.configuration.getInt("channelGroup")) : new NioEventLoopGroup(this.configuration.getInt("channelGroup")); this.serverBootstrap = new ServerBootstrap() .group(acceptGroup, ioGroup) .channel(useEpoll ? EpollServerSocketChannel.class : NioServerSocketChannel.class) .childHandler(new ChannelInitialiser(channelGroup, this.channelHandler, null)) .option(ChannelOption.SO_BACKLOG, this.configuration.getInt("backlog")) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.MESSAGE_SIZE_ESTIMATOR, DefaultMessageSizeEstimator.DEFAULT) .childOption(ChannelOption.TCP_NODELAY, this.configuration.getBoolean("tcpNoDelay")) .childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT); }
public void start(int port) { bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); ethereumChannelInitializer = ctx.getBean(EthereumChannelInitializer.class, ""); 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, port: [{}] ", port); logger.info("NodeId: [{}] ", Hex.toHexString(config.nodeId())); channelFuture = b.bind(port).sync(); listening = true; // Wait until the connection is closed. channelFuture.channel().closeFuture().sync(); logger.debug("Connection is closed"); } catch (Exception e) { logger.debug("Exception: {} ({})", e.getMessage(), e.getClass().getName()); throw new Error("Server Disconnected"); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); listening = false; } }
public void start(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, port: [{}] ", port); logger.info("NodeId: [{}] ", Hex.toHexString(config.nodeId())); ChannelFuture f = b.bind(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(); } }
public void start(int port) { inactivesCollector.scheduleAtFixedRate(new TimerTask() { public void run() { Iterator<Channel> iter = channels.iterator(); while(iter.hasNext()){ Channel channel = iter.next(); if(!channel.p2pHandler.isActive()){ iter.remove(); logger.info("Channel removed: {}", channel.p2pHandler.getHandshakeHelloMessage()); } } } }, 2000, 5000); EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); if (peerListener != null) peerListener.console("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(new EthereumChannelInitializer(this)); // Start the client. logger.info("Listening for incoming connections, port: [{}] ", port); ChannelFuture f = b.bind(port).sync(); // Wait until the connection is closed. f.channel().closeFuture().sync(); logger.debug("Connection is closed"); } catch (Exception e) { logger.debug("Exception: {} ({})", e.getMessage(), e.getClass().getName()); throw new Error("Server Disconnnected"); } finally { workerGroup.shutdownGracefully(); } }