1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.core;
4 import io.netty.bootstrap.ServerBootstrap;
5 import io.netty.channel.ChannelFuture;
6 import io.netty.channel.ChannelOption;
7 import io.netty.channel.nio.NioEventLoopGroup;
8 import io.netty.channel.socket.nio.NioServerSocketChannel;
9 import io.netty.handler.logging.LogLevel;
10 import io.netty.handler.logging.LoggingHandler;
11 import io.netty.util.concurrent.GenericFutureListener;
13 import java.net.InetAddress;
14 import java.net.InetSocketAddress;
16 import org.opendaylight.openflowjava.protocol.impl.connection.ServerFacade;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.SettableFuture;
24 * Class implementing server over TCP for handling incoming connections.
26 * @author michal.polkorab
28 public class TcpHandler implements ServerFacade {
31 private String address;
32 private InetAddress startupAddress;
33 private NioEventLoopGroup workerGroup;
34 private NioEventLoopGroup bossGroup;
35 private static final Logger LOGGER = LoggerFactory.getLogger(TcpHandler.class);
36 private SettableFuture<Boolean> isOnlineFuture;
39 private PublishingChannelInitializer channelInitializer;
42 * Enum used for storing names of used components (in pipeline).
44 public static enum COMPONENT_NAMES {
47 * First component in pipeline - detecting TLS connections
51 * Component for handling TLS frames
55 * Decodes incoming messages into message frames
59 * Detects version of incoming OpenFlow Protocol message
63 * Transforms OpenFlow Protocol byte messages into POJOs
67 * Transforms POJOs into OpenFlow Protocol byte messages
71 * Delegates translated POJOs into MessageConsumer
73 DELEGATING_INBOUND_HANDLER,
78 * Constructor of TCPHandler that listens on selected port.
80 * @param port listening port of TCPHandler server
82 public TcpHandler(int port) {
87 * Constructor of TCPHandler that listens on selected address and port.
88 * @param address listening address of TCPHandler server
89 * @param port listening port of TCPHandler server
91 public TcpHandler(InetAddress address, int port) {
93 this.startupAddress = address;
94 channelInitializer = new PublishingChannelInitializer();
95 isOnlineFuture = SettableFuture.create();
99 * Starts server on selected port.
103 LOGGER.info("Switch ");
104 bossGroup = new NioEventLoopGroup();
105 workerGroup = new NioEventLoopGroup();
107 ServerBootstrap b = new ServerBootstrap();
108 b.group(bossGroup, workerGroup)
109 .channel(NioServerSocketChannel.class)
110 .handler(new LoggingHandler(LogLevel.DEBUG))
111 .childHandler(channelInitializer)
112 .option(ChannelOption.SO_BACKLOG, 128)
113 .childOption(ChannelOption.SO_KEEPALIVE, true);
116 if (startupAddress != null) {
117 f = b.bind(startupAddress.getHostAddress(), port).sync();
119 f = b.bind(port).sync();
122 InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
123 address = isa.getHostString();
124 LOGGER.debug("address from tcphandler: " + address);
125 port = isa.getPort();
126 isOnlineFuture.set(true);
127 LOGGER.info("Switch listener started and ready to accept incoming connections on port: " + port);
128 f.channel().closeFuture().sync();
129 } catch (InterruptedException ex) {
130 LOGGER.error(ex.getMessage(), ex);
137 * Shuts down {@link TcpHandler}}
140 public ListenableFuture<Boolean> shutdown() {
141 final SettableFuture<Boolean> result = SettableFuture.create();
142 workerGroup.shutdownGracefully();
143 // boss will shutdown as soon, as worker is down
144 bossGroup.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
147 public void operationComplete(
148 io.netty.util.concurrent.Future<Object> downResult) throws Exception {
149 result.set(downResult.isSuccess());
150 result.setException(downResult.cause());
159 * @return number of connected clients / channels
161 public int getNumberOfConnections() {
162 return channelInitializer.size();
166 * @return channelInitializer providing channels
168 public PublishingChannelInitializer getChannelInitializer() {
169 return channelInitializer;
173 * Sets and starts TCPHandler.
178 public static void main(String[] args) throws Exception {
180 if (args.length > 0) {
181 port = Integer.parseInt(args[0]);
185 new Thread(new TcpHandler(port)).start();
189 public ListenableFuture<Boolean> getIsOnlineFuture() {
190 return isOnlineFuture;
196 public int getPort() {
201 * @return the address
203 public String getAddress() {