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