1 package org.opendaylight.openflowjava.protocol.impl.core;
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;
11 import com.google.common.base.Preconditions;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
16 * Initializes (TCP) connection to device
17 * @author martin.uhlir
20 public class TcpConnectionInitializer implements ServerFacade,
21 ConnectionInitializer {
23 private static final Logger LOGGER = LoggerFactory
24 .getLogger(TcpConnectionInitializer.class);
25 private EventLoopGroup workerGroup;
26 private ThreadConfiguration threadConfig;
28 private TcpChannelInitializer channelInitializer;
30 private boolean isEpollEnabled;
34 * @param workerGroup - shared worker group
36 public TcpConnectionInitializer(EventLoopGroup workerGroup, boolean isEpollEnabled) {
37 Preconditions.checkNotNull(workerGroup, "WorkerGroup can't be null");
38 this.workerGroup = workerGroup;
39 this.isEpollEnabled = isEpollEnabled;
46 b.group(workerGroup).channel(EpollSocketChannel.class)
47 .handler(channelInitializer);
49 b.group(workerGroup).channel(NioSocketChannel.class)
50 .handler(channelInitializer);
55 public ListenableFuture<Boolean> shutdown() {
56 final SettableFuture<Boolean> result = SettableFuture.create();
57 workerGroup.shutdownGracefully();
62 public ListenableFuture<Boolean> getIsOnlineFuture() {
67 public void setThreadConfig(ThreadConfiguration threadConfig) {
68 this.threadConfig = threadConfig;
72 public void initiateConnection(String host, int port) {
74 b.connect(host, port).sync();
75 } catch (InterruptedException e) {
76 LOGGER.error("Unable to initiate connection", e);
81 * @param channelInitializer
83 public void setChannelInitializer(TcpChannelInitializer channelInitializer) {
84 this.channelInitializer = channelInitializer;