Coverage Report - org.apache.giraph.comm.RequestServerHandler
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestServerHandler
80%
16/20
50%
3/6
1.8
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements.  See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership.  The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License.  You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 package org.apache.giraph.comm;
 20  
 
 21  
 import org.apache.hadoop.io.Writable;
 22  
 import org.apache.hadoop.io.WritableComparable;
 23  
 import org.apache.log4j.Logger;
 24  
 import org.jboss.netty.buffer.ChannelBuffer;
 25  
 import org.jboss.netty.buffer.ChannelBuffers;
 26  
 import org.jboss.netty.channel.ChannelHandlerContext;
 27  
 import org.jboss.netty.channel.ChannelStateEvent;
 28  
 import org.jboss.netty.channel.ExceptionEvent;
 29  
 import org.jboss.netty.channel.MessageEvent;
 30  
 import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
 31  
 
 32  
 /**
 33  
  * Generic handler of requests.
 34  
  *
 35  
  * @param <I> Vertex id
 36  
  * @param <V> Vertex data
 37  
  * @param <E> Edge data
 38  
  * @param <M> Message data
 39  
  */
 40  
 @SuppressWarnings("rawtypes")
 41  
 public class RequestServerHandler<I extends WritableComparable,
 42  
     V extends Writable, E extends Writable,
 43  
     M extends Writable> extends SimpleChannelUpstreamHandler {
 44  
   /** Class logger */
 45  1
   private static final Logger LOG =
 46  
       Logger.getLogger(RequestServerHandler.class);
 47  
   /** Data that can be accessed for handling requests */
 48  
   private final ServerData<I, V, E, M> serverData;
 49  
 
 50  
   /**
 51  
    * Constructor with external server data
 52  
    *
 53  
    * @param serverData Data held by the server
 54  
    */
 55  11
   public RequestServerHandler(ServerData<I, V, E, M> serverData) {
 56  11
     this.serverData = serverData;
 57  11
   }
 58  
 
 59  
   @Override
 60  
   public void messageReceived(
 61  
       ChannelHandlerContext ctx, MessageEvent e) {
 62  3
     if (LOG.isDebugEnabled()) {
 63  0
       LOG.debug("messageReceived: Got " + e.getMessage().getClass());
 64  
     }
 65  
     @SuppressWarnings("unchecked")
 66  3
     WritableRequest<I, V, E, M> writableRequest =
 67  
         (WritableRequest<I, V, E, M>) e.getMessage();
 68  3
     writableRequest.doRequest(serverData);
 69  
 
 70  
     // Send the success response with the request id
 71  3
     ChannelBuffer buffer = ChannelBuffers.directBuffer(9);
 72  3
     buffer.writeLong(writableRequest.getRequestId());
 73  3
     buffer.writeByte(0);
 74  3
     e.getChannel().write(buffer);
 75  3
   }
 76  
 
 77  
   @Override
 78  
   public void channelConnected(ChannelHandlerContext ctx,
 79  
                                ChannelStateEvent e) throws Exception {
 80  11
     if (LOG.isDebugEnabled()) {
 81  0
       LOG.debug("channelConnected: Connected the channel on " +
 82  
           ctx.getChannel().getRemoteAddress());
 83  
     }
 84  11
   }
 85  
 
 86  
   @Override
 87  
   public void channelClosed(ChannelHandlerContext ctx,
 88  
                             ChannelStateEvent e) throws Exception {
 89  11
     if (LOG.isDebugEnabled()) {
 90  0
       LOG.debug("channelClosed: Closed the channel on " +
 91  
           ctx.getChannel().getRemoteAddress());
 92  
     }
 93  11
   }
 94  
 
 95  
   @Override
 96  
   public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
 97  0
     throw new IllegalStateException("exceptionCaught: Channel failed with " +
 98  
         "remote address " + ctx.getChannel().getRemoteAddress(), e.getCause());
 99  
   }
 100  
 }