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.controller.sal.restconf.impl.websockets.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;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
28 private static final Logger logger = LoggerFactory.getLogger(WebSocketClientHandler.class.toString());
29 private final WebSocketClientHandshaker handshaker;
30 private ChannelPromise handshakeFuture;
31 private IClientMessageCallback messageListener;
34 public WebSocketClientHandler(WebSocketClientHandshaker handshaker,IClientMessageCallback listener) {
35 this.handshaker = handshaker;
36 this.messageListener = listener;
39 public ChannelFuture handshakeFuture() {
40 return handshakeFuture;
44 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
45 handshakeFuture = ctx.newPromise();
49 public void channelActive(ChannelHandlerContext ctx) throws Exception {
50 handshaker.handshake(ctx.channel());
54 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
55 logger.info("WebSocket Client disconnected!");
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();
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) + ')');
74 messageListener.onMessageReceived(msg);
75 WebSocketFrame frame = (WebSocketFrame) msg;
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");
86 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
87 cause.printStackTrace();
89 if (!handshakeFuture.isDone()) {
90 handshakeFuture.setFailure(cause);