Merge "Added documentation for web socket client"
[yangtools.git] / websocket / websocket-client / src / main / java / org / opendaylight / yangtools / websocket / client / WebSocketIClient.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.websocket.client;
9
10 import io.netty.bootstrap.Bootstrap;
11 import io.netty.buffer.Unpooled;
12 import io.netty.channel.Channel;
13 import io.netty.channel.ChannelInitializer;
14 import io.netty.channel.ChannelPipeline;
15 import io.netty.channel.EventLoopGroup;
16 import io.netty.channel.nio.NioEventLoopGroup;
17 import io.netty.channel.socket.SocketChannel;
18 import io.netty.channel.socket.nio.NioSocketChannel;
19 import io.netty.handler.codec.http.HttpClientCodec;
20 import io.netty.handler.codec.http.HttpObjectAggregator;
21 import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
22 import io.netty.handler.codec.http.websocketx.PingWebSocketFrame;
23 import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
24 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
25 import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
26 import io.netty.handler.codec.http.websocketx.WebSocketVersion;
27
28 import java.net.URI;
29
30 import org.opendaylight.yangtools.websocket.client.callback.ClientMessageCallback;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Implementation of web socket client that supports WS and HTTP protocols.
36  */
37 public class WebSocketIClient {
38
39     private final URI uri;
40     private Bootstrap bootstrap = new Bootstrap();;
41     private final WebSocketClientHandler clientHandler;
42     private static final Logger logger = LoggerFactory
43             .getLogger(WebSocketIClient.class);
44     private Channel clientChannel;
45     private final EventLoopGroup group = new NioEventLoopGroup();
46
47     /**
48      * Creates new web socket client
49      * 
50      * @param uri
51      *            URI
52      * @param clientMessageCallback
53      *            ClientMessageCallback
54      */
55     public WebSocketIClient(URI uri, ClientMessageCallback clientMessageCallback) {
56         this.uri = uri;
57         clientHandler = new WebSocketClientHandler(
58                 WebSocketClientHandshakerFactory.newHandshaker(uri,
59                         WebSocketVersion.V13, null, false, null),
60                 clientMessageCallback); // last null could be replaced with
61                                         // DefaultHttpHeaders
62         initialize();
63     }
64
65     /**
66      * Initializes {@link Channel} one when it was registered to its
67      * {@link EventLoop}.
68      */
69     private void initialize() {
70
71         String protocol = uri.getScheme();
72         if (!"ws".equals(protocol) && !"http".equals(protocol)) {
73             throw new IllegalArgumentException("Unsupported protocol: "
74                     + protocol);
75         }
76
77         bootstrap.group(group).channel(NioSocketChannel.class)
78                 .handler(new ChannelInitializer<SocketChannel>() {
79                     @Override
80                     public void initChannel(SocketChannel ch) throws Exception {
81                         ChannelPipeline pipeline = ch.pipeline();
82                         pipeline.addLast("http-codec", new HttpClientCodec());
83                         pipeline.addLast("aggregator",
84                                 new HttpObjectAggregator(8192));
85                         pipeline.addLast("ws-handler", clientHandler);
86                     }
87                 });
88     }
89
90     /**
91      * Makes the connection attempt and notifies when the handshake process
92      * succeeds or fail.
93      */
94     public void connect() throws InterruptedException {
95         clientChannel = bootstrap.connect(uri.getHost(), uri.getPort()).sync()
96                 .channel();
97         clientHandler.handshakeFuture().sync();
98     }
99
100     /**
101      * Writes a String message via {@link ChannelOutboundInvoker} through the
102      * {@link ChannelPipeline} and request to actual {@link #flush()} to flush
103      * all pending data to the actual transport.
104      * 
105      * @param message
106      *            a message to write
107      */
108     public void writeAndFlush(String message) {
109         clientChannel.writeAndFlush(new TextWebSocketFrame(message));
110     }
111
112     /**
113      * Writes a Object message via {@link ChannelOutboundInvoker} through the
114      * {@link ChannelPipeline} and request to actual {@link #flush()} to flush
115      * all pending data to the actual transport.
116      * 
117      * @param message
118      *            a message to write
119      */
120     public void writeAndFlush(Object message) {
121         clientChannel.writeAndFlush(message);
122     }
123
124     /**
125      * Writes {@link PingWebSocketFrame} via {@link ChannelOutboundInvoker}
126      * through the {@link ChannelPipeline} and request to actual
127      * {@link #flush()} to flush all pending data to the actual transport.
128      */
129     public void ping() {
130         clientChannel.writeAndFlush(new PingWebSocketFrame(Unpooled
131                 .copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
132     }
133
134     /**
135      * Closes the connection when the server responds to the
136      * {@link CloseWebSocketFrame}.
137      */
138     public void close() throws InterruptedException {
139         clientChannel.writeAndFlush(new CloseWebSocketFrame());
140         clientChannel.closeFuture().sync();
141         group.shutdownGracefully();
142     }
143
144 }