2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.websocket.client;
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;
30 import org.opendaylight.yangtools.websocket.client.callback.ClientMessageCallback;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Implementation of web socket client that supports WS and HTTP protocols.
37 public class WebSocketIClient {
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();
48 * Creates new web socket client
52 * @param clientMessageCallback
53 * ClientMessageCallback
55 public WebSocketIClient(URI uri, ClientMessageCallback clientMessageCallback) {
57 clientHandler = new WebSocketClientHandler(
58 WebSocketClientHandshakerFactory.newHandshaker(uri,
59 WebSocketVersion.V13, null, false, null),
60 clientMessageCallback); // last null could be replaced with
66 * Initializes {@link Channel} one when it was registered to its
69 private void initialize() {
71 String protocol = uri.getScheme();
72 if (!"ws".equals(protocol) && !"http".equals(protocol)) {
73 throw new IllegalArgumentException("Unsupported protocol: "
77 bootstrap.group(group).channel(NioSocketChannel.class)
78 .handler(new ChannelInitializer<SocketChannel>() {
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);
91 * Makes the connection attempt and notifies when the handshake process
94 public void connect() throws InterruptedException {
95 clientChannel = bootstrap.connect(uri.getHost(), uri.getPort()).sync()
97 clientHandler.handshakeFuture().sync();
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.
108 public void writeAndFlush(String message) {
109 clientChannel.writeAndFlush(new TextWebSocketFrame(message));
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.
120 public void writeAndFlush(Object message) {
121 clientChannel.writeAndFlush(message);
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.
130 clientChannel.writeAndFlush(new PingWebSocketFrame(Unpooled
131 .copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
135 * Closes the connection when the server responds to the
136 * {@link CloseWebSocketFrame}.
138 public void close() throws InterruptedException {
139 clientChannel.writeAndFlush(new CloseWebSocketFrame());
140 clientChannel.closeFuture().sync();
141 group.shutdownGracefully();