This project has retired. For details please refer to its Attic page.
Source code
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,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.wake.remote.impl;
020
021import org.apache.reef.wake.remote.RemoteIdentifier;
022import org.apache.reef.wake.remote.exception.RemoteRuntimeException;
023
024import java.net.InetSocketAddress;
025
026/**
027 * Remote identifier based on a socket address
028 */
029public class SocketRemoteIdentifier implements RemoteIdentifier {
030
031  private InetSocketAddress addr;
032
033  /**
034   * Constructs a remote identifier
035   *
036   * @param addr the socket address
037   */
038  public SocketRemoteIdentifier(InetSocketAddress addr) {
039    this.addr = addr;
040  }
041
042  /**
043   * Constructs a remote identifier
044   *
045   * @param str the string representation
046   * @throws RemoteRuntimeException
047   */
048  public SocketRemoteIdentifier(String str) {
049    int index = str.indexOf("0:0:0:0:0:0:0:0:");
050
051    if (index >= 0) {
052      String host = str.substring(0, 15);
053      int port = Integer.parseInt(str.substring(index + 16));
054      this.addr = new InetSocketAddress(host, port);
055    } else {
056      index = str.indexOf(":");
057      if (index <= 0) {
058        throw new RemoteRuntimeException("Invalid name " + str);
059      }
060      String host = str.substring(0, index);
061      int port = Integer.parseInt(str.substring(index + 1));
062      this.addr = new InetSocketAddress(host, port);
063    }
064  }
065
066  /**
067   * Gets the socket address
068   *
069   * @return the socket address
070   */
071  public InetSocketAddress getSocketAddress() {
072    return addr;
073  }
074
075  /**
076   * Returns a hash code for the object
077   *
078   * @return a hash code value for this object
079   */
080  @Override
081  public int hashCode() {
082    return addr.hashCode();
083  }
084
085  /**
086   * Checks that another object is equal to this object
087   *
088   * @param o another object
089   * @return true if the object is the same as the object argument; false, otherwise
090   */
091  @Override
092  public boolean equals(Object o) {
093    return addr.equals(((SocketRemoteIdentifier) o).getSocketAddress());
094  }
095
096  /**
097   * Returns a string representation of the object
098   *
099   * @return a string representation of the object
100   */
101  @Override
102  public String toString() {
103    StringBuilder builder = new StringBuilder();
104    builder.append("socket://");
105    builder.append(addr.getHostString());
106    builder.append(":");
107    builder.append(addr.getPort());
108    return builder.toString();
109  }
110
111}