org.tranche.network
Class ConnectionUtil

java.lang.Object
  extended by org.tranche.network.ConnectionUtil

public class ConnectionUtil
extends java.lang.Object

Manages the Tranche server connections for the local JVM.

Author:
James "Augie" Hill - augman85@gmail.com, Bryan Smith - bryanesmith@gmail.com

Method Summary
static void addListener(ConnectionListener l)
          Adds a connection listener.
protected static void adjustConnection(java.lang.String host, int port, boolean ssl)
          Updates the connection to use the given set of connection parameters.
static void adjustConnections()
          Establishes the connections for the client and the server to work with.
static void clearExceptionsHost(java.lang.String host)
           
static TrancheServer connect(StatusTableRow row, boolean locked)
          Forces a connection on the Tranche server represented by the given row.
static TrancheServer connect(java.lang.String host, int port, boolean secure, boolean locked)
           
static TrancheServer connectHost(java.lang.String host, boolean locked)
          Forces a connection on the Tranche server with the given host name.
static TrancheServer connectURL(java.lang.String url, boolean locked)
          Forces a connection on the Tranche server represented by the given URL.
static void flagOffline(java.lang.String host)
          Must FIRST safely force the closure of the connection.
static void flagOffline(java.lang.String host, java.lang.String reason)
          Must FIRST safely force the closure of the connection.
static TrancheServer get(StatusTableRow row)
          Gets the connection to a Tranche server.
static java.util.Collection<java.lang.String> getConnectedHosts()
          Returns the host names for the servers with which there is an open connection.
static java.util.Collection<StatusTableRow> getConnectedRows()
          Returns the rows for the servers with which there is an open connection.
static java.util.Collection<java.lang.String> getConnectedURLs()
          Returns the URLs for the servers with which there is an open connection.
static Connection getConnection(java.lang.String host)
           
static TrancheServer getHost(java.lang.String host)
          Gets the connection to the Tranche server with the given host name.
static java.util.Collection<ConnectionListener> getListeners()
           
static boolean isConnected(java.lang.String host)
          Returns whether there is a connection open to the server with the given host name.
static boolean isLocked(java.lang.String host)
           
static void lockConnection(java.lang.String host)
           
static void removeListener(ConnectionListener l)
          Removes the given connection listener.
static void reportException(StatusTableRow row, java.lang.Exception e)
          Interprets an exception as it occurred during communication with a Tranche server.
static void reportExceptionHost(java.lang.String host, java.lang.Exception e)
          Interprets an exception as it occurred during communication with a Tranche server.
static void reportExceptionURL(java.lang.String URL, java.lang.Exception e)
          Interprets an exception as it occurred during communication with a Tranche server.
static void safeClose(StatusTableRow row)
          Reports that a process is done using a Tranche server connection.
static void safeCloseHost(java.lang.String host)
          Reports that a process is done using a Tranche server connection.
static void safeCloseURL(java.lang.String url)
          Reports that a process is done using a Tranche server connection.
static void safeForceClose(java.lang.String host, java.lang.String reason)
          Closes the connection immediately.
static void safeForceCloseAll(java.lang.String reason)
           
static int size()
          Gets the number of open connections.
static void unlockConnection(java.lang.String host)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

isConnected

public static boolean isConnected(java.lang.String host)

Returns whether there is a connection open to the server with the given host name.

Parameters:
host - A host name
Returns:
Whether there is a connection open to the server with the given host name

isLocked

public static boolean isLocked(java.lang.String host)
Parameters:
host -
Returns:

size

public static int size()

Gets the number of open connections.

Returns:
The number of open connections

getConnectedHosts

public static java.util.Collection<java.lang.String> getConnectedHosts()

Returns the host names for the servers with which there is an open connection.

Returns:
A collection of host names for the servers with which there is an open connection

getConnectedRows

public static java.util.Collection<StatusTableRow> getConnectedRows()

Returns the rows for the servers with which there is an open connection.

Returns:
A collection of rows for the servers with which there is an open connection.

getConnectedURLs

public static java.util.Collection<java.lang.String> getConnectedURLs()

Returns the URLs for the servers with which there is an open connection.

Returns:
A collection of URLs for the servers with which there is an open connection.

get

public static TrancheServer get(StatusTableRow row)

Gets the connection to a Tranche server.

Returns NULL if there is no connection with the Tranche server.

Parameters:
row - A row representing a Tranche server.
Returns:
A connection to a Tranche server

getHost

public static TrancheServer getHost(java.lang.String host)

Gets the connection to the Tranche server with the given host name.

Returns NULL if there is no connection with the Tranche server.

Parameters:
host - A host name
Returns:
A connection to a Tranche server. NULL if there is no connection with the Tranche server

