Bug 3230 - Attempt to use Epoll native transport if available
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / UdpHandler.java
index 3e6e384e66c83a408e52152f1b569a4a8b731a09..9339ba1664f7781a914f33f5ed0becb84edf562c 100644 (file)
@@ -12,7 +12,10 @@ import io.netty.bootstrap.Bootstrap;
 import io.netty.channel.ChannelFuture;
 import io.netty.channel.ChannelOption;
 import io.netty.channel.EventLoopGroup;
+import io.netty.channel.epoll.EpollDatagramChannel;
+import io.netty.channel.epoll.EpollEventLoopGroup;
 import io.netty.channel.nio.NioEventLoopGroup;
+import io.netty.channel.socket.DatagramChannel;
 import io.netty.channel.socket.nio.NioDatagramChannel;
 import io.netty.util.concurrent.GenericFutureListener;
 
@@ -41,6 +44,7 @@ public final class UdpHandler implements ServerFacade {
     private final SettableFuture<Boolean> isOnlineFuture;
     private UdpChannelInitializer channelInitializer;
     private ThreadConfiguration threadConfig;
+    private Class<? extends DatagramChannel> datagramChannelClass;
 
     /**
      * Constructor of UdpHandler that listens on selected port.
@@ -64,16 +68,11 @@ public final class UdpHandler implements ServerFacade {
 
     @Override
     public void run() {
-        if (threadConfig != null) {
-            group = new NioEventLoopGroup(threadConfig.getWorkerThreadCount());
-        } else {
-            group = new NioEventLoopGroup();
-        }
         final ChannelFuture f;
         try {
             Bootstrap b = new Bootstrap();
             b.group(group)
-             .channel(NioDatagramChannel.class)
+             .channel(datagramChannelClass)
              .option(ChannelOption.SO_BROADCAST, false)
              .handler(channelInitializer);
 
@@ -146,4 +145,51 @@ public final class UdpHandler implements ServerFacade {
     public void setThreadConfig(ThreadConfiguration threadConfig) {
         this.threadConfig = threadConfig;
     }
+
+    /**
+     * 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) {
+            initiateEpollEventLoopGroups(threadConfiguration);
+        } else {
+            initiateNioEventLoopGroups(threadConfiguration);
+        }
+    }
+
+    /**
+     * Initiate Nio event loop groups
+     * @param threadConfiguration number of threads to be created, if not specified in threadConfig
+     */
+    public void initiateNioEventLoopGroups(ThreadConfiguration threadConfiguration) {
+        datagramChannelClass = NioDatagramChannel.class;
+        if (threadConfiguration != null) {
+            group = new NioEventLoopGroup(threadConfiguration.getWorkerThreadCount());
+        } else {
+            group = new NioEventLoopGroup();
+        }
+    }
+
+    /**
+     * Initiate Epoll event loop groups with Nio as fall back
+     * @param threadConfiguration
+     */
+    protected void initiateEpollEventLoopGroups(ThreadConfiguration threadConfiguration) {
+        try {
+            datagramChannelClass = EpollDatagramChannel.class;
+            if (threadConfiguration != null) {
+                group = new EpollEventLoopGroup(threadConfiguration.getWorkerThreadCount());
+            } else {
+                group = new EpollEventLoopGroup();
+            }
+            return;
+        } catch (Throwable ex) {
+            LOGGER.debug("Epoll initiation failed");
+        }
+
+        //Fallback mechanism
+        initiateNioEventLoopGroups(threadConfiguration);
+    }
 }
\ No newline at end of file