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.WebSocketClientHandshaker;
19 import io.netty.handler.codec.http.websocketx.WebSocketFrame;
20 import io.netty.util.CharsetUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
25 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
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;
33 public WebSocketClientHandler(WebSocketClientHandshaker handshaker,IClientMessageCallback listener) {
34 this.handshaker = handshaker;
35 this.messageListener = listener;
38 public ChannelFuture handshakeFuture() {
39 return handshakeFuture;
43 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
44 handshakeFuture = ctx.newPromise();
48 public void channelActive(ChannelHandlerContext ctx) throws Exception {
49 handshaker.handshake(ctx.channel());
53 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
54 logger.info("WebSocket Client disconnected!");
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();
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) + ')');
73 messageListener.onMessageReceived(msg);
74 WebSocketFrame frame = (WebSocketFrame) msg;
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");
85 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
86 cause.printStackTrace();
88 if (!handshakeFuture.isDone()) {
89 handshakeFuture.setFailure(cause);