Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 package org.ros.internal.transport;
00018
00019 import org.apache.commons.logging.Log;
00020 import org.apache.commons.logging.LogFactory;
00021 import org.jboss.netty.channel.ChannelHandlerContext;
00022 import org.jboss.netty.channel.ChannelStateEvent;
00023 import org.jboss.netty.channel.ExceptionEvent;
00024 import org.jboss.netty.channel.SimpleChannelHandler;
00025 import org.jboss.netty.channel.group.ChannelGroup;
00026 import org.ros.exception.RosRuntimeException;
00027
00028 import java.io.IOException;
00029 import java.nio.channels.Channels;
00030
00036 public class ConnectionTrackingHandler extends SimpleChannelHandler {
00037
00038 private static final boolean DEBUG = false;
00039 private static final Log log = LogFactory.getLog(ConnectionTrackingHandler.class);
00040
00044 private final ChannelGroup channelGroup;
00045
00046 public ConnectionTrackingHandler(ChannelGroup channelGroup) {
00047 this.channelGroup = channelGroup;
00048 }
00049
00050 @Override
00051 public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
00052 if (DEBUG) {
00053 log.info("Channel opened: " + e.getChannel());
00054 }
00055 channelGroup.add(e.getChannel());
00056 super.channelOpen(ctx, e);
00057 }
00058
00059 @Override
00060 public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
00061 if (DEBUG) {
00062 log.info("Channel closed: " + e.getChannel());
00063 }
00064 super.channelClosed(ctx, e);
00065 }
00066
00067 @Override
00068 public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
00069 ctx.getChannel().close();
00070 if (e.getCause() instanceof IOException) {
00071
00072
00073
00074
00075 if (DEBUG) {
00076 log.error("Channel exception: " + ctx.getChannel(), e.getCause());
00077 } else {
00078 log.error("Channel exception: " + e.getCause());
00079 }
00080 } else {
00081 throw new RosRuntimeException(e.getCause());
00082 }
00083 }
00084 }