introducing general purpose websocket client
[yangtools.git] / websocket / websocket-client / src / test / java / org / opendaylight / yangtools / websocket / server / WebSocketServer.java
1 /*
2  * Copyright 2012 The Netty Project
3  *
4  * The Netty Project licenses this file to you under the Apache License,
5  * version 2.0 (the "License"); you may not use this file except in compliance
6  * with the License. You may obtain a copy of the License at:
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */
16 package org.opendaylight.yangtools.websocket.server;
17
18 import io.netty.bootstrap.ServerBootstrap;
19 import io.netty.channel.Channel;
20 import io.netty.channel.EventLoopGroup;
21 import io.netty.channel.nio.NioEventLoopGroup;
22 import io.netty.channel.socket.nio.NioServerSocketChannel;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * A HTTP server which serves Web Socket requests at:
28  *
29  * http://localhost:8080/websocket
30  *
31  * Open your browser at http://localhost:8080/, then the demo page will be loaded and a Web Socket connection will be
32  * made automatically.
33  *
34  * This server illustrates support for the different web socket specification versions and will work with:
35  *
36  * <ul>
37  * <li>Safari 5+ (draft-ietf-hybi-thewebsocketprotocol-00)
38  * <li>Chrome 6-13 (draft-ietf-hybi-thewebsocketprotocol-00)
39  * <li>Chrome 14+ (draft-ietf-hybi-thewebsocketprotocol-10)
40  * <li>Chrome 16+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
41  * <li>Firefox 7+ (draft-ietf-hybi-thewebsocketprotocol-10)
42  * <li>Firefox 11+ (RFC 6455 aka draft-ietf-hybi-thewebsocketprotocol-17)
43  * </ul>
44  */
45 public class WebSocketServer implements Runnable {
46
47     private final int port;
48     private final ServerBootstrap bootstrap = new ServerBootstrap();
49     private final EventLoopGroup bossGroup = new NioEventLoopGroup();
50     private final EventLoopGroup workerGroup = new NioEventLoopGroup();
51     private static final Logger logger = LoggerFactory.getLogger(WebSocketServer.class.toString());
52
53     public WebSocketServer(int port) {
54         this.port = port;
55     }
56
57     public void run(){
58         try {
59             startServer();
60         } catch (Exception e) {
61             logger.info("Exception occured while starting webSocket server {}",e);
62         }
63     }
64     public void startServer() throws Exception {
65         try {
66             bootstrap.group(bossGroup, workerGroup)
67              .channel(NioServerSocketChannel.class)
68              .childHandler(new WebSocketServerInitializer());
69
70             Channel ch = bootstrap.bind(port).sync().channel();
71             logger.info("Web socket server started at port " + port + '.');
72             logger.info("Open your browser and navigate to http://localhost:" + port + '/');
73
74             ch.closeFuture().sync();
75         } finally {
76             bossGroup.shutdownGracefully();
77             workerGroup.shutdownGracefully();
78         }
79     }
80
81
82 }