451a9d203b7a6544872e71ece37e33f17f06dd05
[yangtools.git] / websocket / websocket-client / src / main / java / org / opendaylight / yangtools / websocket / 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.yangtools.websocket.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 import org.opendaylight.yangtools.websocket.client.callback.ClientMessageCallback;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * {@link WebSocketClientHandler} is implementation of
28  * {@link SimpleChannelInboundHandler} which handle {@link TextWebSocketFrame},
29  * {@link PongWebSocketFrame} and {@link CloseWebSocketFrame} messages.
30  *
31  * @deprecated This code is deprecated without replacement.
32  */
33 @Deprecated
34 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
35
36     private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketClientHandler.class.toString());
37     private final WebSocketClientHandshaker handshaker;
38     private final ClientMessageCallback messageListener;
39
40     private ChannelPromise handshakeFuture;
41
42     /**
43      * Create new Web Socket Client Handler.
44      *
45      * @param handshaker
46      *            manages handshake process
47      * @param listener
48      *
49      *
50      */
51     public WebSocketClientHandler(final WebSocketClientHandshaker handshaker, final ClientMessageCallback listener) {
52         this.handshaker = handshaker;
53         this.messageListener = listener;
54     }
55
56     /**
57      * Notifies by Future when handshake process succeeds or fails.
58      *
59      * @return information about the completation of the handshake
60      */
61     public ChannelFuture handshakeFuture() {
62         return handshakeFuture;
63     }
64
65     @Override
66     public void handlerAdded(final ChannelHandlerContext ctx) throws Exception {
67         handshakeFuture = ctx.newPromise();
68     }
69
70     @Override
71     public void channelActive(final ChannelHandlerContext ctx) throws Exception {
72         handshaker.handshake(ctx.channel());
73     }
74
75     @Override
76     public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
77         LOGGER.info("WebSocket Client disconnected!");
78     }
79
80     @Override
81     public void channelRead0(final ChannelHandlerContext ctx, final Object msg)
82             throws Exception {
83         Channel ch = ctx.channel();
84         if (!handshaker.isHandshakeComplete()) {
85             handshaker.finishHandshake(ch, (FullHttpResponse) msg);
86             LOGGER.info("WebSocket Client connected!");
87             handshakeFuture.setSuccess();
88             return;
89         }
90
91         if (msg instanceof FullHttpResponse) {
92             FullHttpResponse response = (FullHttpResponse) msg;
93             throw new RuntimeException(
94                     "Unexpected FullHttpResponse (getStatus="
95                             + response.getStatus() + ", content="
96                             + response.content().toString(CharsetUtil.UTF_8)
97                             + ')');
98         }
99
100         messageListener.onMessageReceived(msg);
101         WebSocketFrame frame = (WebSocketFrame) msg;
102
103         if (frame instanceof TextWebSocketFrame) {
104             TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
105             LOGGER.info("WebSocket Client received message: "
106                     + textFrame.text());
107         } else if (frame instanceof PongWebSocketFrame) {
108             LOGGER.info("WebSocket Client received pong");
109         } else if (frame instanceof CloseWebSocketFrame) {
110             LOGGER.info("WebSocket Client received closing");
111             ch.close();
112         }
113     }
114
115     @Override
116     public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause)
117             throws Exception {
118         if (!handshakeFuture.isDone()) {
119             handshakeFuture.setFailure(cause);
120         }
121         ctx.close();
122     }
123 }