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(final 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(final String str) { 049 int index = str.indexOf("0:0:0:0:0:0:0:0:"); 050 051 if (index >= 0) { 052 final String host = str.substring(0, 15); 053 final 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 final String host = str.substring(0, index); 061 final 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(final Object o) { 093 if (this == o) { 094 return true; 095 } 096 if (o == null || getClass() != o.getClass()) { 097 return false; 098 } 099 return addr.equals(((SocketRemoteIdentifier) o).getSocketAddress()); 100 } 101 102 /** 103 * Returns a string representation of the object. 104 * 105 * @return a string representation of the object 106 */ 107 @Override 108 public String toString() { 109 final StringBuilder builder = new StringBuilder(); 110 builder.append("socket://"); 111 builder.append(addr.getHostString()); 112 builder.append(":"); 113 builder.append(addr.getPort()); 114 return builder.toString(); 115 } 116 117}