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.opendaylight.yangtools.websocket.client;
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;
27 * {@link WebSocketClientHandler} is implementation of
28 * {@link SimpleChannelInboundHandler} which handle {@link TextWebSocketFrame},
29 * {@link PongWebSocketFrame} and {@link CloseWebSocketFrame} messages.
31 * @deprecated This code is deprecated without replacement.
34 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
36 private static final Logger LOGGER = LoggerFactory.getLogger(WebSocketClientHandler.class.toString());
37 private final WebSocketClientHandshaker handshaker;
38 private final ClientMessageCallback messageListener;
40 private ChannelPromise handshakeFuture;
43 * Create new Web Socket Client Handler.
46 * manages handshake process
51 public WebSocketClientHandler(final WebSocketClientHandshaker handshaker, final ClientMessageCallback listener) {
52 this.handshaker = handshaker;
53 this.messageListener = listener;
57 * Notifies by Future when handshake process succeeds or fails.
59 * @return information about the completation of the handshake
61 public ChannelFuture handshakeFuture() {
62 return handshakeFuture;
66 public void handlerAdded(final ChannelHandlerContext ctx) throws Exception {
67 handshakeFuture = ctx.newPromise();
71 public void channelActive(final ChannelHandlerContext ctx) throws Exception {
72 handshaker.handshake(ctx.channel());
76 public void channelInactive(final ChannelHandlerContext ctx) throws Exception {
77 LOGGER.info("WebSocket Client disconnected!");
81 public void channelRead0(final ChannelHandlerContext ctx, final Object msg)
83 Channel ch = ctx.channel();
84 if (!handshaker.isHandshakeComplete()) {
85 handshaker.finishHandshake(ch, (FullHttpResponse) msg);
86 LOGGER.info("WebSocket Client connected!");
87 handshakeFuture.setSuccess();
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)
100 messageListener.onMessageReceived(msg);
101 WebSocketFrame frame = (WebSocketFrame) msg;
103 if (frame instanceof TextWebSocketFrame) {
104 TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
105 LOGGER.info("WebSocket Client received message: "
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");
116 public void exceptionCaught(final ChannelHandlerContext ctx, final Throwable cause)
118 if (!handshakeFuture.isDone()) {
119 handshakeFuture.setFailure(cause);