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.InetSocketAddress;
15 import org.opendaylight.openflowjava.protocol.impl.connection.ServerFacade;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.SettableFuture;
23 * Class implementing server over TCP for handling incoming connections.
25 * @author michal.polkorab
27 public class TcpHandler implements ServerFacade {
30 private String address;
31 private NioEventLoopGroup workerGroup;
32 private NioEventLoopGroup bossGroup;
33 private static final Logger LOGGER = LoggerFactory.getLogger(TcpHandler.class);
34 private SettableFuture<Boolean> isOnlineFuture;
37 private PublishingChannelInitializer channelInitializer;
40 * Enum used for storing names of used components (in pipeline).
42 public static enum COMPONENT_NAMES {
45 * First component in pipeline - detecting TLS connections
49 * Component for handling TLS frames
53 * Decodes incoming messages into message frames
57 * Detects version of incoming OpenFlow Protocol message
61 * Transforms OpenFlow Protocol messages
65 * Communicates with upper layers (outside OF Library)
72 * Constructor of TCPHandler that listens on selected port.
74 * @param port listening port of TCPHandler server
76 public TcpHandler(int port) {
78 channelInitializer = new PublishingChannelInitializer();
79 isOnlineFuture = SettableFuture.create();
83 * Starts server on selected port.
87 LOGGER.info("Switch ");
88 bossGroup = new NioEventLoopGroup();
89 workerGroup = new NioEventLoopGroup();
91 ServerBootstrap b = new ServerBootstrap();
92 b.group(bossGroup, workerGroup)
93 .channel(NioServerSocketChannel.class)
94 .handler(new LoggingHandler(LogLevel.DEBUG))
95 .childHandler(channelInitializer)
96 .option(ChannelOption.SO_BACKLOG, 128)
97 .childOption(ChannelOption.SO_KEEPALIVE, true);
99 ChannelFuture f = b.bind(port).sync();
101 InetSocketAddress isa = (InetSocketAddress) f.channel().localAddress();
102 address = isa.getHostName().toString();
103 port = isa.getPort();
104 isOnlineFuture.set(true);
105 LOGGER.info("Switch listener started and ready to accept incoming connections on port: " + port);
106 f.channel().closeFuture().sync();
107 } catch (InterruptedException ex) {
108 LOGGER.error(ex.getMessage(), ex);
115 * Shuts down {@link TcpHandler}}
118 public ListenableFuture<Boolean> shutdown() {
119 final SettableFuture<Boolean> result = SettableFuture.create();
120 workerGroup.shutdownGracefully();
121 // boss will shutdown as soon, as worker is down
122 bossGroup.shutdownGracefully().addListener(new GenericFutureListener<io.netty.util.concurrent.Future<Object>>() {
125 public void operationComplete(
126 io.netty.util.concurrent.Future<Object> downResult) throws Exception {
127 result.set(downResult.isSuccess());
128 result.setException(downResult.cause());
137 * @return number of connected clients / channels
139 public int getNumberOfConnections() {
140 return channelInitializer.size();
144 * @return channelInitializer providing channels
146 public PublishingChannelInitializer getChannelInitializer() {
147 return channelInitializer;
151 * Sets and starts TCPHandler.
156 public static void main(String[] args) throws Exception {
158 if (args.length > 0) {
159 port = Integer.parseInt(args[0]);
163 new Thread(new TcpHandler(port)).start();
167 public ListenableFuture<Boolean> getIsOnlineFuture() {
168 return isOnlineFuture;
174 public int getPort() {
179 * @return the address
181 public String getAddress() {