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;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
26 private static final Logger LOG = LoggerFactory.getLogger(WebSocketClientHandler.class.toString());
27 private final WebSocketClientHandshaker handshaker;
28 private ChannelPromise handshakeFuture;
29 private final IClientMessageCallback messageListener;
31 public WebSocketClientHandler(WebSocketClientHandshaker handshaker, IClientMessageCallback listener) {
32 this.handshaker = handshaker;
33 this.messageListener = listener;
36 public ChannelFuture handshakeFuture() {
37 return handshakeFuture;
41 public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
42 handshakeFuture = ctx.newPromise();
46 public void channelActive(ChannelHandlerContext ctx) throws Exception {
47 handshaker.handshake(ctx.channel());
51 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
52 LOG.info("WebSocket Client disconnected!");
56 public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
57 Channel ch = ctx.channel();
58 if (!handshaker.isHandshakeComplete()) {
59 handshaker.finishHandshake(ch, (FullHttpResponse) msg);
60 LOG.info("WebSocket Client connected!");
61 handshakeFuture.setSuccess();
65 if (msg instanceof FullHttpResponse) {
66 FullHttpResponse response = (FullHttpResponse) msg;
67 throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content="
68 + response.content().toString(CharsetUtil.UTF_8) + ')');
71 messageListener.onMessageReceived(msg);
72 WebSocketFrame frame = (WebSocketFrame) msg;
74 if (frame instanceof PongWebSocketFrame) {
75 LOG.info("WebSocket Client received pong");
76 } else if (frame instanceof CloseWebSocketFrame) {
77 LOG.info("WebSocket Client received closing");
83 public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
84 LOG.info("Cause: {} .", cause.toString());
86 if (!handshakeFuture.isDone()) {
87 handshakeFuture.setFailure(cause);