Search in sources :

Example 71 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project bgpcep by opendaylight.

the class PCCDispatcherImpl method createClient.

@Override
@SuppressWarnings("unchecked")
public Future<PCEPSession> createClient(final InetSocketAddress remoteAddress, final long reconnectTime, final PCEPSessionListenerFactory listenerFactory, final PCEPSessionNegotiatorFactory negotiatorFactory, final KeyMapping keys, final InetSocketAddress localAddress, final BigInteger dbVersion) {
    final Bootstrap b = new Bootstrap();
    b.group(this.workerGroup);
    b.localAddress(localAddress);
    setChannelFactory(b, keys);
    b.option(ChannelOption.SO_KEEPALIVE, true);
    b.option(ChannelOption.SO_REUSEADDR, true);
    b.option(ChannelOption.RCVBUF_ALLOCATOR, new io.netty.channel.FixedRecvByteBufAllocator(1));
    final long retryTimer = reconnectTime == -1 ? 0 : reconnectTime;
    final PCCReconnectPromise promise = new PCCReconnectPromise(remoteAddress, (int) retryTimer, CONNECT_TIMEOUT, b);
    final ChannelInitializer<SocketChannel> channelInitializer = new ChannelInitializer<SocketChannel>() {

        @Override
        protected void initChannel(final SocketChannel ch) {
            ch.pipeline().addLast(PCCDispatcherImpl.this.factory.getDecoders());
            ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new PCEPSessionNegotiatorFactoryDependencies() {

                @Override
                public PCEPSessionListenerFactory getListenerFactory() {
                    return listenerFactory;
                }

                @Override
                public PCEPPeerProposal getPeerProposal() {
                    return new PCCPeerProposal(dbVersion);
                }
            }, ch, promise));
            ch.pipeline().addLast(PCCDispatcherImpl.this.factory.getEncoders());
            ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                @Override
                public void channelInactive(final ChannelHandlerContext ctx) {
                    if (promise.isCancelled()) {
                        return;
                    }
                    if (!promise.isInitialConnectFinished()) {
                        LOG.debug("Connection to {} was dropped during negotiation, reattempting", remoteAddress);
                        return;
                    }
                    LOG.debug("Reconnecting after connection to {} was dropped", remoteAddress);
                    PCCDispatcherImpl.this.createClient(remoteAddress, reconnectTime, listenerFactory, negotiatorFactory, keys, localAddress, dbVersion);
                }
            });
        }
    };
    b.handler(channelInitializer);
    promise.connect();
    return promise;
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) PCEPSessionNegotiatorFactoryDependencies(org.opendaylight.protocol.pcep.PCEPSessionNegotiatorFactoryDependencies) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter)

Example 72 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project UnderNet by itsMatoosh.

the class Client method connect.

/**
 * Connects the client to a node.
 */
public void connect(Node node) {
    if (status == InterfaceStatus.STOPPING) {
        logger.error("Can't connect to nodes, while the client is stopping!");
        return;
    }
    if (status != InterfaceStatus.STARTED) {
        EventManager.callEvent(new ClientStatusEvent(this, InterfaceStatus.STARTED));
    }
    logger.info("Connecting to node: " + node.address);
    // Making sure the list of client futures exists.
    if (closeFutures == null) {
        closeFutures = new ArrayList<>();
    }
    // Starting the client.
    Bootstrap clientBootstrap = new Bootstrap();
    // Assigning the channel to the client event loop group.
    clientBootstrap.group(workerEventLoopGroup);
    // Using the non blocking io.
    clientBootstrap.channel(NioSocketChannel.class);
    // Making sure the connection is sending the keep alive signal.
    clientBootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    clientBootstrap.handler(new ClientChannelInitializer(this));
    // Connecting
    // Connecting to the node.
    ChannelFuture future = clientBootstrap.connect(node.address);
    ChannelFuture closeFuture = future.channel().closeFuture();
    closeFuture.addListener(new GenericFutureListener<Future<? super Void>>() {

        @Override
        public void operationComplete(Future<? super Void> future) throws Exception {
            // Removing the future from future list.
            closeFutures.remove(future);
            if (closeFutures.size() == 0) {
                // Stopping the worker group.
                EventManager.callEvent(new ClientStatusEvent(Client.this, InterfaceStatus.STOPPED));
            }
        }
    });
    closeFutures.add(closeFuture);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Bootstrap(io.netty.bootstrap.Bootstrap) ChannelFuture(io.netty.channel.ChannelFuture) Future(io.netty.util.concurrent.Future) ClientStatusEvent(me.matoosh.undernet.event.client.ClientStatusEvent)

Example 73 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project runelite by runelite.

the class CacheClient method connect.

public void connect() {
    Bootstrap b = new Bootstrap();
    b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ChannelPipeline p = ch.pipeline();
            // p.addFirst(new HttpProxyHandler(new InetSocketAddress("runelite.net", 3128)));
            p.addLast("decoder", new HandshakeResponseDecoder());
            p.addLast(new CacheClientHandler(), new HandshakeResponseHandler(CacheClient.this), new ArchiveResponseHandler(CacheClient.this));
            p.addLast(new UpdateHandshakeEncoder(), new EncryptionEncoder(), new ArchiveRequestEncoder());
        }
    });
    // Start the client.
    ChannelFuture f = b.connect(host, PORT).syncUninterruptibly();
    channel = f.channel();
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) EncryptionEncoder(net.runelite.protocol.update.encoders.EncryptionEncoder) UpdateHandshakeEncoder(net.runelite.protocol.handshake.UpdateHandshakeEncoder) HandshakeResponseDecoder(net.runelite.protocol.update.decoders.HandshakeResponseDecoder) IOException(java.io.IOException) ChannelPipeline(io.netty.channel.ChannelPipeline) ArchiveRequestEncoder(net.runelite.protocol.update.encoders.ArchiveRequestEncoder) Bootstrap(io.netty.bootstrap.Bootstrap)

