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 int DEFAULT_PORT = 8181;
20 private EventLoopGroup bossGroup;
21 private EventLoopGroup workerGroup;
22 private static WebSocketServer instance = null;
23 private int port = DEFAULT_PORT;
25 private WebSocketServer(int port) {
30 * Create instance of {@link WebSocketServer}
33 * TCP port used for this server
34 * @return instance of {@link WebSocketServer}
36 public static WebSocketServer createInstance(int port) {
37 Preconditions.checkState(instance == null, "createInstance() has already been called");
38 Preconditions.checkArgument(port > 1024, "Privileged port (below 1024) is not allowed");
40 instance = new WebSocketServer(port);
45 * Return websocket TCP port
47 public int getPort() {
52 * Get instance of {@link WebSocketServer} created by {@link #createInstance(int)}
54 * @return instance of {@link WebSocketServer}
56 public static WebSocketServer getInstance() {
57 Preconditions.checkNotNull(instance, "createInstance() must be called prior to getInstance()");
62 * Destroy this already created instance
64 public static void destroyInstance() {
65 Preconditions.checkState(instance != null, "createInstance() must be called prior to destroyInstance()");
73 bossGroup = new NioEventLoopGroup();
74 workerGroup = new NioEventLoopGroup();
76 ServerBootstrap b = new ServerBootstrap();
77 b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class)
78 .childHandler(new WebSocketServerInitializer());
80 Channel ch = b.bind(port).sync().channel();
81 logger.info("Web socket server started at port {}.", port);
83 ch.closeFuture().sync();
84 } catch (InterruptedException e) {
92 * Stops the web socket server and removes all listeners.
95 Notificator.removeAllListeners();
96 if (bossGroup != null) {
97 bossGroup.shutdownGracefully();
100 if (workerGroup != null) {
101 workerGroup.shutdownGracefully();