Bug 3230 - Attempt to use Epoll native transport if available
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpConnectionInitializer.java
1 package org.opendaylight.openflowjava.protocol.impl.core;
2
3 import io.netty.bootstrap.Bootstrap;
4 import io.netty.channel.EventLoopGroup;
5 import io.netty.channel.epoll.EpollSocketChannel;
6 import io.netty.channel.socket.nio.NioSocketChannel;
7 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14
15 /**
16  * Initializes (TCP) connection to device
17  * @author martin.uhlir
18  *
19  */
20 public class TcpConnectionInitializer implements ServerFacade,
21         ConnectionInitializer {
22
23     private static final Logger LOGGER = LoggerFactory
24             .getLogger(TcpConnectionInitializer.class);
25     private EventLoopGroup workerGroup;
26     private ThreadConfiguration threadConfig;
27
28     private TcpChannelInitializer channelInitializer;
29     private Bootstrap b;
30     private boolean isEpollEnabled;
31
32     /**
33      * Constructor
34      * @param workerGroup - shared worker group
35      */
36     public TcpConnectionInitializer(EventLoopGroup workerGroup, boolean isEpollEnabled) {
37         Preconditions.checkNotNull(workerGroup, "WorkerGroup can't be null");
38         this.workerGroup = workerGroup;
39         this.isEpollEnabled = isEpollEnabled;
40     }
41
42     @Override
43     public void run() {
44         b = new Bootstrap();
45         if(isEpollEnabled) {
46             b.group(workerGroup).channel(EpollSocketChannel.class)
47                     .handler(channelInitializer);
48         } else {
49             b.group(workerGroup).channel(NioSocketChannel.class)
50                     .handler(channelInitializer);
51         }
52     }
53
54     @Override
55     public ListenableFuture<Boolean> shutdown() {
56         final SettableFuture<Boolean> result = SettableFuture.create();
57         workerGroup.shutdownGracefully();
58         return result;
59     }
60
61     @Override
62     public ListenableFuture<Boolean> getIsOnlineFuture() {
63         return null;
64     }
65
66     @Override
67     public void setThreadConfig(ThreadConfiguration threadConfig) {
68         this.threadConfig = threadConfig;
69     }
70
71     @Override
72     public void initiateConnection(String host, int port) {
73         try {
74             b.connect(host, port).sync();
75         } catch (InterruptedException e) {
76             LOGGER.error("Unable to initiate connection", e);
77         }
78     }
79
80     /**
81      * @param channelInitializer
82      */
83     public void setChannelInitializer(TcpChannelInitializer channelInitializer) {
84         this.channelInitializer = channelInitializer;
85     }
86 }