Take advantage of MultipartTransactionAware
[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.ChannelPipeline;
10 import io.netty.channel.EventLoopGroup;
11 import io.netty.channel.nio.NioEventLoopGroup;
12 import io.netty.channel.socket.nio.NioServerSocketChannel;
13
14 /**
15  * {@link WebSocketServer} is responsible to start and stop web socket server at
16  * {@link #PORT}.
17  */
18 public class WebSocketServer implements Runnable {
19
20         private static final Logger logger = LoggerFactory
21                         .getLogger(WebSocketServer.class);
22
23         public static final int PORT = 8181;
24         private EventLoopGroup bossGroup;
25         private EventLoopGroup workerGroup;
26
27         @Override
28         public void run() {
29                 bossGroup = new NioEventLoopGroup();
30                 workerGroup = new NioEventLoopGroup();
31                 try {
32                         ServerBootstrap b = new ServerBootstrap();
33                         b.group(bossGroup, workerGroup)
34                                         .channel(NioServerSocketChannel.class)
35                                         .childHandler(new WebSocketServerInitializer());
36
37                         Channel ch = b.bind(PORT).sync().channel();
38                         logger.info("Web socket server started at port {}.", PORT);
39
40                         ch.closeFuture().sync();
41                 } catch (InterruptedException e) {
42                         // NOOP
43                 } finally {
44                         stop();
45                 }
46         }
47
48         /**
49          * Stops the web socket server and removes all listeners.
50          */
51         private void stop() {
52                 Notificator.removeAllListeners();
53                 if (bossGroup != null) {
54                         bossGroup.shutdownGracefully();
55                 }
56                 if (workerGroup != null) {
57                         workerGroup.shutdownGracefully();
58                 }
59         }
60
61 }