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 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
33 private static final Logger LOGGER = LoggerFactory
34 .getLogger(WebSocketClientHandler.class.toString());
35 private final WebSocketClientHandshaker handshaker;
36 private ChannelPromise handshakeFuture;
37 private ClientMessageCallback messageListener;
40 * Create new Web Socket Client Handler.
43 * manages handshake process
48 public WebSocketClientHandler(WebSocketClientHandshaker handshaker,
49 ClientMessageCallback listener) {
50 this.handshaker = handshaker;
51 this.messageListener = listener;
55 * Notifies by Future when handshake process succeeds or fails.
57 * @return information about the completation of the handshake
59 public ChannelFuture handshakeFuture() {
60 return handshakeFuture;
64 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
65 handshakeFuture = ctx.newPromise();
69 public void channelActive(ChannelHandlerContext ctx) throws Exception {
70 handshaker.handshake(ctx.channel());
74 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
75 LOGGER.info("WebSocket Client disconnected!");
79 public void channelRead0(ChannelHandlerContext ctx, Object msg)
81 Channel ch = ctx.channel();
82 if (!handshaker.isHandshakeComplete()) {
83 handshaker.finishHandshake(ch, (FullHttpResponse) msg);
84 LOGGER.info("WebSocket Client connected!");
85 handshakeFuture.setSuccess();
89 if (msg instanceof FullHttpResponse) {
90 FullHttpResponse response = (FullHttpResponse) msg;
91 throw new RuntimeException(
92 "Unexpected FullHttpResponse (getStatus="
93 + response.getStatus() + ", content="
94 + response.content().toString(CharsetUtil.UTF_8)
98 messageListener.onMessageReceived(msg);
99 WebSocketFrame frame = (WebSocketFrame) msg;
101 if (frame instanceof TextWebSocketFrame) {
102 TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
103 LOGGER.info("WebSocket Client received message: "
105 } else if (frame instanceof PongWebSocketFrame) {
106 LOGGER.info("WebSocket Client received pong");
107 } else if (frame instanceof CloseWebSocketFrame) {
108 LOGGER.info("WebSocket Client received closing");
114 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
116 if (!handshakeFuture.isDone()) {
117 handshakeFuture.setFailure(cause);