org.tranche.network
Class StatusTable

java.lang.Object
  extended by org.tranche.commons.Debuggable
      extended by org.tranche.network.StatusTable
All Implemented Interfaces:
java.io.Serializable

public class StatusTable
extends org.tranche.commons.Debuggable
implements java.io.Serializable

Represents a set of servers. Each server is represented by a row in the table.

The table keeps the servers sorted alphabetically by host name.

Author:
James "Augie" Hill - augman85@gmail.com
See Also:
Serialized Form

Field Summary
static int VERSION_LATEST
          The value for the latest version of the object.
static int VERSION_ONE
          The value for the first version of the object.
 
Constructor Summary
StatusTable()
          Default constructor.
StatusTable(java.util.Collection<StatusTableRow> rows)
           
StatusTable(java.io.InputStream in)
          Deserializes the table based on the given stream.
 
Method Summary
 void addListener(StatusTableListener l)
          Adds the given listener to the list of listeners.
 void clear()
          Removes all the rows from the table.
 StatusTable clone()
          Creates a duplicate table.
 boolean contains(java.lang.String host)
          Returns whether a row with the given host name is contained within the table.
protected  void deserialize(java.io.InputStream in)
          Sets the table based on the given serialized table.
protected  void deserializeVersionOne(java.io.InputStream in)
          Sets the values of the row based the given version one serialized table.
 boolean equals(java.lang.Object o)
          Evaluates the given object against this one.
 void fireRowsUpdated(java.util.Map<java.lang.String,java.lang.Boolean> hosts, java.util.Map<java.lang.String,java.lang.Boolean> affectsHashSpans)
          Fires an event for having updated a map of hosts.
 void fireRowUpdated(java.lang.String host, boolean affectsConnectivity, boolean affectsHashSpans)
          Fires an event for having updated a row.
 java.lang.String getHost(int i)
           
 java.util.List<java.lang.String> getHosts()
          Returns the list of hosts.
 java.util.Collection<StatusTableListener> getListeners()
           
 StatusTableRow getRow(java.lang.String host)
          Gets the row with the given host name.
 java.util.List<StatusTableRow> getRows()
          Returns the rows in the table.
 java.util.List<StatusTableRow> getRows(java.lang.String startHost, java.lang.String endHost)
          Gets all of the rows that have host names between startHost (inclusive) and endHost (exclusive.)
 StatusTable getStatus(java.lang.String startHost, java.lang.String endHost)
          Returns a status table containing the rows between the two given host names.
 java.util.List<java.lang.String> getURLs()
          Returns the list of URLs.
 int getVersion()
          Gets the version.
 int hashCode()
           
 boolean isEmpty()
          Returns whether there are no rows in the table.
 void removeDefunctRows()
          Removes rows that are offline, not connected, and haven't been updated in x number of milliseconds.
 void removeListener(StatusTableListener l)
          Removes the given listener from the list of listeners.
 void removeRow(java.lang.String host)
          Removes the rows with the given host name.
 void removeRows(java.util.Collection<java.lang.String> hosts)
          Removes the rows with host names within the given collection
 void serialize(int tableVersion, int rowVersion, java.io.OutputStream out)
          Write to the output stream in the given table and row versions.
protected  void serializeVersionOne(int rowVersion, java.io.OutputStream out)
          Outputs the values in the structure defined by version one to the given output stream.
protected  void setRow(StatusTableRow row)
          Sets the row in the table.
protected  void setRows(java.util.Collection<StatusTableRow> rows)
          Sets the rows of the table.
 int size()
          Gets the number of rows in the table.
 byte[] toByteArray(int tableVersion, int rowVersion)
           
 java.lang.String toString()
          Prints out the values of the attributes in the row.
 
Methods inherited from class org.tranche.commons.Debuggable
debugErr, debugErr, debugOut, isDebug, setDebug
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

VERSION_ONE

public static final int VERSION_ONE

The value for the first version of the object.

See Also:
Constant Field Values

VERSION_LATEST

public static final int VERSION_LATEST

The value for the latest version of the object.

See Also:
Constant Field Values
Constructor Detail

StatusTable

public StatusTable()

Default constructor.


StatusTable

public StatusTable(java.util.Collection<StatusTableRow> rows)
Parameters:
rows - A collection of rows

StatusTable

public StatusTable(java.io.InputStream in)
            throws java.io.IOException

Deserializes the table based on the given stream.

Parameters:
in - The stream containing a serialized row
Throws:
java.io.IOException
Method Detail

getVersion

public int getVersion()

Gets the version.

Returns:
The version

clear

public void clear()

Removes all the rows from the table.


setRows

protected void setRows(java.util.Collection<StatusTableRow> rows)

Sets the rows of the table. The rows are sorted alphabetically by host name.

Will overwrite rows of the same host names.

Does not clear the previous rows.

Parameters:
rows - A collection of rows

setRow

protected void setRow(StatusTableRow row)

Sets the row in the table. The rows are sorted alphabetically by host name.

