private int port;
private String address;
private final InetAddress startupAddress;
+ private final Runnable readyRunnable;
private EventLoopGroup workerGroup;
private EventLoopGroup bossGroup;
private final SettableFuture<Boolean> isOnlineFuture;
*
* @param port listening port of TCPHandler server
*/
- public TcpHandler(final int port) {
- this(null, port);
+ public TcpHandler(final int port, Runnable readyRunnable) {
+ this(null, port, readyRunnable);
}
/**
* @param address listening address of TCPHandler server
* @param port listening port of TCPHandler server
*/
- public TcpHandler(final InetAddress address, final int port) {
+ public TcpHandler(final InetAddress address, final int port, Runnable readyRunnable) {
this.port = port;
this.startupAddress = address;
isOnlineFuture = SettableFuture.create();
+ this.readyRunnable = readyRunnable;
}
/**
* Starts server on selected port.
*/
@Override
+ @SuppressWarnings("checkstyle:IllegalCatch")
public void run() {
/*
* We generally do not perform IO-unrelated tasks, so we want to have
} catch (InterruptedException e) {
LOG.error("Interrupted while binding port {}", port, e);
return;
+ } catch (Throwable throwable) {
+ // sync() re-throws exceptions declared as Throwable, so the compiler doesn't see them
+ LOG.error("Error while binding address {} and port {}", startupAddress, port, throwable);
+ throw throwable;
}
try {
LOG.debug("address from tcphandler: {}", address);
isOnlineFuture.set(true);
LOG.info("Switch listener started and ready to accept incoming tcp/tls connections on port: {}", port);
+
+ readyRunnable.run();
+
+ // This waits until this channel is closed, and rethrows the cause of the failure if this future failed.
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
LOG.error("Interrupted while waiting for port {} shutdown", port, e);
*/
public final class UdpHandler implements ServerFacade {
- private static final Logger LOG = LoggerFactory
- .getLogger(UdpHandler.class);
+ private static final Logger LOG = LoggerFactory.getLogger(UdpHandler.class);
+
private int port;
private EventLoopGroup group;
private final InetAddress startupAddress;
+ private final Runnable readyRunnable;
private final SettableFuture<Boolean> isOnlineFuture;
private UdpChannelInitializer channelInitializer;
private ThreadConfiguration threadConfig;
*
* @param port listening port of UdpHandler server
*/
- public UdpHandler(final int port) {
- this(null, port);
+ public UdpHandler(final int port, Runnable readyRunnable) {
+ this(null, port, readyRunnable);
}
/**
* @param address listening address of UdpHandler server
* @param port listening port of UdpHandler server
*/
- public UdpHandler(final InetAddress address, final int port) {
+ public UdpHandler(final InetAddress address, final int port, Runnable readyRunnable) {
this.port = port;
this.startupAddress = address;
isOnlineFuture = SettableFuture.create();
+ this.readyRunnable = readyRunnable;
}
@Override
+ @SuppressWarnings("checkstyle:IllegalCatch")
public void run() {
final ChannelFuture f;
try {
} catch (InterruptedException e) {
LOG.error("Interrupted while binding port {}", port, e);
return;
+ } catch (Throwable throwable) {
+ // sync() re-throws exceptions declared as Throwable, so the compiler doesn't see them
+ LOG.error("Error while binding address {} and port {}", startupAddress, port, throwable);
+ throw throwable;
}
try {
LOG.debug("Address from udpHandler: {}", address);
isOnlineFuture.set(true);
LOG.info("Switch listener started and ready to accept incoming udp connections on port: {}", port);
+ readyRunnable.run();
+ // This waits until this channel is closed, and rethrows the cause of the failure if this future failed.
f.channel().closeFuture().sync();
} catch (InterruptedException e) {
LOG.error("Interrupted while waiting for port {} shutdown", port, e);