@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { if (LOG.isTraceEnabled()) { LOG.trace("Channel active {}", ctx.channel()); } final Channel channel = ctx.channel(); InetAddress addr = ((InetSocketAddress) channel.remoteAddress()) .getAddress(); if (maxClientCnxns > 0 && getClientCnxnCount(addr) >= maxClientCnxns) { ServerMetrics.CONNECTION_REJECTED.add(1); LOG.warn("Too many connections from {} - max is {}", addr, maxClientCnxns); channel.close(); return; } NettyServerCnxn cnxn = new NettyServerCnxn(channel, zkServer, NettyServerCnxnFactory.this); ctx.channel().attr(CONNECTION_ATTRIBUTE).set(cnxn); if (secure) { SslHandler sslHandler = ctx.pipeline().get(SslHandler.class); Future<Channel> handshakeFuture = sslHandler.handshakeFuture(); handshakeFuture.addListener(new CertificateVerifier(sslHandler, cnxn)); } else { allChannels.add(ctx.channel()); addCnxn(cnxn); } }
@Override public void stop() { final Channel channel = channelReference.get(); if (channel != null) { channel.close(); channel.closeFuture().syncUninterruptibly(); } childChannels.close().syncUninterruptibly(); if (childEventLoopGroup != null) { childEventLoopGroup.shutdownGracefully(); } bootstrap = null; }
@Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt == WebSocketServerProtocolHandler .ServerHandshakeStateEvent.HANDSHAKE_COMPLETE) { ctx.pipeline().remove(HttpRequestHandler.class); group.writeAndFlush(new TextWebSocketFrame( "Client " + ctx.channel() + " joined")); group.add(ctx.channel()); } else { super.userEventTriggered(ctx, evt); } }
ServerBootstrap server = new ServerBootstrap(); server.group(acceptorGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override Channel channel = server.bind(new InetSocketAddress(httpIngestHost, httpIngestPort)).sync().channel(); allOpenChannels.add(channel);
/** * Start proxy server * */ public void start() throws InterruptedException { ServerBootstrap serverBootstrap = new ServerBootstrap(); serverBootstrap.group(_acceptorGroup, _upstreamWorkerGroup); serverBootstrap.channelFactory(new ChannelFactory<ServerChannel>() { @Override public ServerChannel newChannel() { return new NioServerSocketChannel(); } }); serverBootstrap.childHandler(new ProxyInitializer(this)); //bind ChannelFuture future = serverBootstrap.bind(_host, _port); //wait for the future future.awaitUninterruptibly(); if (!future.isSuccess()) { future.channel().closeFuture().awaitUninterruptibly(); throw new ChannelException(String.format("Failed to bind to: %s:%d", _host, _port), future.cause()); } else { _allChannels.add(future.channel()); } }
if (shared == null || port == 0) { serverChannelGroup = new DefaultChannelGroup("vertx-acceptor-channels", GlobalEventExecutor.INSTANCE); ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.group(vertx.getAcceptorEventLoopGroup(), availableWorkers); applyConnectionOptions(address.path() != null, bootstrap); sslHelper.validate(vertx); bootstrap.childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { } else { Channel serverChannel = res.result(); if (serverChannel.localAddress() instanceof InetSocketAddress) { HttpServerImpl.this.actualPort = ((InetSocketAddress)serverChannel.localAddress()).getPort(); } else { HttpServerImpl.this.actualPort = address.port(); serverChannelGroup.add(serverChannel); VertxMetrics metrics = vertx.metricsSPI(); this.metrics = metrics != null ? metrics.createHttpServerMetrics(options, address) : null;
@Override public void operationComplete(ChannelFuture future) throws Exception { writer.decrementAndGet(); channel = future.channel(); if (isClosed()) { channel .close() .addListener( new ChannelFutureListener() { @Override Connection.this.factory.allChannels.add(channel); if (!future.isSuccess()) { if (logger.isDebugEnabled()) logger.debug( logger.debug( "{} Connection established, initializing transport", Connection.this); channel.closeFuture().addListener(new ChannelCloseListener()); channelReadyFuture.set(null);
/** * Start WebImageViewer. * @param fsimage the fsimage to load. * @throws IOException if fail to load the fsimage. */ @VisibleForTesting public void initServer(String fsimage) throws IOException, InterruptedException { final FSImageLoader loader = FSImageLoader.load(fsimage); bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpRequestDecoder(), new StringEncoder(), new HttpResponseEncoder(), new FSImageHandler(loader, allChannels)); } }); channel = bootstrap.bind(address).sync().channel(); allChannels.add(channel); address = (InetSocketAddress) channel.localAddress(); LOG.info("WebImageViewer started. Listening on " + address.toString() + ". Press Ctrl+C to stop the viewer."); }
private void startServerChannels() { String[] hosts = TransportConfiguration.splitHosts(host); for (String h : hosts) { SocketAddress address; if (useInvm) { address = new LocalAddress(h); } else { address = new InetSocketAddress(h, port); } Channel serverChannel = bootstrap.bind(address).syncUninterruptibly().channel(); serverChannelGroup.add(serverChannel); } }
@Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { final Channel channel = future.channel(); channels.add(channel); LOG.debug("Started channel {}", channel); final DatagramChannelConfig channelConfig = (DatagramChannelConfig) channel.config(); final int receiveBufferSize = channelConfig.getReceiveBufferSize(); if (receiveBufferSize != expectedRecvBufferSize) { LOG.warn("receiveBufferSize (SO_RCVBUF) for input {} (channel {}) should be {} but is {}.", input, channel, expectedRecvBufferSize, receiveBufferSize); } } else { LOG.warn("Failed to start channel for input {}", input, future.cause()); } } }
/** * Starts this mock server and listens for traffic on the given port. * * @param port the port to which this server should bind * * @return a {@code Future} that will succeed when the server has bound to the given port and is ready to accept * traffic */ public Future<Void> start(final int port) { final ChannelFuture channelFuture = this.bootstrap.bind(port); this.allChannels.add(channelFuture.channel()); return channelFuture; }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { channels.add(ctx.channel()); ctx.fireChannelActive(); }
public void start() throws Exception { InetSocketAddress endpoint; if (address == null) { endpoint = new InetSocketAddress(port); } else { endpoint = new InetSocketAddress(address, port); } Channel channel = bootstrap.bind(endpoint).sync().channel(); if (channel != null) { getChannelGroup().add(channel); } }
/** * Stop proxy server * */ public void stop() { ChannelGroupFuture future = _allChannels.close().awaitUninterruptibly(); if (!future.isSuccess()) { final Iterator<ChannelFuture> iter = future.iterator(); while (iter.hasNext()) { final ChannelFuture cf = iter.next(); if (!cf.isSuccess()) { LOG.warn(String.format("Failed to close channel %s because %s", cf.channel(), cf.cause())); } } } _acceptorGroup.shutdownGracefully(); _upstreamWorkerGroup.shutdownGracefully(); _downstreamWorkerGroup.shutdownGracefully(); }
final EventLoopGroup bossGroup = bootstrap.config().group(); final EventLoopGroup workerGroup = bootstrap.config().childGroup(); ChannelFuture parentCloseFuture = parentChannel.close(); if (bossGroup != null) { parentCloseFuture.addListener(future -> { bossGroup.shutdownGracefully(); }); ChannelGroupFuture allChannelsCloseFuture = allChannels.close(); if (workerGroup != null) { allChannelsCloseFuture.addListener(future -> { workerGroup.shutdownGracefully(); });
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { if (LOG.isTraceEnabled()) { LOG.trace("Channel inactive {}", ctx.channel()); } allChannels.remove(ctx.channel()); NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).getAndSet(null); if (cnxn != null) { if (LOG.isTraceEnabled()) { LOG.trace("Channel inactive caused close {}", cnxn); } cnxn.close(); } }