OpenDaylight Controller functional modules.
[controller.git] / opendaylight / protocol_plugins / openflow / src / main / java / org / opendaylight / controller / protocol_plugin / openflow / core / internal / ControllerIO.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.protocol_plugin.openflow.core.internal;
11
12 import java.io.IOException;
13 import java.nio.channels.SelectionKey;
14 import java.nio.channels.Selector;
15 import java.nio.channels.ServerSocketChannel;
16 import java.nio.channels.spi.SelectorProvider;
17 import java.util.Iterator;
18
19 import org.opendaylight.controller.protocol_plugin.openflow.core.IController;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class ControllerIO {
24     private static final Logger logger = LoggerFactory
25             .getLogger(ControllerIO.class);
26     private static Short defaultOpenFlowPort = 6633;
27     private Short openFlowPort;
28     private SelectionKey serverSelectionKey;
29     private IController listener;
30     private ServerSocketChannel serverSocket;
31     private Selector selector;
32     private boolean running;
33     private Thread controllerIOThread;
34
35     public ControllerIO(IController l) {
36         this.listener = l;
37         this.openFlowPort = defaultOpenFlowPort;
38         String portString = System.getProperty("port");
39         if (portString != null) {
40             try {
41                 openFlowPort = Short.decode(portString).shortValue();
42             } catch (NumberFormatException e) {
43                 logger.warn("Invalid port:" + portString + ", use default("
44                         + openFlowPort + ")");
45             }
46         }
47     }
48
49     public void start() throws IOException {
50         this.running = true;
51         // obtain a selector
52         this.selector = SelectorProvider.provider().openSelector();
53         // create the listening socket
54         this.serverSocket = ServerSocketChannel.open();
55         this.serverSocket.configureBlocking(false);
56         this.serverSocket.socket().bind(
57                 new java.net.InetSocketAddress(openFlowPort));
58         this.serverSocket.socket().setReuseAddress(true);
59         // register this socket for accepting incoming connections
60         this.serverSelectionKey = this.serverSocket.register(selector,
61                 SelectionKey.OP_ACCEPT);
62         controllerIOThread = new Thread(new Runnable() {
63             @Override
64             public void run() {
65                 while (running) {
66                     try {
67                         // wait for an incoming connection
68                         selector.select(0);
69                         Iterator<SelectionKey> selectedKeys = selector
70                                 .selectedKeys().iterator();
71                         while (selectedKeys.hasNext()) {
72                             SelectionKey skey = selectedKeys.next();
73                             selectedKeys.remove();
74                             if (skey.isValid() && skey.isAcceptable()) {
75                                  ((Controller) listener).handleNewConnection(selector,
76                                         serverSelectionKey);
77                             }
78                         }
79                     } catch (IOException e) {
80                         logger.error("Caught I/O Exception: " + e.toString());
81                         return;
82                     }
83                 }
84             }
85         }, "ControllerI/O Thread");
86         controllerIOThread.start();
87         logger.info("Controller is now listening on port " + openFlowPort);
88     }
89
90     public void shutDown() {
91         this.running = false;
92         this.selector.wakeup();
93         try {
94             this.serverSocket.close();
95         } catch (IOException e) {
96             e.printStackTrace();
97         }
98     }
99 }