Search in sources :

Example 1 with MAX_PORT_NUMBER

use of io.scalecube.transport.Addressing.MAX_PORT_NUMBER in project scalecube by scalecube.

the class TransportImpl method bind0.

/**
 * Helper bind method to start accepting connections on {@code listenAddress} and {@code bindPort}.
 *
 * @param bindPort bind port.
 * @param finalBindPort maximum port to bind.
 * @throws NoSuchElementException if {@code bindPort} greater than {@code finalBindPort}.
 * @throws IllegalArgumentException if {@code bindPort} doesnt belong to the range [{@link Addressing#MIN_PORT_NUMBER}
 *         .. {@link Addressing#MAX_PORT_NUMBER}].
 */
private CompletableFuture<Transport> bind0(ServerBootstrap server, InetAddress listenAddress, int bindPort, int finalBindPort) {
    incomingMessagesSubject.subscribeOn(Schedulers.from(bootstrapFactory.getWorkerGroup()));
    final CompletableFuture<Transport> result = new CompletableFuture<>();
    // Perform basic bind port validation
    if (bindPort < MIN_PORT_NUMBER || bindPort > MAX_PORT_NUMBER) {
        result.completeExceptionally(new IllegalArgumentException("Invalid port number: " + bindPort));
        return result;
    }
    if (bindPort > finalBindPort) {
        result.completeExceptionally(new NoSuchElementException("Could not find an available port from: " + bindPort + " to: " + finalBindPort));
        return result;
    }
    // Get address object and bind
    address = Address.create(listenAddress.getHostAddress(), bindPort);
    ChannelFuture bindFuture = server.bind(listenAddress, address.port());
    bindFuture.addListener((ChannelFutureListener) channelFuture -> {
        if (channelFuture.isSuccess()) {
            serverChannel = (ServerChannel) channelFuture.channel();
            networkEmulator = new NetworkEmulator(address, config.isUseNetworkEmulator());
            networkEmulatorHandler = config.isUseNetworkEmulator() ? new NetworkEmulatorHandler(networkEmulator) : null;
            LOGGER.info("Bound to: {}", address);
            result.complete(TransportImpl.this);
        } else {
            Throwable cause = channelFuture.cause();
            if (config.isPortAutoIncrement() && isAddressAlreadyInUseException(cause)) {
                LOGGER.warn("Can't bind to address {}, try again on different port [cause={}]", address, cause.toString());
                bind0(server, listenAddress, bindPort + 1, finalBindPort).thenAccept(result::complete);
            } else {
                LOGGER.error("Failed to bind to: {}, cause: {}", address, cause);
                result.completeExceptionally(cause);
            }
        }
    });
    return result;
}
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) CompletableFuture(java.util.concurrent.CompletableFuture) ServerChannel(io.netty.channel.ServerChannel) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with MAX_PORT_NUMBER

use of io.scalecube.transport.Addressing.MAX_PORT_NUMBER in project scalecube by scalecube.

the class NettyServerTransport method bind0.

private CompletableFuture<NettyServerTransport> bind0(InetAddress bindAddress, int bindPort, int finalBindPort) {
    CompletableFuture<NettyServerTransport> result = new CompletableFuture<>();
    // Perform basic bind port validation
    if (bindPort < MIN_PORT_NUMBER || bindPort > MAX_PORT_NUMBER) {
        result.completeExceptionally(new IllegalArgumentException("Invalid port number: " + bindPort));
        return result;
    }
    if (bindPort > finalBindPort) {
        result.completeExceptionally(new NoSuchElementException("Could not find an available port from: " + bindPort + " to: " + finalBindPort));
        return result;
    }
    // Start binding
    ChannelFuture bindFuture = serverBootstrap.bind(bindAddress, bindPort);
    bindFuture.addListener((ChannelFutureListener) channelFuture -> {
        if (channelFuture.isSuccess()) {
            NettyServerTransport.this.init(bindAddress, bindPort, (ServerChannel) channelFuture.channel());
            result.complete(NettyServerTransport.this);
        } else {
            Throwable cause = channelFuture.cause();
            if (config.isPortAutoIncrement() && isAddressAlreadyInUseException(cause)) {
                bind0(bindAddress, bindPort + 1, finalBindPort).thenAccept(result::complete);
            } else {
                result.completeExceptionally(cause);
            }
        }
    });
    return result;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ChannelContext(io.scalecube.streams.ChannelContext) Address(io.scalecube.transport.Address) Addressing(io.scalecube.transport.Addressing) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) BindException(java.net.BindException) ServerChannel(io.netty.channel.ServerChannel) ChannelFuture(io.netty.channel.ChannelFuture) ConcurrentMap(java.util.concurrent.ConcurrentMap) ListeningServerStream(io.scalecube.streams.ListeningServerStream) InetAddress(java.net.InetAddress) Consumer(java.util.function.Consumer) MAX_PORT_NUMBER(io.scalecube.transport.Addressing.MAX_PORT_NUMBER) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Optional(java.util.Optional) NoSuchElementException(java.util.NoSuchElementException) MIN_PORT_NUMBER(io.scalecube.transport.Addressing.MIN_PORT_NUMBER) CompletableFuture(java.util.concurrent.CompletableFuture) ServerChannel(io.netty.channel.ServerChannel) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ServerBootstrap (io.netty.bootstrap.ServerBootstrap)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 ServerChannel (io.netty.channel.ServerChannel)2 MAX_PORT_NUMBER (io.scalecube.transport.Addressing.MAX_PORT_NUMBER)2 MIN_PORT_NUMBER (io.scalecube.transport.Addressing.MIN_PORT_NUMBER)2 BindException (java.net.BindException)2 InetAddress (java.net.InetAddress)2 NoSuchElementException (java.util.NoSuchElementException)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Bootstrap (io.netty.bootstrap.Bootstrap)1 ByteBuf (io.netty.buffer.ByteBuf)1 Channel (io.netty.channel.Channel)1 ChannelDuplexHandler (io.netty.channel.ChannelDuplexHandler)1 ChannelHandler (io.netty.channel.ChannelHandler)1 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)1 ChannelInitializer (io.netty.channel.ChannelInitializer)1