Revert "WIP: Bump upstreams"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpHandler.java
index 0a48de11c2d37db1d14513091c39a53a7da7a001..9ca513ae0ece53c2f19c018cf1575f0e44e330b1 100644 (file)
@@ -51,9 +51,10 @@ public class TcpHandler implements ServerFacade {
     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;
+    private final SettableFuture<Boolean> isOnlineFuture = SettableFuture.create();
     private ThreadConfiguration threadConfig;
 
     private TcpChannelInitializer channelInitializer;
@@ -65,8 +66,8 @@ public class TcpHandler implements ServerFacade {
      *
      * @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);
     }
 
     /**
@@ -74,16 +75,17 @@ public class TcpHandler implements ServerFacade {
      * @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
@@ -119,6 +121,10 @@ public class TcpHandler implements ServerFacade {
         } 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 {
@@ -129,8 +135,11 @@ public class TcpHandler implements ServerFacade {
             this.port = isa.getPort();
 
             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();
+            isOnlineFuture.set(true);
+
+            // 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);
@@ -193,7 +202,6 @@ public class TcpHandler implements ServerFacade {
      * @param threadConfiguration number of threads to be created, if not specified in threadConfig
      */
     public void initiateEventLoopGroups(ThreadConfiguration threadConfiguration, boolean isEpollEnabled) {
-
         if (isEpollEnabled) {
             initiateEpollEventLoopGroups(threadConfiguration);
         } else {