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
8 package org.opendaylight.openflowjava.protocol.impl.core;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import com.google.common.util.concurrent.SettableFuture;
14 import io.netty.bootstrap.Bootstrap;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.epoll.EpollSocketChannel;
17 import io.netty.channel.socket.nio.NioSocketChannel;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * Initializes (TCP) connection to device.
24 * @author martin.uhlir
26 public class TcpConnectionInitializer implements ServerFacade, ConnectionInitializer {
27 private static final Logger LOG = LoggerFactory.getLogger(TcpConnectionInitializer.class);
29 private final SettableFuture<Void> hasRun = SettableFuture.create();
30 private final EventLoopGroup workerGroup;
31 private final boolean isEpollEnabled;
33 private TcpChannelInitializer channelInitializer;
34 private Bootstrap bootstrap;
39 * @param workerGroup - shared worker group
41 public TcpConnectionInitializer(final EventLoopGroup workerGroup, final boolean isEpollEnabled) {
42 this.workerGroup = requireNonNull(workerGroup, "WorkerGroup can't be null");
43 this.isEpollEnabled = isEpollEnabled;
48 bootstrap = new Bootstrap();
50 bootstrap.group(workerGroup).channel(EpollSocketChannel.class).handler(channelInitializer);
52 bootstrap.group(workerGroup).channel(NioSocketChannel.class).handler(channelInitializer);
58 public ListenableFuture<Boolean> shutdown() {
59 final var result = SettableFuture.<Boolean>create();
60 workerGroup.shutdownGracefully();
65 public ListenableFuture<Void> getIsOnlineFuture() {
70 public void initiateConnection(final String host, final int port) {
72 bootstrap.connect(host, port).sync();
73 } catch (InterruptedException e) {
74 LOG.error("Unable to initiate connection", e);
78 public void setChannelInitializer(final TcpChannelInitializer channelInitializer) {
79 this.channelInitializer = channelInitializer;