Example 74 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project scalecube by scalecube.

the class TransportImpl method connect.

private ChannelFuture connect(Address address) {
    OutgoingChannelInitializer channelInitializer = new OutgoingChannelInitializer(address);
    Bootstrap client = bootstrapFactory.clientBootstrap().handler(channelInitializer);
    ChannelFuture connectFuture = client.connect(address.host(), address.port());
    // Register logger and cleanup listener
    connectFuture.addListener((ChannelFutureListener) channelFuture -> {
        if (channelFuture.isSuccess()) {
            LOGGER.debug("Connected from {} to {}: {}", TransportImpl.this.address, address, channelFuture.channel());
        } else {
            LOGGER.warn("Failed to connect from {} to {}", TransportImpl.this.address, address);
            outgoingChannels.remove(address);
        }
    });
    return connectFuture;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ProtobufVarint32LengthFieldPrepender(io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) BindException(java.net.BindException) Observable(rx.Observable) InetAddress(java.net.InetAddress) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) MAX_PORT_NUMBER(io.scalecube.transport.Addressing.MAX_PORT_NUMBER) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) Map(java.util.Map) Schedulers(rx.schedulers.Schedulers) NoSuchElementException(java.util.NoSuchElementException) MIN_PORT_NUMBER(io.scalecube.transport.Addressing.MIN_PORT_NUMBER) Nonnull(javax.annotation.Nonnull) MessageToByteEncoder(io.netty.handler.codec.MessageToByteEncoder) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) MessageToMessageDecoder(io.netty.handler.codec.MessageToMessageDecoder) Logger(org.slf4j.Logger) ChannelInitializer(io.netty.channel.ChannelInitializer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ChannelPipeline(io.netty.channel.ChannelPipeline) ServerChannel(io.netty.channel.ServerChannel) ProtobufVarint32FrameDecoder(io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder) Subject(rx.subjects.Subject) Preconditions.checkState(com.google.common.base.Preconditions.checkState) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelHandler(io.netty.channel.ChannelHandler) PublishSubject(rx.subjects.PublishSubject) CheckForNull(javax.annotation.CheckForNull) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap)

Example 75 with Bootstrap

use of org.apache.flink.shaded.netty4.io.netty.bootstrap.Bootstrap in project scalecube by scalecube.

the class StreamProcessorsTest method testClientStreamProcessorsNotSameAfterSetup.

@Test
public void testClientStreamProcessorsNotSameAfterSetup() {
    ClientStreamProcessors defaultOne = StreamProcessors.newClient();
    // client with bootstrap
    ClientStreamProcessors first = defaultOne.bootstrap(new Bootstrap());
    // another client with custom bootstrap
    ClientStreamProcessors second = defaultOne.bootstrap(new Bootstrap());
    assertNotSame(defaultOne, first);
    assertNotSame(defaultOne, second);
    assertNotSame(first, second);
}
Also used : Bootstrap(io.netty.bootstrap.Bootstrap) Test(org.junit.Test)

Aggregations

Bootstrap (io.netty.bootstrap.Bootstrap)429 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)207 Channel (io.netty.channel.Channel)194 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)189 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)169 ChannelFuture (io.netty.channel.ChannelFuture)152 EventLoopGroup (io.netty.channel.EventLoopGroup)139 SocketChannel (io.netty.channel.socket.SocketChannel)123 InetSocketAddress (java.net.InetSocketAddress)119 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)113 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)108 Test (org.junit.jupiter.api.Test)89 ChannelPipeline (io.netty.channel.ChannelPipeline)76 LocalChannel (io.netty.channel.local.LocalChannel)74 LocalServerChannel (io.netty.channel.local.LocalServerChannel)71 LocalAddress (io.netty.channel.local.LocalAddress)66 CountDownLatch (java.util.concurrent.CountDownLatch)62 IOException (java.io.IOException)60 NioServerSocketChannel (io.netty.channel.socket.nio.NioServerSocketChannel)56 ChannelInitializer (io.netty.channel.ChannelInitializer)45