private static int bind( int firstPort, int portsCount, boolean tryAnyPort, @NotNull ServerBootstrap bootstrap, @NotNull ChannelRegistrar channelRegistrar) throws Exception { InetAddress address = NetUtils.getLoopbackAddress(); for (int i = 0; i < portsCount; i++) { int port = firstPort + i; if (ArrayUtil.indexOf(FORBIDDEN_PORTS, i) >= 0) { continue; } ChannelFuture future = bootstrap.bind(address, port).awaitUninterruptibly(); if (future.isSuccess()) { channelRegistrar.add(future.channel()); return port; } else if (!tryAnyPort && i == (portsCount - 1)) { ExceptionUtil.rethrowAll(future.cause()); } } LOG.info("We cannot bind to our default range, so, try to bind to any free port"); ChannelFuture future = bootstrap.bind(address, 0).awaitUninterruptibly(); if (future.isSuccess()) { channelRegistrar.add(future.channel()); return ((InetSocketAddress) future.channel().localAddress()).getPort(); } else { ExceptionUtil.rethrowAll(future.cause()); } return -1; // unreachable }
@Override public void dispose() { channelRegistrar.close(isOwnerOfEventLoopGroup); LOG.info("web server stopped"); }
public boolean isRunning() { return !channelRegistrar.isEmpty(); }