001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hdfs.net;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023import java.net.Socket;
024import java.nio.channels.ReadableByteChannel;
025
026import org.apache.hadoop.net.SocketInputStream;
027import org.apache.hadoop.net.SocketOutputStream;
028import org.apache.hadoop.net.unix.DomainSocket;
029
030/**
031 * Represents a peer that we communicate with by using non-blocking I/O
032 * on a Socket.
033 */
034public class NioInetPeer implements Peer {
035  private final Socket socket;
036
037  /**
038   * An InputStream which simulates blocking I/O with timeouts using NIO.
039   */
040  private final SocketInputStream in;
041
042  /**
043   * An OutputStream which simulates blocking I/O with timeouts using NIO.
044   */
045  private final SocketOutputStream out;
046
047  private final boolean isLocal;
048
049  public NioInetPeer(Socket socket) throws IOException {
050    this.socket = socket;
051    this.in = new SocketInputStream(socket.getChannel(), 0);
052    this.out = new SocketOutputStream(socket.getChannel(), 0);
053    this.isLocal = socket.getInetAddress().equals(socket.getLocalAddress());
054  }
055
056  @Override
057  public ReadableByteChannel getInputStreamChannel() {
058    return in;
059  }
060
061  @Override
062  public void setReadTimeout(int timeoutMs) throws IOException {
063    in.setTimeout(timeoutMs);
064  }
065
066  @Override
067  public int getReceiveBufferSize() throws IOException {
068    return socket.getReceiveBufferSize();
069  }
070
071  @Override
072  public boolean getTcpNoDelay() throws IOException {
073    return socket.getTcpNoDelay();
074  }
075
076  @Override
077  public void setWriteTimeout(int timeoutMs) throws IOException {
078    out.setTimeout(timeoutMs);
079  }
080
081  @Override
082  public boolean isClosed() {
083    return socket.isClosed();
084  }
085
086  @Override
087  public void close() throws IOException {
088    // We always close the outermost streams-- in this case, 'in' and 'out'
089    // Closing either one of these will also close the Socket.
090    try {
091      in.close();
092    } finally {
093      out.close();
094    }
095  }
096
097  @Override
098  public String getRemoteAddressString() {
099    return socket.getRemoteSocketAddress().toString();
100  }
101
102  @Override
103  public String getLocalAddressString() {
104    return socket.getLocalSocketAddress().toString();
105  }
106
107  @Override
108  public InputStream getInputStream() throws IOException {
109    return in;
110  }
111
112  @Override
113  public OutputStream getOutputStream() throws IOException {
114    return out;
115  }
116
117  @Override
118  public boolean isLocal() {
119    return isLocal;
120  }
121
122  @Override
123  public String toString() {
124    return "NioInetPeer(" + socket.toString() + ")";
125  }
126
127  @Override
128  public DomainSocket getDomainSocket() {
129    return null;
130  }
131
132  @Override
133  public boolean hasSecureChannel() {
134    return false;
135  }
136}