its.element
Class ClientConnection

java.lang.Object
  extended byits.element.ClientConnection

public class ClientConnection
extends java.lang.Object

Creates and monitors a client socket for reading ITS Frames. The socket is connected to a server and attempts to reconnect are made if the connection is broken.

The connection is considered to be broken if any socket exceptions are thrown during a read, or if an excessive time passes without receiving any data. This timeout value is 3 minutes by default.

If a connection attempt fails, there is a pause before the next attempt. The length of the pause starts at one second and doubles with each successive attempt before reaching a maximum of 64 seconds. A limit may be set on the number of unsuccessful reconnection attempts, but by default there is no limit.


Constructor Summary
ClientConnection(java.net.InetAddress host, int port)
          Creates connection given the address and port of the server
ClientConnection(java.lang.String host, int port)
          Creates connection given a hostname and a port.
 
Method Summary
 void close()
          Closes input stream and underlying client socket.
 void connect()
          Connect to server.
protected  void finalize()
          calls close to clean up the socket and stream
 int getInitialBackoff()
           
 int getMaxAttempt()
           
 int getMaxBackoff()
           
 int getTimeout()
           
 ItsFrame read()
          Reads an ITS frame from the server.
 void setInitialBackoff(int newBackoff)
          sets initialBackoff, the pause between the first two connection retries.
 void setMaxAttempt(int newMax)
          set the maximum number of connect attempts to be made.
 void setMaxBackoff(int newBackoff)
          sets maxBackoff, the maximum pause between connection attempts.
 void setServerPort(int port)
           
 void setTimeout(int newTimeout)
          sets receive timeout.
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ClientConnection

public ClientConnection(java.net.InetAddress host,
                        int port)
                 throws java.io.IOException
Creates connection given the address and port of the server

Parameters:
host - the server address
port - the server port number
Throws:
java.io.IOException - if a connection to the given server is impossible

ClientConnection

public ClientConnection(java.lang.String host,
                        int port)
                 throws java.io.IOException
Creates connection given a hostname and a port.

Parameters:
host - the server name
port - the server port number
Throws:
java.io.IOException - if a connection to the given server is impossible
Method Detail

connect

public void connect()
             throws ItsException
Connect to server. Does not return until a connection has been made.

Throws:
ItsException - thrown when too many reconnection attempts have been made

read

public ItsFrame read()
              throws ItsException
Reads an ITS frame from the server. Does not return until a frame is read. If the read operation times out, attempts to reconnect to the server.

Throws:
ItsException - thrown when too many reconnection attempts have been made

close

public void close()
Closes input stream and underlying client socket.


setMaxAttempt

public void setMaxAttempt(int newMax)
set the maximum number of connect attempts to be made. setting to 0 means try forever.


getMaxAttempt

public int getMaxAttempt()
Returns:
maxAttempt, where 0 means try forever

getInitialBackoff

public int getInitialBackoff()
Returns:
initialBackoff in milliseconds

setInitialBackoff

public void setInitialBackoff(int newBackoff)
sets initialBackoff, the pause between the first two connection retries. This will double with each attempt until maxBackoff is reached.

Parameters:
newBackoff - the new initial backoff between connection attempts in milliseconds. This must be >= 1.

setMaxBackoff

public void setMaxBackoff(int newBackoff)
sets maxBackoff, the maximum pause between connection attempts. The pause starts at 1 second and doubles with each attempt until maxBackoff is reached

Parameters:
newBackoff - the new maximum backoff between connection attempts. This must be >= initialBackoff.

getMaxBackoff

public int getMaxBackoff()
Returns:
maxBackoff in milliseconds

setTimeout

public void setTimeout(int newTimeout)
sets receive timeout. If a read operation blocks more than this interval, then ClientConnection will disconnect and attempt to reconnect.

Parameters:
newTimeout - the new receive timeout in milliseconds. A value of 0 disables the timeout and a read will block forever.

getTimeout

public int getTimeout()
Returns:
receive timeout in milliseconds

setServerPort

public void setServerPort(int port)

finalize

protected void finalize()
calls close to clean up the socket and stream