/**
* Helios, OpenSource Monitoring
* Brought to you by the Helios Development Group
*
* Copyright 2007, Helios Development Group and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*
*/
package org.helios.apmrouter.wsclient;
import java.net.SocketAddress;
import java.util.concurrent.atomic.AtomicReference;
import org.helios.apmrouter.util.SimpleLogger;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
import org.jboss.netty.handler.codec.http.HttpResponse;
import org.jboss.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
import org.jboss.netty.handler.codec.http.websocketx.WebSocketFrame;
import org.jboss.netty.util.CharsetUtil;
/**
* <p>Title: WebSocketClientHandler</p>
* <p>Description: WebSocket client handler</p>
* <p>Company: Helios Development Group LLC</p>
* @author Whitehead (nwhitehead AT heliosdev DOT org)
* <p><code>org.helios.apmrouter.wsclient.WebSocketClientHandler</code></p>
*/
public class WebSocketClientHandler extends SimpleChannelUpstreamHandler {
/** The handshaker for this websocket client handler */
protected final WebSocketClientHandshaker handshaker;
/** A reference to the remote socket address for this handler */
protected final AtomicReference<SocketAddress> remoteSocketAddress = new AtomicReference<SocketAddress>(null);
/**
* Creates a new WebSocketClientHandler
* @param handshaker The handshaker for this websocket client handler
*/
public WebSocketClientHandler(WebSocketClientHandshaker handshaker) {
this.handshaker = handshaker;
}
/**
* Returns the remote socket address for this handler
* @return the remote socket address for this handler
*/
public SocketAddress getRemoteSocketAddress() {
return remoteSocketAddress.get();
}
/**
* {@inheritDoc}
* @see org.jboss.netty.channel.SimpleChannelUpstreamHandler#messageReceived(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.MessageEvent)
*/
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
Channel ch = ctx.getChannel();
if (!handshaker.isHandshakeComplete()) {
handshaker.finishHandshake(ch, (HttpResponse) e.getMessage());
SimpleLogger.debug("WebSocketClient connected [" , e.getRemoteAddress() , "]");
return;
}
if (e.getMessage() instanceof HttpResponse) {
HttpResponse response = (HttpResponse) e.getMessage();
throw new Exception("Unexpected HttpResponse (status=" + response.getStatus() + ", content="
+ response.getContent().toString(CharsetUtil.UTF_8) + ")");
}
WebSocketFrame frame = (WebSocketFrame) e.getMessage();
if (frame instanceof TextWebSocketFrame) {
TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
//SimpleLogger.debug("WebSocket Client received message: [" ,textFrame.getText(), "]");
ctx.sendUpstream(e);
} else if (frame instanceof PongWebSocketFrame) {
SimpleLogger.debug("WebSocket Client received pong from [", e.getRemoteAddress() , "]");
} else if (frame instanceof CloseWebSocketFrame) {
SimpleLogger.debug("WebSocket Client Closing Connection To [", e.getRemoteAddress() , "]");
ch.close();
}
}
/**
* {@inheritDoc}
* @see org.jboss.netty.channel.SimpleChannelUpstreamHandler#exceptionCaught(org.jboss.netty.channel.ChannelHandlerContext, org.jboss.netty.channel.ExceptionEvent)
*/
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
final Throwable t = e.getCause();
//t.printStackTrace();
e.getChannel().close();
}
}