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.unix.DomainSocket; 027 028/** 029 * Represents a peer that we communicate with by using a basic Socket 030 * that has no associated Channel. 031 * 032 */ 033public class BasicInetPeer implements Peer { 034 private final Socket socket; 035 private final OutputStream out; 036 private final InputStream in; 037 private final boolean isLocal; 038 039 public BasicInetPeer(Socket socket) throws IOException { 040 this.socket = socket; 041 this.out = socket.getOutputStream(); 042 this.in = socket.getInputStream(); 043 this.isLocal = socket.getInetAddress().equals(socket.getLocalAddress()); 044 } 045 046 @Override 047 public ReadableByteChannel getInputStreamChannel() { 048 /* 049 * This Socket has no channel, so there's nothing to return here. 050 */ 051 return null; 052 } 053 054 @Override 055 public void setReadTimeout(int timeoutMs) throws IOException { 056 socket.setSoTimeout(timeoutMs); 057 } 058 059 @Override 060 public int getReceiveBufferSize() throws IOException { 061 return socket.getReceiveBufferSize(); 062 } 063 064 @Override 065 public boolean getTcpNoDelay() throws IOException { 066 return socket.getTcpNoDelay(); 067 } 068 069 @Override 070 public void setWriteTimeout(int timeoutMs) { 071 /* 072 * We can't implement write timeouts. :( 073 * 074 * Java provides no facility to set a blocking write timeout on a Socket. 075 * You can simulate a blocking write with a timeout by using 076 * non-blocking I/O. However, we can't use nio here, because this Socket 077 * doesn't have an associated Channel. 078 * 079 * See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4031100 for 080 * more details. 081 */ 082 } 083 084 @Override 085 public boolean isClosed() { 086 return socket.isClosed(); 087 } 088 089 @Override 090 public void close() throws IOException { 091 socket.close(); 092 } 093 094 @Override 095 public String getRemoteAddressString() { 096 return socket.getRemoteSocketAddress().toString(); 097 } 098 099 @Override 100 public String getLocalAddressString() { 101 return socket.getLocalSocketAddress().toString(); 102 } 103 104 @Override 105 public InputStream getInputStream() throws IOException { 106 return in; 107 } 108 109 @Override 110 public OutputStream getOutputStream() throws IOException { 111 return out; 112 } 113 114 @Override 115 public boolean isLocal() { 116 return isLocal; 117 } 118 119 @Override 120 public String toString() { 121 return "BasicInetPeer(" + socket.toString() + ")"; 122 } 123 124 @Override 125 public DomainSocket getDomainSocket() { 126 return null; 127 } 128 129 @Override 130 public boolean hasSecureChannel() { 131 return false; 132 } 133}