Pavlin Radoslavov | de4c389 | 2014-07-06 23:19:59 -0700 | [diff] [blame] | 1 | package net.onrc.onos.apps.websocket; |
| 2 | |
| 3 | import javax.websocket.DeploymentException; |
| 4 | import javax.websocket.server.ServerContainer; |
| 5 | |
| 6 | import net.onrc.onos.core.topology.ITopologyService; |
| 7 | |
| 8 | import org.eclipse.jetty.server.Server; |
| 9 | import org.eclipse.jetty.servlet.ServletContextHandler; |
| 10 | import org.eclipse.jetty.websocket.jsr356.server.deploy.WebSocketServerContainerInitializer; |
| 11 | import org.slf4j.Logger; |
| 12 | import org.slf4j.LoggerFactory; |
| 13 | |
| 14 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 15 | |
| 16 | /** |
| 17 | * The WebSocket manager class. |
| 18 | * There is a single instance for all WebSocket endpoints. |
| 19 | */ |
| 20 | class WebSocketManager { |
| 21 | protected static ITopologyService topologyService; |
| 22 | |
| 23 | private static final Logger log = |
| 24 | LoggerFactory.getLogger(WebSocketManager.class); |
| 25 | private int webSocketPort; |
| 26 | private JettyServer jettyServer; |
| 27 | |
| 28 | /** |
| 29 | * Constructor. |
| 30 | * |
| 31 | * @param topologyService the Topology Service to use. |
| 32 | * @param webSocketPort the WebSocket port to use. |
| 33 | */ |
| 34 | @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", |
| 35 | justification = "The writing to WebSocketManager.topologyService happens only once on startup") |
| 36 | WebSocketManager(ITopologyService topologyService, |
| 37 | int webSocketPort) { |
| 38 | WebSocketManager.topologyService = topologyService; |
| 39 | this.webSocketPort = webSocketPort; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Startup processing. |
| 44 | */ |
| 45 | void startup() { |
| 46 | log.debug("Starting WebSocket server on port {}", webSocketPort); |
| 47 | |
| 48 | jettyServer = new JettyServer(webSocketPort); |
| 49 | this.jettyServer.start(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Class for creating the WebSocket server and associated state. |
| 54 | */ |
| 55 | static class JettyServer extends Thread { |
| 56 | private Server server; |
| 57 | private ServletContextHandler context; |
| 58 | private ServerContainer container; |
| 59 | |
| 60 | /** |
| 61 | * Constructor. |
| 62 | * |
| 63 | * @param port the port to listen on. |
| 64 | */ |
| 65 | JettyServer(final int port) { |
| 66 | server = new Server(port); |
| 67 | |
| 68 | // Initialize the context handler |
| 69 | context = new ServletContextHandler(ServletContextHandler.SESSIONS); |
| 70 | context.setContextPath("/ws/onos"); |
| 71 | server.setHandler(context); |
| 72 | |
| 73 | // Initialize the WebSocket layer |
| 74 | container = |
| 75 | WebSocketServerContainerInitializer.configureContext(context); |
| 76 | try { |
| 77 | container.addEndpoint(TopologyWebSocket.class); |
| 78 | } catch (DeploymentException e) { |
| 79 | log.debug("Exception adding WebSocket endpoint: ", e); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Run the thread. |
| 85 | */ |
| 86 | @Override |
| 87 | public void run() { |
| 88 | try { |
| 89 | this.server.start(); |
| 90 | } catch (final Exception e) { |
| 91 | log.debug("Exception starting the WebSocket server: ", e); |
| 92 | } |
| 93 | try { |
| 94 | this.server.join(); |
| 95 | } catch (final InterruptedException e) { |
| 96 | log.debug("Exception joining the WebSocket server: ", e); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |