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;
12 import java.net.InetSocketAddress;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
17 import com.google.common.util.concurrent.SettableFuture;
20 * Class implementing server over TCP for handling incoming connections.
22 * @author michal.polkorab
24 public class TcpHandler extends Thread {
27 private String address;
28 private NioEventLoopGroup workerGroup;
29 private NioEventLoopGroup bossGroup;
30 private static final Logger LOGGER = LoggerFactory.getLogger(TcpHandler.class);
31 private SettableFuture<Boolean> isOnlineFuture;
34 private PublishingChannelInitializer channelInitializer;
37 * Enum used for storing names of used components (in pipeline).
39 public static enum COMPONENT_NAMES {
42 * First component in pipeline - detecting TLS connections
46 * Component for handling TLS frames
50 * Decodes incoming messages into message frames
54 * Detects version of incoming OpenFlow Protocol message
58 * Transforms OpenFlow Protocol messages
62 * Communicates with upper layers (outside OF Library)
69 * Constructor of TCPHandler that listens on selected port.
71 * @param port listening port of TCPHandler server
73 public TcpHandler(int port) {
75 channelInitializer = new PublishingChannelInitializer();
76 isOnlineFuture = SettableFuture.create();
80 * Starts server on selected port.
84 LOGGER.info("Switch ");
85 bossGroup = new NioEventLoopGroup();
86 workerGroup = new NioEventLoopGroup();
88 ServerBootstrap b = new ServerBootstrap();
89 b.group(bossGroup, workerGroup)
90 .channel(NioServerSocketChannel.class)
91 .handler(new LoggingHandler(LogLevel.DEBUG))
92 .childHandler(channelInitializer)
93 .option(ChannelOption.SO_BACKLOG, 128)
94 .childOption(ChannelOption.SO_KEEPALIVE, true);
96 ChannelFuture f = b.bind(port).sync();
98 InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
99 address = isa.getHostName().toString();
100 port = isa.getPort();
101 isOnlineFuture.set(true);
102 LOGGER.info("Switch listener started and ready to accept incoming connections on port: " + port);
103 f.channel().closeFuture().sync();
104 } catch (InterruptedException ex) {
105 LOGGER.error(ex.getMessage(), ex);
112 * Shuts down {@link TcpHandler}}
114 public void shutdown() {
115 workerGroup.shutdownGracefully();
116 bossGroup.shutdownGracefully();
121 * @return number of connected clients / channels
123 public int getNumberOfConnections() {
124 return channelInitializer.size();
128 * @return channelInitializer providing channels
130 public PublishingChannelInitializer getChannelInitializer() {
131 return channelInitializer;
135 * Sets and starts TCPHandler.
140 public static void main(String[] args) throws Exception {
142 if (args.length > 0) {
143 port = Integer.parseInt(args[0]);
147 new TcpHandler(port).start();
151 * @return the isOnlineFuture
153 public SettableFuture<Boolean> getIsOnlineFuture() {
154 return isOnlineFuture;
160 public int getPort() {
165 * @return the address
167 public String getAddress() {