1 package org.opendaylight.controller.sal.streams.websockets;
3 import com.google.common.base.Preconditions;
4 import io.netty.bootstrap.ServerBootstrap;
5 import io.netty.channel.Channel;
6 import io.netty.channel.EventLoopGroup;
7 import io.netty.channel.nio.NioEventLoopGroup;
8 import io.netty.channel.socket.nio.NioServerSocketChannel;
9 import org.opendaylight.controller.sal.streams.listeners.Notificator;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
14 * {@link WebSocketServer} is responsible to start and stop web socket server
16 public class WebSocketServer implements Runnable {
18 private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class);
19 public static final String WEBSOCKET_SERVER_CONFIG_PROPERTY = "restconf.websocket.port";
20 public static final int DEFAULT_PORT = 8181;
21 private EventLoopGroup bossGroup;
22 private EventLoopGroup workerGroup;
23 private static WebSocketServer singleton = null;
24 private int port = DEFAULT_PORT;
26 private WebSocketServer(int port) {
31 * Create instance of {@link WebSocketServer}
34 * TCP port used for this server
35 * @return instance of {@link WebSocketServer}
37 public static WebSocketServer createInstance(int port) {
38 if (singleton != null) {
39 throw new IllegalStateException("createInstance() has already been called");
42 throw new IllegalArgumentException("Privileged port (below 1024) is not allowed");
44 singleton = new WebSocketServer(port);
49 * Return websocket TCP port
51 public int getPort() {
56 * Get instance of {@link WebSocketServer} created by {@link #createInstance(int)}
58 * @return instance of {@link WebSocketServer}
60 public static WebSocketServer getInstance() {
61 Preconditions.checkNotNull(singleton, "createInstance() must be called prior to getInstance()");
66 * Destroy this already created instance
68 public static void destroyInstance() {
69 if (singleton == null) {
70 throw new IllegalStateException("createInstance() must be called prior to destroyInstance()");
77 bossGroup = new NioEventLoopGroup();
78 workerGroup = new NioEventLoopGroup();
80 ServerBootstrap b = new ServerBootstrap();
81 b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
82 .childHandler(new WebSocketServerInitializer());
84 Channel ch = b.bind(port).sync().channel();
85 logger.info("Web socket server started at port {}.", port);
87 ch.closeFuture().sync();
88 } catch (InterruptedException e) {
96 * Stops the web socket server and removes all listeners.
99 Notificator.removeAllListeners();
100 if (bossGroup != null) {
101 bossGroup.shutdownGracefully();
103 if (workerGroup != null) {
104 workerGroup.shutdownGracefully();