eu.xtreemos.system.communication.net
Class CommunicationAddress

java.lang.Object
  extended by eu.xtreemos.system.communication.net.CommunicationAddress
All Implemented Interfaces:
java.io.Serializable

public class CommunicationAddress
extends java.lang.Object
implements java.io.Serializable

Author:
gregor.pipan@xlab.si, eva.milosev@xlab.si - commentor :), matej.artac@xlab.si Creation date: 17 Feb 2007
See Also:
Serialized Form

Field Summary
 java.net.InetAddress externalAddress
          The address of the host that is a public external IP.
 java.net.InetAddress host
           
(package private) static org.apache.log4j.Logger logger
           
 int port
           
private static long serialVersionUID
           
private static java.lang.String TO_STRING_PREFIX
           
 
Constructor Summary
CommunicationAddress()
           
CommunicationAddress(CommunicationAddress address)
           
CommunicationAddress(java.net.InetAddress host, int port)
           
CommunicationAddress(java.net.InetAddress localAddress, int port, java.net.InetAddress extAddress)
           
CommunicationAddress(java.lang.String host, int port)
           
 
Method Summary
 boolean equals(CommunicationAddress other)
           
 boolean equals(java.lang.Object arg0)
           
 java.lang.String getAdd()
           
 CommunicationAddress getExternalAddress()
          Composes an instance of CommunicationAddress that points to the external address of an xosd machine.
static CommunicationAddress getFromHostPort(java.lang.String hostport)
          Parse the address from a string.
 java.net.InetAddress getHost()
           
 int getPort()
           
 java.lang.String getStringHost()
           
 int hashCode()
           
static boolean isMyAddress(java.net.InetAddress addressToCheck)
          Check whether the given address is an address used by one of the local network interfaces.
 boolean isSame(CommunicationAddress address)
          Checks whether the two CommunicationAddress instances point to the same address.
protected  boolean isSameHostPort(CommunicationAddress address)
          Check the equality of the port numbers and the host IPs.
 boolean isSameLAN(CommunicationAddress arg0)
          Checks whether the two communication addresses share the same LAN.
static java.net.InetAddress localNetAddress()
          A helper class that polls the local network interfaces and selects the IPv4 address used by one of the interfaces.
static java.net.InetAddress localNetAddress(java.lang.String interfaceName)
          A helper class that polls the network interface with the given name, and returns its IPv4 address.
static CommunicationAddress parse(java.lang.String text)
          Convert a String formatted as the communication address.
 void setHost(java.net.InetAddress host)
           
 void setPort(int port)
           
 void setStringHost(java.lang.String stringHost)
           
 java.lang.String toString()
           
 java.lang.String toWholeAddress()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

TO_STRING_PREFIX

private static final java.lang.String TO_STRING_PREFIX
See Also:
Constant Field Values

logger

static org.apache.log4j.Logger logger

externalAddress

public java.net.InetAddress externalAddress
The address of the host that is a public external IP. This is the address of a firewall/gateway node that bridges a private local network with Internet (NAT).


host

public java.net.InetAddress host

port

public int port
Constructor Detail

CommunicationAddress

public CommunicationAddress()

CommunicationAddress

public CommunicationAddress(java.lang.String host,
                            int port)
                     throws java.net.UnknownHostException
Throws:
java.net.UnknownHostException

CommunicationAddress

public CommunicationAddress(java.net.InetAddress host,
                            int port)

CommunicationAddress

public CommunicationAddress(java.net.InetAddress localAddress,
                            int port,
                            java.net.InetAddress extAddress)

CommunicationAddress

public CommunicationAddress(CommunicationAddress address)
Method Detail

equals

public boolean equals(CommunicationAddress other)

getAdd

public java.lang.String getAdd()

getFromHostPort

public static CommunicationAddress getFromHostPort(java.lang.String hostport)
Parse the address from a string. This is the reverse operation from toString().

Parameters:
hostport - The string formatted as the CommunicationAddress.
Returns:
An instance representing the parsed string.

isSame

public boolean isSame(CommunicationAddress address)
Checks whether the two CommunicationAddress instances point to the same address. This includes both the equality of the host IP and port number as well as being on the same LAN (i.e. having the same external address).

Parameters:
address - The argument to check the equality with.
Returns:
True if the two instances represent the same address, or false if they differ at least by the port number, the address or the LAN they share.

isSameHostPort

protected boolean isSameHostPort(CommunicationAddress address)
Check the equality of the port numbers and the host IPs.

Parameters:
address -
Returns:

getHost

public java.net.InetAddress getHost()
Returns:
the host

setHost

public void setHost(java.net.InetAddress host)
Parameters:
host - the host to set

getPort

public int getPort()
Returns:
the port

setPort

public void setPort(int port)
Parameters:
port - the port to set

toWholeAddress

public java.lang.String toWholeAddress()

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

localNetAddress

public static java.net.InetAddress localNetAddress()
A helper class that polls the local network interfaces and selects the IPv4 address used by one of the interfaces. Currently it first tries eth0 and eth1, then proceeds with the rest of the available interfaces that are neither loopback nore vmware interfaces. The method returns the first suitable interface's address it encounters.

Returns:
The address of the first suitable local network interface. If no suitable interfaces are found, it returns null.

localNetAddress

public static java.net.InetAddress localNetAddress(java.lang.String interfaceName)
A helper class that polls the network interface with the given name, and returns its IPv4 address.

Parameters:
interfaceName - The name of the network interface we would like the address off.
Returns:
The IPv4 address used by the given interfaceName. If the interface does not exist or has no suitable address, the return value is null.

isMyAddress

public static boolean isMyAddress(java.net.InetAddress addressToCheck)
Check whether the given address is an address used by one of the local network interfaces.

Parameters:
addressToCheck - The address we would like to check.
Returns:
True if the addressToCheck is indeed from this node, and false otherwise.

getExternalAddress

public CommunicationAddress getExternalAddress()
Composes an instance of CommunicationAddress that points to the external address of an xosd machine. This address is usually a gateway or a firewall computer/router which will forward the traffic to a different physical machine within NAT. TODO: the port number returned will be the same port number as that of the original communication address.

Returns:
A new instance of the external address of the xosd computer, or null, if the external address is not defined.

isSameLAN

public boolean isSameLAN(CommunicationAddress arg0)
Checks whether the two communication addresses share the same LAN. This is established by comparing the external address members of the instances. If either of the external addresses is null or if the external host and port match, the two addresses are assumed to be on the same LAN.

Returns:
True if the addresses represent machines sharing the same LAN, or false otherwise.

getStringHost

public java.lang.String getStringHost()

setStringHost

public void setStringHost(java.lang.String stringHost)

parse

public static CommunicationAddress parse(java.lang.String text)
                                  throws java.lang.Exception
Convert a String formatted as the communication address. The format should be in the form ://ip:port or ://ip:port(extip)

Parameters:
text - The formatted string input.
Returns:
The result of the parsing the string.
Throws:
java.lang.Exception - When the string is badly formed.

equals

public boolean equals(java.lang.Object arg0)
Overrides:
equals in class java.lang.Object

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object