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 package org.apache.hadoop.hdfs.net; 19 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.io.OutputStream; 23 import java.net.Socket; 24 import java.nio.channels.ReadableByteChannel; 25 26 import org.apache.hadoop.net.SocketInputStream; 27 import org.apache.hadoop.net.SocketOutputStream; 28 import org.apache.hadoop.net.unix.DomainSocket; 29 30 /** 31 * Represents a peer that we communicate with by using non-blocking I/O 32 * on a Socket. 33 */ 34 class NioInetPeer implements Peer { 35 private final Socket socket; 36 37 /** 38 * An InputStream which simulates blocking I/O with timeouts using NIO. 39 */ 40 private final SocketInputStream in; 41 42 /** 43 * An OutputStream which simulates blocking I/O with timeouts using NIO. 44 */ 45 private final SocketOutputStream out; 46 47 private final boolean isLocal; 48 NioInetPeer(Socket socket)49 NioInetPeer(Socket socket) throws IOException { 50 this.socket = socket; 51 this.in = new SocketInputStream(socket.getChannel(), 0); 52 this.out = new SocketOutputStream(socket.getChannel(), 0); 53 this.isLocal = socket.getInetAddress().equals(socket.getLocalAddress()); 54 } 55 56 @Override getInputStreamChannel()57 public ReadableByteChannel getInputStreamChannel() { 58 return in; 59 } 60 61 @Override setReadTimeout(int timeoutMs)62 public void setReadTimeout(int timeoutMs) throws IOException { 63 in.setTimeout(timeoutMs); 64 } 65 66 @Override getReceiveBufferSize()67 public int getReceiveBufferSize() throws IOException { 68 return socket.getReceiveBufferSize(); 69 } 70 71 @Override getTcpNoDelay()72 public boolean getTcpNoDelay() throws IOException { 73 return socket.getTcpNoDelay(); 74 } 75 76 @Override setWriteTimeout(int timeoutMs)77 public void setWriteTimeout(int timeoutMs) throws IOException { 78 out.setTimeout(timeoutMs); 79 } 80 81 @Override isClosed()82 public boolean isClosed() { 83 return socket.isClosed(); 84 } 85 86 @Override close()87 public void close() throws IOException { 88 // We always close the outermost streams-- in this case, 'in' and 'out' 89 // Closing either one of these will also close the Socket. 90 try { 91 in.close(); 92 } finally { 93 out.close(); 94 } 95 } 96 97 @Override getRemoteAddressString()98 public String getRemoteAddressString() { 99 return socket.getRemoteSocketAddress().toString(); 100 } 101 102 @Override getLocalAddressString()103 public String getLocalAddressString() { 104 return socket.getLocalSocketAddress().toString(); 105 } 106 107 @Override getInputStream()108 public InputStream getInputStream() throws IOException { 109 return in; 110 } 111 112 @Override getOutputStream()113 public OutputStream getOutputStream() throws IOException { 114 return out; 115 } 116 117 @Override isLocal()118 public boolean isLocal() { 119 return isLocal; 120 } 121 122 @Override toString()123 public String toString() { 124 return "NioInetPeer(" + socket.toString() + ")"; 125 } 126 127 @Override getDomainSocket()128 public DomainSocket getDomainSocket() { 129 return null; 130 } 131 132 @Override hasSecureChannel()133 public boolean hasSecureChannel() { 134 return false; 135 } 136 } 137