ClientIOHandler.java

/**
 * Copyright 2016-2018 Dell Inc. or its subsidiaries. All rights reserved.
 *
 * <p>Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file
 * except in compliance with the License. A copy of the License is located at
 *
 * <p>http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 * <p>or in the "license" file accompanying this file. This file 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 com.emc.ecs.nfsclient.network;

import com.emc.ecs.nfsclient.rpc.Xdr;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import java.net.InetSocketAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/** Handles RPC responses and connection termination for one channel. */
public class ClientIOHandler extends SimpleChannelInboundHandler<byte[]> {
  private static final Logger LOG = LoggerFactory.getLogger(ClientIOHandler.class);
  private final Connection connection;

  public ClientIOHandler(Connection connection) {
    this.connection = connection;
  }

  public InetSocketAddress getRemoteAddress() {
    return connection.getRemoteAddress();
  }

  @Override
  public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    connection.close();
    super.channelInactive(ctx);
  }

  @Override
  protected void channelRead0(ChannelHandlerContext ctx, byte[] response) {
    Xdr xdr = RecordMarkingUtil.removeRecordMarking(response);
    connection.notifySender(xdr.getXid(), xdr);
  }

  @Override
  // The channel failure was already logged and delivered to pending RPCs; close is cleanup.
  @SuppressWarnings("FutureReturnValueIgnored")
  public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    if (cause instanceof java.net.BindException) {
      // Privileged-port probing handles bind failures and reports exhaustion itself.
      LOG.debug(
          "Local TCP bind attempt failed: remote={}, local={}",
          getRemoteAddress(),
          ctx.channel().localAddress(),
          cause);
      return;
    }
    LOG.warn(
        "TCP channel failed: remote={}, local={}, pending RPCs will fail: {}",
        getRemoteAddress(),
        ctx.channel().localAddress(),
        cause.toString(),
        cause);
    connection.close(cause);
    ctx.close();
  }
}