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;
}
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;
}
Aggregations