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.onap.ccsdk.sli.plugins.fabricdiscovery;
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.TextWebSocketFrame;
24 import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
25 import io.netty.handler.codec.http.websocketx.WebSocketVersion;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class WebSocketClient {
32 private static final Logger LOG = LoggerFactory.getLogger(WebSocketClient.class);
34 private final URI uri;
35 private final Bootstrap bootstrap = new Bootstrap();
36 private final WebSocketClientHandler clientHandler;
37 private Channel clientChannel;
38 private final EventLoopGroup group = new NioEventLoopGroup();
40 public WebSocketClient(final URI uri, final IClientMessageCallback clientMessageCallback) {
42 clientHandler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(uri,
43 WebSocketVersion.V13, null, false, null), clientMessageCallback);
44 // last null could be replaced with DefaultHttpHeaders
48 public void initialize() {
50 String protocol = uri.getScheme();
51 if (!"ws".equals(protocol)) {
52 throw new IllegalArgumentException("Unsupported protocol: " + protocol);
55 bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<SocketChannel>() {
57 public void initChannel(final SocketChannel ch) throws Exception {
58 ChannelPipeline pipeline = ch.pipeline();
59 pipeline.addLast("http-codec", new HttpClientCodec());
60 pipeline.addLast("aggregator", new HttpObjectAggregator(8192));
61 pipeline.addLast("ws-handler", clientHandler);
66 public void connect() throws InterruptedException {
67 LOG.info("WebSocket Client connecting");
68 clientChannel = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel();
69 clientHandler.handshakeFuture().sync();
72 public void writeAndFlush(final String message) {
73 clientChannel.writeAndFlush(new TextWebSocketFrame(message));
76 public void writeAndFlush(final Object message) {
77 clientChannel.writeAndFlush(message);
81 clientChannel.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 })));
84 public void close(final String reasonText) throws InterruptedException {
85 CloseWebSocketFrame closeWebSocketFrame = new CloseWebSocketFrame(1000, reasonText);
86 clientChannel.writeAndFlush(closeWebSocketFrame);
88 // WebSocketClientHandler will close the connection when the server
89 // responds to the CloseWebSocketFrame.
90 clientChannel.closeFuture().sync();
91 group.shutdownGracefully();