jist.runtime
Class Node

java.lang.Object
  extended by jist.runtime.Node
All Implemented Interfaces:
java.io.Serializable

public final class Node
extends java.lang.Object
implements java.io.Serializable

Stores and manipulates host:port information.

Since:
JIST1.0
Version:
$Id: Node.java,v 1.12 2004/04/06 16:07:43 barr Exp $
Author:
Rimon Barr <barr+jist@cs.cornell.edu>
See Also:
Serialized Form

Field Summary
private  java.lang.String cacheToString
          cached toString() of this object.
private  int hashCode
          hash of this object.
private  java.net.InetAddress host
          Internet address of node.
private static java.net.InetAddress localhost
          Internet address of local host.
private static java.lang.String loopback
          Local host (loopback) Internet address string.
private  short port
          Port of jist node.
 
Constructor Summary
Node(byte[] node, int offset)
          Instantiate node object from serialized information in byte array at given offset.
Node(byte[] addr, short port)
          Instantiate node object with given host and port.
Node(java.net.InetAddress host, int port)
          Instantiate node object with given host and port.
Node(java.net.InetAddress host, short port)
          Instantiate node object with given host and port.
Node(int port)
          Instantiate node object with given port on local host.
Node(short port)
          Instantiate node object with given port on the local host.
Node(java.lang.String host, int port)
          Instantiate node object with host string and port.
Node(java.lang.String host, short port)
          Instantiate node object with given host and port.
 
Method Summary
private  void calcHashCode()
          Compute and cache node hash code.
 boolean equals(java.lang.Object o)
          Determine whether this node is the same as another.
 java.net.InetAddress getHost()
          Return host Internet address of current node.
 java.lang.String getHostString()
          Return host string of current node.
private static java.net.InetAddress getLocalHost()
          Find local host Internet address.
private static java.net.InetAddress getLocalHostJava()
          Find local host Internet address using (portable) Java mechanisms.
static java.net.InetAddress getLocalHostLinux(java.lang.String iface)
          Find local host Internet address using Unix mechanisms.
 short getPort()
          Return port of current node.
 int hashCode()
          Return node hash code.
static Node parse(java.lang.String s)
          Parse String (host:port) into a Node.
static Node parse(java.lang.String s, int defaultPort)
          Parse string (host:port) into a Node, using the default port if the string does not contain a port.
 byte[] toByteArray()
          Serialize node instance into byte array.
 void toByteArray(byte[] b, int offset)
          Serialize node instance into byte array.
 java.lang.String toString()
          Return string representation of node in format host:port.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

host

private java.net.InetAddress host
Internet address of node.


port

private short port
Port of jist node.


hashCode

private int hashCode
hash of this object.


cacheToString

private java.lang.String cacheToString
cached toString() of this object.


localhost

private static java.net.InetAddress localhost
Internet address of local host.


loopback

private static final java.lang.String loopback
Local host (loopback) Internet address string.

See Also:
Constant Field Values
Constructor Detail

Node

public Node(java.net.InetAddress host,
            short port)
Instantiate node object with given host and port.

Parameters:
host - Internet address of node instance
port - port of node instance

Node

public Node(java.lang.String host,
            short port)
     throws java.net.UnknownHostException
Instantiate node object with given host and port.

Parameters:
host - Internet address or name of node instance
port - port of node instance
Throws:
java.net.UnknownHostException - thrown if named host can not be resolved

Node

public Node(byte[] addr,
            short port)
     throws java.net.UnknownHostException
Instantiate node object with given host and port.

Parameters:
addr - Internet address of node instance as quad-byte array
port - port of node instance
Throws:
java.net.UnknownHostException - never

Node

public Node(short port)
Instantiate node object with given port on the local host.

Parameters:
port - port of node instance

Node

public Node(java.net.InetAddress host,
            int port)
Instantiate node object with given host and port. Convenience method to accept integer ports and convert to short.

Parameters:
host - Internet address of node instance
port - port of node instance

Node

public Node(java.lang.String host,
            int port)
     throws java.net.UnknownHostException
Instantiate node object with host string and port. Convenience method to accept integer ports and convert to short.

Parameters:
host - Internet address string of node instance
port - port of node instance
Throws:
java.net.UnknownHostException - invalid host name or IP address

Node

public Node(int port)
Instantiate node object with given port on local host. Convenience method to accept integer ports and convert to short.

Parameters:
port - port of node instance

Node

public Node(byte[] node,
            int offset)
Instantiate node object from serialized information in byte array at given offset. The byte array must contain 4-bytes for the Internet address and 2-bytes for the port.

Parameters:
node - byte array with node information
offset - location of information within byte array
Method Detail

toByteArray

public byte[] toByteArray()
Serialize node instance into byte array. The byte array will be of length 6.

Returns:
serialized node information

toByteArray

public void toByteArray(byte[] b,
                        int offset)
Serialize node instance into byte array. The serialized byte representation of a node occupies 6 bytes.

Parameters:
b - byte array to place node information into
offset - starting location within byte array.

parse

public static Node parse(java.lang.String s)
                  throws java.net.UnknownHostException
Parse String (host:port) into a Node.

Parameters:
s - serialized node string in format host:port
Returns:
node instance
Throws:
java.net.UnknownHostException - thrown if host name within string can not be resolved

parse

public static Node parse(java.lang.String s,
                         int defaultPort)
                  throws java.net.UnknownHostException
Parse string (host:port) into a Node, using the default port if the string does not contain a port.

Parameters:
s - serialized node string in format host[:port]
defaultPort - default port to use if port omitted
Returns:
parsed node object
Throws:
java.net.UnknownHostException - thrown if host name within string can not be resolved.

getHost

public java.net.InetAddress getHost()
Return host Internet address of current node.

Returns:
host Internet address

getHostString

public java.lang.String getHostString()
Return host string of current node.

Returns:
host string

getPort

public short getPort()
Return port of current node.

Returns:
node port

toString

public java.lang.String toString()
Return string representation of node in format host:port.

Overrides:
toString in class java.lang.Object
Returns:
string representation of node in format host:port

equals

public boolean equals(java.lang.Object o)
Determine whether this node is the same as another.

Overrides:
equals in class java.lang.Object
Parameters:
o - object to test equality
Returns:
whether this node is equal to given object

calcHashCode

private void calcHashCode()
Compute and cache node hash code.


hashCode

public int hashCode()
Return node hash code.

Overrides:
hashCode in class java.lang.Object
Returns:
node hash code

getLocalHost

private static java.net.InetAddress getLocalHost()
Find local host Internet address. Attempts to discover local host IP address using a number of techniques.

Returns:
Internet address of localhost, or null if it can not be discovered.

getLocalHostJava

private static java.net.InetAddress getLocalHostJava()
Find local host Internet address using (portable) Java mechanisms.

Returns:
Internet address of localhost, or null if it can not be discovered.

getLocalHostLinux

public static java.net.InetAddress getLocalHostLinux(java.lang.String iface)
Find local host Internet address using Unix mechanisms. Specifically, this method invokes 'ifconfig', and parses its output, searching for the IP address of the given interface.

Parameters:
iface - network interface to query 'ifconfig'
Returns:
Internet address of localhost, or null if it can not be discovered.