Renamed packages to org.opendaylight.openflowjava.protocol.impl.*
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / TcpHandler.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
2 package org.opendaylight.openflowjava.protocol.impl.core;
3
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
12 import java.net.InetSocketAddress;
13
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16
17 import com.google.common.util.concurrent.SettableFuture;
18
19 /**
20  * Class implementing server over TCP for handling incoming connections.
21  *
22  * @author michal.polkorab
23  */
24 public class TcpHandler extends Thread {
25
26     private int port;
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;
32     
33     
34     private PublishingChannelInitializer channelInitializer;
35
36     /**
37      * Enum used for storing names of used components (in pipeline).
38      */
39     public static enum COMPONENT_NAMES {
40
41         /**
42          * First component in pipeline - detecting TLS connections
43          */
44         TLS_DETECTOR,
45         /**
46          * Component for handling TLS frames
47          */
48         SSL_HANDLER,
49         /**
50          * Decodes incoming messages into message frames
51          */
52         OF_FRAME_DECODER,
53         /**
54          * Detects version of incoming OpenFlow Protocol message
55          */
56         OF_VERSION_DETECTOR,
57         /**
58          * Transforms OpenFlow Protocol messages
59          */
60         OF_CODEC,
61         /**
62          * Communicates with upper layers (outside OF Library)
63          */
64         OF_FACADE
65     }
66     
67
68     /**
69      * Constructor of TCPHandler that listens on selected port.
70      *
71      * @param port listening port of TCPHandler server
72      */
73     public TcpHandler(int port) {
74         this.port = port;
75         channelInitializer = new PublishingChannelInitializer();
76         isOnlineFuture = SettableFuture.create();
77     }
78
79     /**
80      * Starts server on selected port.
81      */
82     @Override
83     public void run() {
84         LOGGER.info("Switch ");
85         bossGroup = new NioEventLoopGroup();
86         workerGroup = new NioEventLoopGroup();
87         try {
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);
95
96             ChannelFuture f = b.bind(port).sync();
97             
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);
106         } finally {
107             shutdown();
108         }
109     }
110
111     /**
112      * Shuts down {@link TcpHandler}}
113      */
114     public void shutdown() {
115         workerGroup.shutdownGracefully();
116         bossGroup.shutdownGracefully();
117     }
118     
119     /**
120      * 
121      * @return number of connected clients / channels
122      */
123     public int getNumberOfConnections() {
124         return channelInitializer.size();
125     }
126     
127     /**
128      * @return channelInitializer providing channels
129      */
130     public PublishingChannelInitializer getChannelInitializer() {
131         return channelInitializer;
132     }
133     
134     /**
135      * Sets and starts TCPHandler.
136      *
137      * @param args
138      * @throws Exception
139      */
140     public static void main(String[] args) throws Exception {
141         int port;
142         if (args.length > 0) {
143             port = Integer.parseInt(args[0]);
144         } else {
145             port = 6633;
146         }
147         new TcpHandler(port).start();
148     }
149     
150     /**
151      * @return the isOnlineFuture
152      */
153     public SettableFuture<Boolean> getIsOnlineFuture() {
154         return isOnlineFuture;
155     }
156     
157     /**
158      * @return the port
159      */
160     public int getPort() {
161         return port;
162     }
163     
164     /**
165      * @return the address
166      */
167     public String getAddress() {
168         return address;
169     }
170     
171 }