/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.client.netty;

import java.io.IOException;
import java.util.HashSet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import tachyon.Constants;
import tachyon.network.protocol.RPCBlockResponse;
import tachyon.network.protocol.RPCMessage;
import tachyon.network.protocol.RPCResponse;

/**
 * This handles all the messages received by the client channel.
 */
@ChannelHandler.Sharable
public final class ClientHandler extends SimpleChannelInboundHandler<RPCMessage> {
  private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);

  /**
   * The interface for listeners to implement to receive callbacks when messages are received.
   */
  public interface ResponseListener {
    /** This method will be called when a message is received on the client. */
    void onResponseReceived(RPCResponse response);
  }

  private final HashSet<ResponseListener> mListeners;

  public ClientHandler() {
    mListeners = new HashSet<ResponseListener>();
  }

  public void addListener(ResponseListener listener) {
    mListeners.add(listener);
  }

  public void removeListener(ResponseListener listener) {
    mListeners.remove(listener);
  }

  @Override
  public void channelRead0(final ChannelHandlerContext ctx, final RPCMessage msg)
      throws IOException {
    switch (msg.getType()) {
      case RPC_BLOCK_RESPONSE:
        handleBlockResponse(ctx, (RPCBlockResponse) msg);
        break;
      default:
        throw new IllegalArgumentException("No handler implementation for rpc response type: "
            + msg.getType());
    }
  }

  @Override
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
    LOG.warn("Exception thrown while processing request", cause);
    ctx.close();
  }

  private void handleBlockResponse(final ChannelHandlerContext ctx, final RPCBlockResponse resp)
      throws IOException {
    for (ResponseListener listener : mListeners) {
      listener.onResponseReceived(resp);
    }
  }
}

@@ -1913,19 +1913,20 @@
 stener%3E(
+4
 );%0A
-
   %7D%0A%0A  p
@@ -2237,66 +2237,42 @@
     
-switch (msg.getType()) %7B%0A      case RPC_BLOCK_RESPONSE:%0A  
+if (msg instanceof RPCResponse) %7B%0A
     
@@ -2275,29 +2275,24 @@
       handle
-Block
 Response(ctx
@@ -2297,21 +2297,16 @@
 tx, (RPC
-Block
 Response
@@ -2321,36 +2321,79 @@
     
-    break;%0A      default:%0A  
+%7D else %7B%0A      // The client should only receive RPCResponse messages.%0A
     
@@ -2481,18 +2481,16 @@
 type: %22%0A
-  
         
@@ -2734,21 +2734,16 @@
 d handle
-Block
 Response
@@ -2785,21 +2785,16 @@
 inal RPC
-Block
 Response

handle general responses