package org.opendaylight.openflowjava.protocol.impl.core; import io.netty.bootstrap.Bootstrap; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.nio.NioSocketChannel; import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Preconditions; import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.SettableFuture; /** * Initializes (TCP) connection to device * @author martin.uhlir * */ public class TcpConnectionInitializer implements ServerFacade, ConnectionInitializer { private static final Logger LOGGER = LoggerFactory .getLogger(TcpConnectionInitializer.class); private EventLoopGroup workerGroup; private ThreadConfiguration threadConfig; private TcpChannelInitializer channelInitializer; private Bootstrap b; /** * Constructor * @param workerGroup - shared worker group */ public TcpConnectionInitializer(NioEventLoopGroup workerGroup) { Preconditions.checkNotNull(workerGroup, "WorkerGroup can't be null"); this.workerGroup = workerGroup; } @Override public void run() { b = new Bootstrap(); b.group(workerGroup).channel(NioSocketChannel.class) .handler(channelInitializer); } @Override public ListenableFuture shutdown() { final SettableFuture result = SettableFuture.create(); workerGroup.shutdownGracefully(); return result; } @Override public ListenableFuture getIsOnlineFuture() { return null; } @Override public void setThreadConfig(ThreadConfiguration threadConfig) { this.threadConfig = threadConfig; } @Override public void initiateConnection(String host, int port) { try { b.connect(host, port).sync(); } catch (InterruptedException e) { LOGGER.error("Unable to initiate connection", e); } } /** * @param channelInitializer */ public void setChannelInitializer(TcpChannelInitializer channelInitializer) { this.channelInitializer = channelInitializer; } }