connect

public static TrancheServer connect(StatusTableRow row,
                                    boolean locked)
                             throws java.lang.Exception

Forces a connection on the Tranche server represented by the given row.

Parameters:
row - A row representing a Tranche server
locked -
Returns:
A connection to a Tranche server
Throws:
java.lang.Exception

connectHost

public static TrancheServer connectHost(java.lang.String host,
                                        boolean locked)
                                 throws java.lang.Exception

Forces a connection on the Tranche server with the given host name.

Parameters:
host - A host name
locked -
Returns:
A connection to a Tranche server
Throws:
java.lang.Exception

connect

public static TrancheServer connect(java.lang.String host,
                                    int port,
                                    boolean secure,
                                    boolean locked)
                             throws java.lang.Exception
Parameters:
host -
port -
secure -
locked -
Returns:
Throws:
java.lang.Exception

connectURL

public static TrancheServer connectURL(java.lang.String url,
                                       boolean locked)
                                throws java.lang.Exception

Forces a connection on the Tranche server represented by the given URL.

Parameters:
url - A Tranche server URL
locked -
Returns:
A connection to a Tranche server
Throws:
java.lang.Exception

reportException

public static void reportException(StatusTableRow row,
                                   java.lang.Exception e)

Interprets an exception as it occurred during communication with a Tranche server.

Take care to ensure that only exceptions having to do with the communication with the Tranche server are being reported.

Parameters:
row - A row
e - An exception

reportExceptionURL

public static void reportExceptionURL(java.lang.String URL,
                                      java.lang.Exception e)

Interprets an exception as it occurred during communication with a Tranche server.

Take care to ensure that only exceptions having to do with the communication with the Tranche server are being reported.

Parameters:
URL - A Tranche URL
e - An exception

reportExceptionHost

public static void reportExceptionHost(java.lang.String host,
                                       java.lang.Exception e)

Interprets an exception as it occurred during communication with a Tranche server.

Take care to ensure that only exceptions having to do with the communication with the Tranche server are being reported.

Parameters:
host - A host name
e - An exception

clearExceptionsHost

public static void clearExceptionsHost(java.lang.String host)
Parameters:
host -

flagOffline

public static void flagOffline(java.lang.String host)

Must FIRST safely force the closure of the connection.

Sets a server as offline in the network status.

Parameters:
host - A host name

flagOffline

public static void flagOffline(java.lang.String host,
                               java.lang.String reason)

Must FIRST safely force the closure of the connection.

Sets a server as offline in the network status.

Parameters:
host - A host name
reason - Brief explanation why flagging offline. Can be null, but any information helps.

lockConnection

public static void lockConnection(java.lang.String host)
Parameters:
host -

unlockConnection

public static void unlockConnection(java.lang.String host)
Parameters:
host -

getConnection

public static Connection getConnection(java.lang.String host)
Parameters:
host -
Returns:

safeClose

public static void safeClose(StatusTableRow row)

Reports that a process is done using a Tranche server connection.

Parameters:
row - A status row

safeCloseURL

public static void safeCloseURL(java.lang.String url)

Reports that a process is done using a Tranche server connection.

Parameters:
url - A Tranche URL

safeCloseHost

public static void safeCloseHost(java.lang.String host)

Reports that a process is done using a Tranche server connection.

Parameters:
host - A host name

safeForceClose

public static void safeForceClose(java.lang.String host,
                                  java.lang.String reason)

Closes the connection immediately.

Parameters:
host - A host name
reason - An arbitrary message explaining why called. Useful for troubleshooting.

safeForceCloseAll

public static void safeForceCloseAll(java.lang.String reason)
Parameters:
reason -

adjustConnection

protected static void adjustConnection(java.lang.String host,
                                       int port,
                                       boolean ssl)

Updates the connection to use the given set of connection parameters.

When some connection parameters change, need to kill the old connection and open a new one.

Parameters:
host - A host name
port - A port number
ssl - Whether the server communicates over SSL

adjustConnections

public static void adjustConnections()

Establishes the connections for the client and the server to work with.

For the client, this means connecting to a full hash span with read/write priveleges for all hashes.

For a data server, this means connecting to the servers from which to perform updates to the network status table, the servers with overlapping hash spans, and also a full hash span of servers (for maintenance of sticky chunks.)

For a routing server, this means connecting to the servers from which to perform update to the network status table and the servers being routed to.

If connections have already been made, will try to make as few changes as possible to that pool of existing connections.


addListener

public static void addListener(ConnectionListener l)

Adds a connection listener.

Parameters:
l - A connection listener

removeListener

public static void removeListener(ConnectionListener l)

Removes the given connection listener.

Parameters:
l - A connection listener

getListeners

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


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