Will overwrite rows of the same host names.

If setting many rows, use setRows(Collection) instead.

Parameters:
row - A row

removeDefunctRows

public void removeDefunctRows()

Removes rows that are offline, not connected, and haven't been updated in x number of milliseconds.

Also removes banned servers.


removeRows

public void removeRows(java.util.Collection<java.lang.String> hosts)

Removes the rows with host names within the given collection

Parameters:
hosts - A collection of host names

removeRow

public void removeRow(java.lang.String host)

Removes the rows with the given host name.

Removing many rows, use removeRows(Collection) instead.

Parameters:
host -

size

public int size()

Gets the number of rows in the table.

Returns:
The number of rows in the table

isEmpty

public boolean isEmpty()

Returns whether there are no rows in the table.

Returns:
Whether there are no rows in the table.

getRows

public java.util.List<StatusTableRow> getRows()

Returns the rows in the table. They will be sorted in alphabetical order by host name.

Returns:
The rows in the table.

getRows

public java.util.List<StatusTableRow> getRows(java.lang.String startHost,
                                              java.lang.String endHost)

Gets all of the rows that have host names between startHost (inclusive) and endHost (exclusive.)

Parameters:
startHost - Return rows that have host names alphabetically after or equal to this host name.
endHost - Return rows that have host names alphabetically before this host name.
Returns:
A list of objects that represent server status.

getStatus

public StatusTable getStatus(java.lang.String startHost,
                             java.lang.String endHost)

Returns a status table containing the rows between the two given host names.

Parameters:
startHost - A host name
endHost - A host name
Returns:
A status table

getRow

public StatusTableRow getRow(java.lang.String host)

Gets the row with the given host name.

Parameters:
host - A host name.
Returns:
The row with the given host name. NULL if the row does not exist.

contains

public boolean contains(java.lang.String host)

Returns whether a row with the given host name is contained within the table.

Parameters:
host - A host name
Returns:
Whether a row with the given host name is contained within the table

getHost

public java.lang.String getHost(int i)
Parameters:
i -
Returns:

getHosts

public java.util.List<java.lang.String> getHosts()

Returns the list of hosts.

Returns:
The list of hosts

getURLs

public java.util.List<java.lang.String> getURLs()

Returns the list of URLs.

Returns:
The list of URLs

toByteArray

public byte[] toByteArray(int tableVersion,
                          int rowVersion)
                   throws java.io.IOException
Returns:
Throws:
java.io.IOException

serialize

public void serialize(int tableVersion,
                      int rowVersion,
                      java.io.OutputStream out)
               throws java.io.IOException

Write to the output stream in the given table and row versions.

Parameters:
out - The output stream
Throws:
java.io.IOException

serializeVersionOne

protected void serializeVersionOne(int rowVersion,
                                   java.io.OutputStream out)
                            throws java.io.IOException

Outputs the values in the structure defined by version one to the given output stream.

Parameters:
out - The output stream
Throws:
java.io.IOException

deserialize

protected void deserialize(java.io.InputStream in)
                    throws java.io.IOException

Sets the table based on the given serialized table.

Parameters:
in - An input stream containing a serialized table
Throws:
java.io.IOException

deserializeVersionOne

protected void deserializeVersionOne(java.io.InputStream in)
                              throws java.io.IOException

Sets the values of the row based the given version one serialized table.

Parameters:
in - An input stream containing a version one serialized table
Throws:
java.io.IOException

clone

public StatusTable clone()

Creates a duplicate table.

Overrides:
clone in class java.lang.Object
Returns:
A duplicate table

equals

public boolean equals(java.lang.Object o)

Evaluates the given object against this one.

Overrides:
equals in class java.lang.Object
Parameters:
o - A StatusTable object
Returns:
Whether the given object and this one are equivalent.

hashCode

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

toString

public java.lang.String toString()

Prints out the values of the attributes in the row.

Overrides:
toString in class java.lang.Object
Returns:
The printed value of the row

addListener

public void addListener(StatusTableListener l)

Adds the given listener to the list of listeners.

Parameters:
l - A listener

removeListener

public void removeListener(StatusTableListener l)

Removes the given listener from the list of listeners.

Parameters:
l - A listener

getListeners

public java.util.Collection<StatusTableListener> getListeners()
Returns:

fireRowsUpdated

public void fireRowsUpdated(java.util.Map<java.lang.String,java.lang.Boolean> hosts,
                            java.util.Map<java.lang.String,java.lang.Boolean> affectsHashSpans)

Fires an event for having updated a map of hosts.

Parameters:
hosts - A map of hosts to whether connectivity was affected
affectsHashSpans - A map of hosts to whether hash spans were affected

fireRowUpdated

public void fireRowUpdated(java.lang.String host,
                           boolean affectsConnectivity,
                           boolean affectsHashSpans)

Fires an event for having updated a row.

Parameters:
host - The host name
affectsConnectivity - Whether the update changes the connectivity


This code is free for use both commercially and non-commercially as stated in the project's license