X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-rest-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fsal%2Frestconf%2Fimpl%2Fwebsockets%2Fclient%2FWebSocketClient.java;h=664aad6578c84fab98a3e569d00fa93dd3a1ee8d;hp=845d54ea1f568388ff6c7ba1400f0e76cac9ccf5;hb=refs%2Fchanges%2F27%2F8927%2F3;hpb=cb210bea2ab44aa2922ed86232c9db9a10452fc0 diff --git a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/client/WebSocketClient.java b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/client/WebSocketClient.java index 845d54ea1f..664aad6578 100644 --- a/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/client/WebSocketClient.java +++ b/opendaylight/md-sal/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/websockets/client/WebSocketClient.java @@ -23,15 +23,13 @@ import io.netty.handler.codec.http.websocketx.PingWebSocketFrame; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory; import io.netty.handler.codec.http.websocketx.WebSocketVersion; - import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URI; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class WebSocketClient { +public class WebSocketClient { private final URI uri; private Bootstrap bootstrap = new Bootstrap();; @@ -40,51 +38,57 @@ public class WebSocketClient { private Channel clientChannel; private final EventLoopGroup group = new NioEventLoopGroup(); - public WebSocketClient(URI uri,IClientMessageCallback clientMessageCallback) { + public WebSocketClient(URI uri, IClientMessageCallback clientMessageCallback) { this.uri = uri; - clientHandler = new WebSocketClientHandler( - WebSocketClientHandshakerFactory.newHandshaker( - uri, WebSocketVersion.V13, null, false,null),clientMessageCallback); // last null could be replaced with DefaultHttpHeaders + clientHandler = new WebSocketClientHandler(WebSocketClientHandshakerFactory.newHandshaker(uri, + WebSocketVersion.V13, null, false, null), clientMessageCallback); // last + // null + // could + // be + // replaced + // with + // DefaultHttpHeaders initialize(); } - private void initialize(){ + + private void initialize() { String protocol = uri.getScheme(); if (!"http".equals(protocol)) { throw new IllegalArgumentException("Unsupported protocol: " + protocol); } - bootstrap.group(group) - .channel(NioSocketChannel.class) - .handler(new ChannelInitializer() { - @Override - public void initChannel(SocketChannel ch) throws Exception { - ChannelPipeline pipeline = ch.pipeline(); - pipeline.addLast("http-codec", new HttpClientCodec()); - pipeline.addLast("aggregator", new HttpObjectAggregator(8192)); - pipeline.addLast("ws-handler", clientHandler); - } - }); + bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer() { + @Override + public void initChannel(SocketChannel ch) throws Exception { + ChannelPipeline pipeline = ch.pipeline(); + pipeline.addLast("http-codec", new HttpClientCodec()); + pipeline.addLast("aggregator", new HttpObjectAggregator(8192)); + pipeline.addLast("ws-handler", clientHandler); + } + }); } - public void connect() throws InterruptedException{ + + public void connect() throws InterruptedException { System.out.println("WebSocket Client connecting"); - clientChannel = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel(); + clientChannel = bootstrap.connect(uri.getHost(), uri.getPort()).sync().channel(); clientHandler.handshakeFuture().sync(); } - public void writeAndFlush(String message){ + public void writeAndFlush(String message) { clientChannel.writeAndFlush(new TextWebSocketFrame(message)); } - public void writeAndFlush(Object message){ + + public void writeAndFlush(Object message) { clientChannel.writeAndFlush(message); } - public void ping(){ - clientChannel.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[]{1, 2, 3, 4, 5, 6}))); + public void ping() { + clientChannel.writeAndFlush(new PingWebSocketFrame(Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 }))); } public void close(String reasonText) throws InterruptedException { - CloseWebSocketFrame closeWebSocketFrame = new CloseWebSocketFrame(1000,reasonText); + CloseWebSocketFrame closeWebSocketFrame = new CloseWebSocketFrame(1000, reasonText); clientChannel.writeAndFlush(closeWebSocketFrame); // WebSocketClientHandler will close the connection when the server @@ -122,7 +126,7 @@ public class WebSocketClient { @Override public void onMessageReceived(Object message) { if (message instanceof TextWebSocketFrame) { - logger.info("received message {}"+ ((TextWebSocketFrame)message).text()); + logger.info("received message {}" + ((TextWebSocketFrame) message).text()); } } }