Code example for Socket

Methods: getChannel, getInputStream

0
   *         the JVM default socket factory if the configuration does not 
   *         contain a default socket factory property. 
   */ 
  public static SocketFactory getDefaultSocketFactory(Configuration conf) {
 
    String propValue = conf.get("hadoop.rpc.socket.factory.class.default");
    if ((propValue == null) || (propValue.length() == 0))
      return SocketFactory.getDefault();
 
    return getSocketFactoryFromProperty(conf, propValue);
  } 
 
  /** 
   * Get the socket factory corresponding to the given proxy URI. If the 
   * given proxy URI corresponds to an absence of configuration parameter, 
   * returns null. If the URI is malformed raises an exception. 
   *  
   * @param propValue the property which is the class name of the 
   *        SocketFactory to instantiate; assumed non null and non empty. 
   * @return a socket factory as defined in the property value. 
   */ 
  public static SocketFactory getSocketFactoryFromProperty(
      Configuration conf, String propValue) {
 
    try { 
      Class<?> theClass = conf.getClassByName(propValue);
      return (SocketFactory) ReflectionUtils.newInstance(theClass, conf);
 
    } catch (ClassNotFoundException cnfe) {
      throw new RuntimeException("Socket Factory class not found: " + cnfe);
    } 
  } 
 
  /** 
   * Util method to build socket addr from either: 
   *   <host>:<post> 
   *   <fs>://<host>:<port>/<path> 
   */ 
  public static InetSocketAddress createSocketAddr(String target) {
    return createSocketAddr(target, -1);
  } 
 
  /** 
   * Util method to build socket addr from either: 
   *   <host> 
   *   <host>:<post> 
   *   <fs>://<host>:<port>/<path> 
   */ 
  public static InetSocketAddress createSocketAddr(String target,
                                                   int defaultPort) {
    int colonIndex = target.indexOf(':');
    if (colonIndex < 0 && defaultPort == -1) {
      throw new RuntimeException("Not a host:port pair: " + target);
    } 
    String hostname;
    int port = -1;
    if (!target.contains("/")) {
      if (colonIndex == -1) {
        hostname = target;
      } else { 
        // must be the old style <host>:<port> 
        hostname = target.substring(0, colonIndex);
        port = Integer.parseInt(target.substring(colonIndex + 1));
      } 
    } else { 
      // a new uri 
      URI addr = new Path(target).toUri();
      hostname = addr.getHost();
      port = addr.getPort();
    } 
 
    if (port == -1) {
      port = defaultPort;
    } 
   
    if (getStaticResolution(hostname) != null) {
      hostname = getStaticResolution(hostname);
    } 
    return new InetSocketAddress(hostname, port);
  } 
 
  /** 
   * Handle the transition from pairs of attributes specifying a host and port 
   * to a single colon separated one. 
   * @param conf the configuration to check 
   * @param oldBindAddressName the old address attribute name 
   * @param oldPortName the old port attribute name 
   * @param newBindAddressName the new combined name 
   * @return the complete address from the configuration 
   */ 
  @Deprecated 
  public static String getServerAddress(Configuration conf,
                                        String oldBindAddressName,
                                        String oldPortName,
                                        String newBindAddressName) {
    String oldAddr = conf.get(oldBindAddressName);
    String oldPort = conf.get(oldPortName);
    String newAddrPort = conf.get(newBindAddressName);
    if (oldAddr == null && oldPort == null) {
      return newAddrPort;
    } 
    String[] newAddrPortParts = newAddrPort.split(":",2);
    if (newAddrPortParts.length != 2) {
      throw new IllegalArgumentException("Invalid address/port: " + 
                                         newAddrPort);
    } 
    if (oldAddr == null) {
      oldAddr = newAddrPortParts[0];
    } else { 
      LOG.warn("Configuration parameter " + oldBindAddressName +
               " is deprecated. Use " + newBindAddressName + " instead.");
    } 
    if (oldPort == null) {
      oldPort = newAddrPortParts[1];
    } else { 
      LOG.warn("Configuration parameter " + oldPortName +
               " is deprecated. Use " + newBindAddressName + " instead.");      
    } 
    return oldAddr + ":" + oldPort;
  } 
   
  /** 
   * Adds a static resolution for host. This can be used for setting up 
   * hostnames with names that are fake to point to a well known host. For e.g. 
   * in some testcases we require to have daemons with different hostnames 
   * running on the same machine. In order to create connections to these 
   * daemons, one can set up mappings from those hostnames to "localhost". 
   * {@link NetUtils#getStaticResolution(String)} can be used to query for 
   * the actual hostname.  
   * @param host 
   * @param resolvedName 
   */ 
  public static void addStaticResolution(String host, String resolvedName) {
    synchronized (hostToResolved) {
      hostToResolved.put(host, resolvedName);
    } 
  } 
   
  /** 
   * Retrieves the resolved name for the passed host. The resolved name must 
   * have been set earlier using  
   * {@link NetUtils#addStaticResolution(String, String)} 
   * @param host 
   * @return the resolution 
   */ 
  public static String getStaticResolution(String host) {
    synchronized (hostToResolved) {
      return hostToResolved.get(host);
    } 
  } 
   
  /** 
   * This is used to get all the resolutions that were added using 
   * {@link NetUtils#addStaticResolution(String, String)}. The return 
   * value is a List each element of which contains an array of String  
   * of the form String[0]=hostname, String[1]=resolved-hostname 
   * @return the list of resolutions 
   */ 
  public static List <String[]> getAllStaticResolutions() {
    synchronized (hostToResolved) {
      Set <Entry <String, String>>entries = hostToResolved.entrySet();
      if (entries.size() == 0) {
        return null; 
      } 
      List <String[]> l = new ArrayList<String[]>(entries.size());
      for (Entry<String, String> e : entries) {
        l.add(new String[] {e.getKey(), e.getValue()});
      } 
    return l;
    } 
  } 
   
  /** 
   * Returns InetSocketAddress that a client can use to  
   * connect to the server. Server.getListenerAddress() is not correct when 
   * the server binds to "0.0.0.0". This returns "127.0.0.1:port" when 
   * the getListenerAddress() returns "0.0.0.0:port". 
   *  
   * @param server 
   * @return socket address that a client can use to connect to the server. 
   */ 
  public static InetSocketAddress getConnectAddress(Server server) {
    InetSocketAddress addr = server.getListenerAddress();
    if (addr.getAddress().getHostAddress().equals("0.0.0.0")) {
      addr = new InetSocketAddress("127.0.0.1", addr.getPort());
    } 
    return addr;
  } 
   
  /** 
   * Same as getInputStream(socket, socket.getSoTimeout()).<br><br> 
   *  
   * From documentation for {@link #getInputStream(Socket, long)}:<br> 
   * Returns InputStream for the socket. If the socket has an associated 
   * SocketChannel then it returns a  
   * {@link SocketInputStream} with the given timeout. If the socket does not 
   * have a channel, {@link Socket#getInputStream()} is returned. In the later 
   * case, the timeout argument is ignored and the timeout set with  
   * {@link Socket#setSoTimeout(int)} applies for reads.<br><br> 
   * 
   * Any socket created using socket factories returned by {@link NetUtils}, 
   * must use this interface instead of {@link Socket#getInputStream()}. 
   *      
   * @see #getInputStream(Socket, long) 
   *  
   * @param socket 
   * @return InputStream for reading from the socket. 
   * @throws IOException 
   */