2 * Copyright (c) 2015 Pantheon Technologies s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowjava.protocol.impl.core;
11 import io.netty.bootstrap.Bootstrap;
12 import io.netty.channel.EventLoopGroup;
13 import io.netty.channel.epoll.EpollSocketChannel;
14 import io.netty.channel.socket.nio.NioSocketChannel;
15 import org.opendaylight.openflowjava.protocol.api.connection.ThreadConfiguration;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import com.google.common.base.Preconditions;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.SettableFuture;
24 * Initializes (TCP) connection to device
25 * @author martin.uhlir
28 public class TcpConnectionInitializer implements ServerFacade,
29 ConnectionInitializer {
31 private static final Logger LOG = LoggerFactory
32 .getLogger(TcpConnectionInitializer.class);
33 private EventLoopGroup workerGroup;
34 private ThreadConfiguration threadConfig;
36 private TcpChannelInitializer channelInitializer;
38 private boolean isEpollEnabled;
42 * @param workerGroup - shared worker group
44 public TcpConnectionInitializer(EventLoopGroup workerGroup, boolean isEpollEnabled) {
45 Preconditions.checkNotNull(workerGroup, "WorkerGroup can't be null");
46 this.workerGroup = workerGroup;
47 this.isEpollEnabled = isEpollEnabled;
54 b.group(workerGroup).channel(EpollSocketChannel.class)
55 .handler(channelInitializer);
57 b.group(workerGroup).channel(NioSocketChannel.class)
58 .handler(channelInitializer);
63 public ListenableFuture<Boolean> shutdown() {
64 final SettableFuture<Boolean> result = SettableFuture.create();
65 workerGroup.shutdownGracefully();
70 public ListenableFuture<Boolean> getIsOnlineFuture() {
75 public void setThreadConfig(ThreadConfiguration threadConfig) {
76 this.threadConfig = threadConfig;
80 public void initiateConnection(String host, int port) {
82 b.connect(host, port).sync();
83 } catch (InterruptedException e) {
84 LOG.error("Unable to initiate connection", e);
89 * @param channelInitializer
91 public void setChannelInitializer(TcpChannelInitializer channelInitializer) {
92 this.channelInitializer = channelInitializer;