Merge "Log the address and port when binding fails"
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpHandler.java
index 9d36e931b82aac1db37ab007c5b4d6c1fe0a6d6d..288c58c92968612ad31f8117c7b2204d0351aa6a 100644 (file)
@@ -8,33 +8,27 @@
 
 package org.opendaylight.openflowjava.protocol.impl.core;
 
+import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.SettableFuture;
 import io.netty.bootstrap.ServerBootstrap;
 import io.netty.buffer.PooledByteBufAllocator;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelOption;
+import io.netty.channel.EventLoopGroup;
 import io.netty.channel.WriteBufferWaterMark;
+import io.netty.channel.epoll.EpollEventLoopGroup;
+import io.netty.channel.epoll.EpollServerSocketChannel;
 import io.netty.channel.nio.NioEventLoopGroup;
 import io.netty.channel.socket.ServerSocketChannel;
 import io.netty.channel.socket.nio.NioServerSocketChannel;
 import io.netty.handler.logging.LogLevel;
 import io.netty.handler.logging.LoggingHandler;
-import io.netty.util.concurrent.GenericFutureListener;
-
-import io.netty.channel.epoll.Epoll;
-import io.netty.channel.EventLoopGroup;
-import io.netty.channel.epoll.EpollEventLoopGroup;
-import io.netty.channel.epoll.EpollServerSocketChannel;
-
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
-
 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.util.concurrent.ListenableFuture;
-import com.google.common.util.concurrent.SettableFuture;
-
 /**
  * Class implementing server over TCP / TLS for handling incoming connections.
  *
@@ -57,6 +51,7 @@ 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;
@@ -71,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);
     }
 
     /**
@@ -80,16 +75,18 @@ 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
@@ -103,8 +100,8 @@ public class TcpHandler implements ServerFacade {
 
         final ChannelFuture f;
         try {
-            ServerBootstrap b = new ServerBootstrap();
-            b.group(bossGroup, workerGroup)
+            ServerBootstrap bootstrap = new ServerBootstrap();
+            bootstrap.group(bossGroup, workerGroup)
                     .channel(socketChannelClass)
                     .handler(new LoggingHandler(LogLevel.DEBUG))
                     .childHandler(channelInitializer)
@@ -118,13 +115,17 @@ public class TcpHandler implements ServerFacade {
                     .childOption(ChannelOption.WRITE_SPIN_COUNT, DEFAULT_WRITE_SPIN_COUNT);
 
             if (startupAddress != null) {
-                f = b.bind(startupAddress.getHostAddress(), port).sync();
+                f = bootstrap.bind(startupAddress.getHostAddress(), port).sync();
             } else {
-                f = b.bind(port).sync();
+                f = bootstrap.bind(port).sync();
             }
         } 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 {
@@ -137,6 +138,10 @@ public class TcpHandler implements ServerFacade {
             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);
@@ -146,29 +151,24 @@ public class TcpHandler implements ServerFacade {
     }
 
     /**
-     * Shuts down {@link TcpHandler}}
+     * Shuts down {@link TcpHandler}}.
      */
     @Override
     public ListenableFuture<Boolean> shutdown() {
         final SettableFuture<Boolean> result = SettableFuture.create();
         workerGroup.shutdownGracefully();
         // boss will shutdown as soon, as worker is down
-        bossGroup.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
-
-            @Override
-            public void operationComplete(
-                    final io.netty.util.concurrent.Future<Object> downResult) throws Exception {
-                result.set(downResult.isSuccess());
-                if (downResult.cause() != null) {
-                    result.setException(downResult.cause());
-                }
+        bossGroup.shutdownGracefully().addListener(downResult -> {
+            result.set(downResult.isSuccess());
+            if (downResult.cause() != null) {
+                result.setException(downResult.cause());
             }
-
         });
         return result;
     }
 
     /**
+     * Returns the number of connected clients / channels.
      *
      * @return number of connected clients / channels
      */
@@ -181,23 +181,14 @@ public class TcpHandler implements ServerFacade {
         return isOnlineFuture;
     }
 
-    /**
-     * @return the port
-     */
     public int getPort() {
         return port;
     }
 
-    /**
-     * @return the address
-     */
     public String getAddress() {
         return address;
     }
 
-    /**
-     * @param channelInitializer
-     */
     public void setChannelInitializer(TcpChannelInitializer channelInitializer) {
         this.channelInitializer = channelInitializer;
     }
@@ -208,12 +199,13 @@ public class TcpHandler implements ServerFacade {
     }
 
     /**
-     * Initiate event loop groups
+     * Initiate event loop groups.
+     *
      * @param threadConfiguration number of threads to be created, if not specified in threadConfig
      */
     public void initiateEventLoopGroups(ThreadConfiguration threadConfiguration, boolean isEpollEnabled) {
 
-        if(isEpollEnabled) {
+        if (isEpollEnabled) {
             initiateEpollEventLoopGroups(threadConfiguration);
         } else {
             initiateNioEventLoopGroups(threadConfiguration);
@@ -221,7 +213,8 @@ public class TcpHandler implements ServerFacade {
     }
 
     /**
-     * Initiate Nio event loop groups
+     * Initiate Nio event loop groups.
+     *
      * @param threadConfiguration number of threads to be created, if not specified in threadConfig
      */
     public void initiateNioEventLoopGroups(ThreadConfiguration threadConfiguration) {
@@ -237,14 +230,16 @@ public class TcpHandler implements ServerFacade {
     }
 
     /**
-     * Initiate Epoll event loop groups with Nio as fall back
-     * @param threadConfiguration
+     * Initiate Epoll event loop groups with Nio as fall back.
+     *
+     * @param threadConfiguration the ThreadConfiguration
      */
+    @SuppressWarnings("checkstyle:IllegalCatch")
     protected void initiateEpollEventLoopGroups(ThreadConfiguration threadConfiguration) {
         try {
             socketChannelClass = EpollServerSocketChannel.class;
             if (threadConfiguration != null) {
-                    bossGroup = new EpollEventLoopGroup(threadConfiguration.getBossThreadCount());
+                bossGroup = new EpollEventLoopGroup(threadConfiguration.getBossThreadCount());
                 workerGroup = new EpollEventLoopGroup(threadConfiguration.getWorkerThreadCount());
             } else {
                 bossGroup = new EpollEventLoopGroup();
@@ -252,7 +247,7 @@ public class TcpHandler implements ServerFacade {
             }
             ((EpollEventLoopGroup)workerGroup).setIoRatio(100);
             return;
-        } catch (Throwable ex) {
+        } catch (RuntimeException ex) {
             LOG.debug("Epoll initiation failed");
         }
 
@@ -260,11 +255,7 @@ public class TcpHandler implements ServerFacade {
         initiateNioEventLoopGroups(threadConfiguration);
     }
 
-    /**
-     * @return workerGroup
-     */
     public EventLoopGroup getWorkerGroup() {
         return workerGroup;
     }
-
 }