1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.openflow.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 org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
15 import com.google.common.util.concurrent.SettableFuture;
18 * Class implementing server over TCP for handling incoming connections.
20 * @author michal.polkorab
22 public class TCPHandler extends Thread {
25 private NioEventLoopGroup workerGroup;
26 private NioEventLoopGroup bossGroup;
27 private static final Logger LOGGER = LoggerFactory.getLogger(TCPHandler.class);
28 private SettableFuture<Boolean> isOnlineFuture;
31 private PublishingChannelInitializer channelInitializer;
34 * Enum used for storing names of used components (in pipeline).
36 public static enum COMPONENT_NAMES {
39 * First component in pipeline - detecting TLS connections
43 * Component for handling TLS frames
47 * Decodes incoming messages into message frames
51 * Detects version of incoming OpenFlow Protocol message
55 * Transforms OpenFlow Protocol messages
59 * Communicates with upper layers (outside OF Library)
66 * Constructor of TCPHandler that listens on selected port.
68 * @param port listening port of TCPHandler server
70 public TCPHandler(int port) {
72 channelInitializer = new PublishingChannelInitializer();
73 isOnlineFuture = SettableFuture.create();
77 * Starts server on selected port.
81 LOGGER.info("Switch ");
82 bossGroup = new NioEventLoopGroup();
83 workerGroup = new NioEventLoopGroup();
85 ServerBootstrap b = new ServerBootstrap();
86 b.group(bossGroup, workerGroup)
87 .channel(NioServerSocketChannel.class)
88 .handler(new LoggingHandler(LogLevel.DEBUG))
89 .childHandler(channelInitializer)
90 .option(ChannelOption.SO_BACKLOG, 128)
91 .childOption(ChannelOption.SO_KEEPALIVE, true);
93 ChannelFuture f = b.bind(port).sync();
94 isOnlineFuture.set(true);
95 LOGGER.info("Switch listener started and ready to accept incoming connections on port: " + port);
96 f.channel().closeFuture().sync();
97 } catch (InterruptedException ex) {
98 LOGGER.error(ex.getMessage(), ex);
105 * Shuts down {@link TCPHandler}}
107 public void shutdown() {
108 workerGroup.shutdownGracefully();
109 bossGroup.shutdownGracefully();
114 * @return number of connected clients / channels
116 public int getNumberOfConnections() {
117 return channelInitializer.size();
121 * @return channelInitializer providing channels
123 public PublishingChannelInitializer getChannelInitializer() {
124 return channelInitializer;
128 * Sets and starts TCPHandler.
133 public static void main(String[] args) throws Exception {
135 if (args.length > 0) {
136 port = Integer.parseInt(args[0]);
140 new TCPHandler(port).start();
144 * @return the isOnlineFuture
146 public SettableFuture<Boolean> getIsOnlineFuture() {
147 return isOnlineFuture;