private Bootstrap buildBootstrap(URI uri, boolean isSecure) { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(this.eventLoopGroup).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { configureChannel(channel.config()); ChannelPipeline pipeline = channel.pipeline(); if (isSecure) { Assert.notNull(sslContext, "sslContext should not be null"); pipeline.addLast(sslContext.newHandler(channel.alloc(), uri.getHost(), uri.getPort())); } pipeline.addLast(new HttpClientCodec()); pipeline.addLast(new HttpObjectAggregator(maxResponseSize)); if (readTimeout > 0) { pipeline.addLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS)); } } }); return bootstrap; }
public EventLoopGroup kqueueGroup() { EventLoopGroup nioEventLoopGroup = new KQueueEventLoopGroup(0, createThreadFactory()); bootstrap.group(nioEventLoopGroup).channel(KQueueDomainSocketChannel.class) .handler(new ChannelInitializer<KQueueDomainSocketChannel>() { @Override protected void initChannel(final KQueueDomainSocketChannel channel) throws Exception { channel.pipeline().addLast(new HttpClientCodec()); channel.pipeline().addLast(new HttpContentDecompressor()); } }); return nioEventLoopGroup; }
@Override public EventLoopGroup init(Bootstrap bootstrap, final DockerClientConfig dockerClientConfig) { EventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(0, createThreadFactory()); // TODO do we really need BouncyCastle? Security.addProvider(new BouncyCastleProvider()); ChannelFactory<NioSocketChannel> factory = () -> configure(new NioSocketChannel()); bootstrap.group(nioEventLoopGroup).channelFactory(factory) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(final SocketChannel channel) throws Exception { channel.pipeline().addLast(new HttpClientCodec()); channel.pipeline().addLast(new HttpContentDecompressor()); } }); return nioEventLoopGroup; }
AtomicReference<Thread> channelThread = new AtomicReference<>(); CountDownLatch connectLatch = new CountDownLatch(1); Bootstrap bootstrap = new Bootstrap(); bootstrap.channelFactory(((VertxInternal)vertx).transport().channelFactory(false)); bootstrap.group(vertx.nettyEventLoopGroup()); bootstrap.resolver(((VertxInternal) vertx).nettyAddressResolverGroup()); bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelFuture channelFut = bootstrap.connect("localhost", 1234); awaitLatch(connectLatch); channelFut.addListener(v -> {
public Connection(final URI uri, final ConnectionPool pool, final int maxInProcess) throws ConnectionException { this.uri = uri; this.cluster = pool.getCluster(); this.client = pool.getClient(); this.pool = pool; this.maxInProcess = maxInProcess; this.keepAliveInterval = pool.settings().keepAliveInterval; connectionLabel = String.format("Connection{host=%s}", pool.host); if (cluster.isClosing()) throw new IllegalStateException("Cannot open a connection with the cluster after close() is called"); final Bootstrap b = this.cluster.getFactory().createBootstrap(); try { if (channelizerClass.get() == null) { channelizerClass.compareAndSet(null, (Class<Channelizer>) Class.forName(cluster.connectionPoolSettings().channelizer)); } channelizer = channelizerClass.get().newInstance(); channelizer.init(this); b.channel(NioSocketChannel.class).handler(channelizer); channel = b.connect(uri.getHost(), uri.getPort()).sync().channel(); channelizer.connected(); logger.info("Created new connection for {}", uri); } catch (Exception ie) { logger.debug("Error opening connection on {}", uri); throw new ConnectionException(uri, "Could not open connection", ie); } }
private Bootstrap buildBootstrap(URI uri, boolean isSecure) { Bootstrap bootstrap = new Bootstrap(); bootstrap.group(this.eventLoopGroup).channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { configureChannel(channel.config()); ChannelPipeline pipeline = channel.pipeline(); if (isSecure) { Assert.notNull(sslContext, "sslContext should not be null"); pipeline.addLast(sslContext.newHandler(channel.alloc(), uri.getHost(), uri.getPort())); } pipeline.addLast(new HttpClientCodec()); pipeline.addLast(new HttpObjectAggregator(maxResponseSize)); if (readTimeout > 0) { pipeline.addLast(new ReadTimeoutHandler(readTimeout, TimeUnit.MILLISECONDS)); } } }); return bootstrap; }
/** * Establishing TCP connection to server * * @param remoteAddress remote address * */ public ChannelFuture connectToServer(final InetSocketAddress remoteAddress) { if (remoteAddress == null) { throw new IllegalStateException("remote address is null"); } Bootstrap bootstrap = new Bootstrap().group(_upstreamWorkerGroup); bootstrap.channelFactory(NioSocketChannel::new); ServerChannelHandler serverChannelHandler = new ServerChannelHandler(this); bootstrap.handler(new ChannelInitializer<Channel>() { protected void initChannel(Channel ch) throws Exception { initChannelPipeline(ch.pipeline(), serverChannelHandler, _serverConnectionIdleTimeoutMsec); _serverChannel = ch; } }); LOG.debug("Server channel is ready. About to connect...."); return bootstrap.connect(remoteAddress); }
public EventLoopGroup kqueueGroup() { EventLoopGroup nioEventLoopGroup = new KQueueEventLoopGroup(0, new DefaultThreadFactory(threadPrefix)); bootstrap.group(nioEventLoopGroup).channel(KQueueDomainSocketChannel.class) .handler(new ChannelInitializer<KQueueDomainSocketChannel>() { @Override protected void initChannel(final KQueueDomainSocketChannel channel) throws Exception { channel.pipeline().addLast(new LoggingHandler(getClass())); channel.pipeline().addLast(new HttpClientCodec()); channel.pipeline().addLast(new HttpContentDecompressor()); } }); return nioEventLoopGroup; }
public EventLoopGroup epollGroup() { EventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(0, createThreadFactory()); ChannelFactory<EpollDomainSocketChannel> factory = () -> configure(new EpollDomainSocketChannel()); bootstrap.group(epollEventLoopGroup).channelFactory(factory).handler(new ChannelInitializer<UnixChannel>() { @Override protected void initChannel(final UnixChannel channel) throws Exception { channel.pipeline().addLast(new HttpClientCodec()); channel.pipeline().addLast(new HttpContentDecompressor()); } }); return epollEventLoopGroup; }
AtomicReference<Thread> channelThread = new AtomicReference<>(); CountDownLatch connectLatch = new CountDownLatch(1); Bootstrap bootstrap = new Bootstrap(); bootstrap.channelFactory(((VertxInternal)vertx).transport().channelFactory(false)); bootstrap.group(vertx.nettyEventLoopGroup()); bootstrap.resolver(((VertxInternal) vertx).nettyAddressResolverGroup()); bootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ChannelFuture channelFut = bootstrap.connect("localhost", 1234); awaitLatch(connectLatch); channelFut.addListener(v -> {
public static void startAuthLogicConnection(String ip, int port) { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap() .group(group) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("MessageDecoder", new PacketDecoder()); pipeline.addLast("MessageEncoder", new PacketEncoder()); pipeline.addLast("AuthLogicConnectionHandler", new AuthLogicConnectionHandler()); //Auth -> gate } }); bootstrap.connect(ip, port); } }
public static void startGateAuthConnection(String ip, int port) { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap() .group(group) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("MessageDecoder", new PacketDecoder()); pipeline.addLast("MessageEncoder", new PacketEncoder()); pipeline.addLast("GateAuthConnectionHandler", new GateAuthConnectionHandler()); //Auth -> gate } }); bootstrap.connect(ip, port); } }
public static void startGateLogicConnection(String ip, int port) { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap() .group(group) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { ChannelPipeline pipeline = channel.pipeline(); pipeline.addLast("MessageDecoder", new PacketDecoder()); pipeline.addLast("MessageEncoder", new PacketEncoder()); pipeline.addLast("GateLogicConnectionHandler", new GateLogicConnectionHandler()); //logic -> gate } }); bootstrap.connect(ip, port); } }
private Bootstrap initClientBootstrap() { Bootstrap b = new Bootstrap(); eventLoopGroup = new NioEventLoopGroup(); b.group(eventLoopGroup) .channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, ClusterClientConfigManager.getConnectTimeout()) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { clientHandler = new TokenClientHandler(currentState, disconnectCallback); ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 2, 0, 2)); pipeline.addLast(new NettyResponseDecoder()); pipeline.addLast(new LengthFieldPrepender(2)); pipeline.addLast(new NettyRequestEncoder()); pipeline.addLast(clientHandler); } }); return b; }
@Override protected void doOpen() throws Throwable { final NettyClientHandler nettyClientHandler = new NettyClientHandler(getUrl(), this); bootstrap = new Bootstrap(); bootstrap.group(nioEventLoopGroup) .option(ChannelOption.SO_KEEPALIVE, true) .option(ChannelOption.TCP_NODELAY, true) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) //.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getTimeout()) .channel(NioSocketChannel.class); if (getConnectTimeout() < 3000) { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 3000); } else { bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, getConnectTimeout()); } bootstrap.handler(new ChannelInitializer() { @Override protected void initChannel(Channel ch) throws Exception { int heartbeatInterval = UrlUtils.getIdleTimeout(getUrl()); NettyCodecAdapter adapter = new NettyCodecAdapter(getCodec(), getUrl(), NettyClient.this); ch.pipeline()//.addLast("logging",new LoggingHandler(LogLevel.INFO))//for debug .addLast("decoder", adapter.getDecoder()) .addLast("encoder", adapter.getEncoder()) .addLast("client-idle-handler", new IdleStateHandler(heartbeatInterval, 0, 0, MILLISECONDS)) .addLast("handler", nettyClientHandler); } }); }
public Bootstrap newBootstrap(final EventPublisher eventPublisher, final ClientEventListener eventListener) { final Bootstrap nettyBootstrap = new Bootstrap().group(eventLoopGroup) .channel(channelClass) .option(ChannelOption.AUTO_READ, false);// by default do not read content unless asked. for (Entry<ChannelOption<?>, Object> optionEntry : options.entrySet()) { // Type is just for safety for user of ClientState, internally in Bootstrap, types are thrown on the floor. @SuppressWarnings("unchecked") ChannelOption<Object> key = (ChannelOption<Object>) optionEntry.getKey(); nettyBootstrap.option(key, optionEntry.getValue()); } nettyBootstrap.handler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel ch) throws Exception { ch.pipeline().addLast(ClientChannelActiveBufferingHandler.getName(), new ChannelActivityBufferingHandler(eventPublisher, eventListener)); } }); return nettyBootstrap; }
public final void run(final String host, final int port, final ChannelHandler pipelineFactory) { EventLoopGroup group = new NioEventLoopGroup(); Bootstrap bootstrap = new Bootstrap(); bootstrap.group(group) .channel(NioSocketChannel.class) .remoteAddress(host, port) .option(ChannelOption.TCP_NODELAY, true) .handler(pipelineFactory); try { Channel channel = bootstrap.connect().sync().channel(); ChannelFuture future = channel.closeFuture().sync(); future.addListener(ChannelFutureListener.CLOSE); } catch (InterruptedException e) { throw new MocoException(e); } finally { group.shutdownGracefully(); } } }