MessageReceiver.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2012 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.queue;
00018 
00019 import org.ros.concurrent.CircularBlockingDeque;
00020 
00021 import org.apache.commons.logging.Log;
00022 import org.apache.commons.logging.LogFactory;
00023 import org.jboss.netty.buffer.ChannelBuffer;
00024 import org.jboss.netty.channel.ChannelHandlerContext;
00025 import org.jboss.netty.channel.MessageEvent;
00026 import org.ros.internal.transport.tcp.AbstractNamedChannelHandler;
00027 import org.ros.message.MessageDeserializer;
00028 
00035 public class MessageReceiver<T> extends AbstractNamedChannelHandler {
00036 
00037   private static final boolean DEBUG = false;
00038   private static final Log log = LogFactory.getLog(MessageReceiver.class);
00039 
00040   private final CircularBlockingDeque<LazyMessage<T>> lazyMessages;
00041   private final MessageDeserializer<T> deserializer;
00042 
00043   public MessageReceiver(CircularBlockingDeque<LazyMessage<T>> lazyMessages,
00044       MessageDeserializer<T> deserializer) {
00045     this.lazyMessages = lazyMessages;
00046     this.deserializer = deserializer;
00047   }
00048 
00049   @Override
00050   public String getName() {
00051     return "IncomingMessageQueueChannelHandler";
00052   }
00053 
00054   @Override
00055   public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
00056     ChannelBuffer buffer = (ChannelBuffer) e.getMessage();
00057     if (DEBUG) {
00058       log.info(String.format("Received %d byte message.", buffer.readableBytes()));
00059     }
00060     // We have to make a defensive copy of the buffer here because Netty does
00061     // not guarantee that the returned ChannelBuffer will not be reused.
00062     lazyMessages.addLast(new LazyMessage<T>(buffer.copy(), deserializer));
00063     super.messageReceived(ctx, e);
00064   }
00065 }


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