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