Added resource /streams/stream/<streamName>
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / main / java / org / opendaylight / controller / sal / streams / websockets / WebSocketServer.java
1 package org.opendaylight.controller.sal.streams.websockets;
2
3 import org.opendaylight.controller.sal.streams.listeners.Notificator;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6
7 import io.netty.bootstrap.ServerBootstrap;
8 import io.netty.channel.Channel;
9 import io.netty.channel.EventLoopGroup;
10 import io.netty.channel.nio.NioEventLoopGroup;
11 import io.netty.channel.socket.nio.NioServerSocketChannel;
12
13 public class WebSocketServer implements Runnable {
14
15     private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class);
16
17     public static final int PORT = 8181;
18     private EventLoopGroup bossGroup;
19     private EventLoopGroup workerGroup;
20
21     @Override
22     public void run() {
23         bossGroup = new NioEventLoopGroup();
24         workerGroup = new NioEventLoopGroup();
25         try {
26             ServerBootstrap b = new ServerBootstrap();
27             b.group(bossGroup, workerGroup)
28                 .channel(NioServerSocketChannel.class)
29                 .childHandler(new WebSocketServerInitializer());
30
31             Channel ch = b.bind(PORT).sync().channel();
32             logger.info("Web socket server started at port {}.", PORT);
33
34             ch.closeFuture().sync();
35         } catch (InterruptedException e) {
36             // NOOP
37         } finally {
38             stop();
39         }
40     }
41
42     private void stop() {
43         Notificator.removeAllListeners();
44         if (bossGroup != null) {
45             bossGroup.shutdownGracefully();
46         }
47         if (workerGroup != null) {
48             workerGroup.shutdownGracefully();
49         }
50     }
51
52 }