ConnectionTrackingHandler.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Google Inc.
00003  * 
00004  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
00005  * use this file except in compliance with the License. You may obtain a copy of
00006  * the License at
00007  * 
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
00013  * License for the specific language governing permissions and limitations under
00014  * the License.
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       // NOTE(damonkohler): We ignore exceptions here because they are common
00072       // (e.g. network failure, connection reset by peer, shutting down, etc.)
00073       // and should not be fatal. However, in all cases the channel should be
00074       // closed.
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 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49