Merge "Increase timeout for waiting for broker service in sal-binding-it."
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / websockets / client / WebSocketClientHandler.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.controller.sal.restconf.impl.websockets.client;
9
10 import io.netty.channel.Channel;
11 import io.netty.channel.ChannelFuture;
12 import io.netty.channel.ChannelHandlerContext;
13 import io.netty.channel.ChannelPromise;
14 import io.netty.channel.SimpleChannelInboundHandler;
15 import io.netty.handler.codec.http.FullHttpResponse;
16 import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
17 import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
18 import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
19 import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
20 import io.netty.handler.codec.http.websocketx.WebSocketFrame;
21 import io.netty.util.CharsetUtil;
22
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
27
28     private static final Logger logger = LoggerFactory.getLogger(WebSocketClientHandler.class.toString());
29     private final WebSocketClientHandshaker handshaker;
30     private ChannelPromise handshakeFuture;
31     private IClientMessageCallback messageListener;
32
33
34     public WebSocketClientHandler(WebSocketClientHandshaker handshaker,IClientMessageCallback listener) {
35         this.handshaker = handshaker;
36         this.messageListener = listener;
37     }
38
39     public ChannelFuture handshakeFuture() {
40         return handshakeFuture;
41     }
42
43     @Override
44     public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
45         handshakeFuture = ctx.newPromise();
46     }
47
48     @Override
49     public void channelActive(ChannelHandlerContext ctx) throws Exception {
50         handshaker.handshake(ctx.channel());
51     }
52
53     @Override
54     public void channelInactive(ChannelHandlerContext ctx) throws Exception {
55         logger.info("WebSocket Client disconnected!");
56     }
57
58     @Override
59     public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
60         Channel ch = ctx.channel();
61         if (!handshaker.isHandshakeComplete()) {
62             handshaker.finishHandshake(ch, (FullHttpResponse) msg);
63             logger.info("WebSocket Client connected!");
64             handshakeFuture.setSuccess();
65             return;
66         }
67
68         if (msg instanceof FullHttpResponse) {
69             FullHttpResponse response = (FullHttpResponse) msg;
70             throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content="
71                     + response.content().toString(CharsetUtil.UTF_8) + ')');
72         }
73
74         messageListener.onMessageReceived(msg);
75         WebSocketFrame frame = (WebSocketFrame) msg;
76
77         if (frame instanceof PongWebSocketFrame) {
78             logger.info("WebSocket Client received pong");
79         } else if (frame instanceof CloseWebSocketFrame) {
80             logger.info("WebSocket Client received closing");
81             ch.close();
82         }
83     }
84
85     @Override
86     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
87         cause.printStackTrace();
88
89         if (!handshakeFuture.isDone()) {
90             handshakeFuture.setFailure(cause);
91         }
92
93         ctx.close();
94     }
95 }
96