org.iperg.part.core
Class IpConnection

java.lang.Object
  extended by org.iperg.part.core.IpConnection
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
IpHttpConnection, IpTcpConnection

public abstract class IpConnection
extends java.lang.Object
implements java.lang.Runnable

The IpConnection class is the super class of all protocol specific connection classes. Connections are bi-directional communications channels that can be set up between pairs of IPerG processes. Once a connection has been established, it can be used to send events back and forth between the processes.

Version:
1.0.0 Initial version
Author:
Olov Stahl - olovs@sics.se

Field Summary
static int BROKEN
          A connection mode that means that the connection does not work properly for some reason, for instance due to a network error or the remote process having crashed.
static int CLOSED_LOCALLY
          A connection mode that means that the connection has been closed by the local process.
static int CLOSED_REMOTELY
          A connection mode that means that the connection has been closed by the remote process.
static int CONNECTED
          A connection status mode which means that the connection object has been successfully connected to a remote address via a call to connect or as a result of accepting an incoming connection after having called listen.
static int CONNECTING
          A connection status mode which means that a connection to a remote process is in progress but not yet finished.
static int LISTEN
          A connection mode that means that the connection object is listening for incoming connections on a local address as a result of the listen method being called.
static int UNCONNECTED
          A connection status mode that means that the connection object has not yet been connected to a local or remote address using the listen or connect methods.
 
Constructor Summary
protected IpConnection(int status)
          Create a new connection object.
 
Method Summary
protected abstract  boolean _outgoingEventsAreCached()
          Check if events sent via the send method are cached locally.
protected  void _send(IpNetworkEvent event)
          Method that does the actual work of sending data to the network.
protected abstract  void close()
          Method to be implemented by sub-classes.
 IpUrl getConnectionUrl()
          Get url that can be used to connect to the local address monitored by this connection, assuming that the listen method has been called on the connection object.
 IpIdentifier getRemoteProcessId()
          Get the identifier of the remote process to which this connection object is connected
 long getSilence()
          Get time (in milliseconds) since any data was last received using this connection.
 int getStatus()
          Get the current connection status mode.
 int getTotalBytesReceived()
          Return the number of bytes that has been received on this connection since it was created.
 int getTotalBytesSent()
          Return the number of bytes that has been sent on this connection since it was created.
 IpUrl getUrl()
          Get url used to create the connection that this object represents
protected  void notifyBrokenConnection()
          Should be called by protocol specific connection subclasses when this connection has been broken.
protected  void notifyBytesReceived(int nrOfBytes)
          Should be called by sub-classes to report that data has been received from the network.
protected  void notifyBytesSent(int nrOfBytes)
          Should be called by sub-classes to report that data has been sent to the network.
protected  void notifyReceivedEvent(IpNetworkEvent event)
          Should be called by protocol specific connection subclasses when an event has been received on this connection.
protected abstract  IpConnection protoAccept()
          Accept an incoming connection
protected abstract  void protoConnect(IpUrl url)
          Method to be implemented by sub-classes.
protected abstract  IpUrl protoListen(IpUrl url)
          Method to be implemented by sub-classes.
 void run()
          Connection thread's run method which checks for incoming connections if connection is in "listen mode", receives data chunks if the connection is in "connected mode", or performs connect if mode is "connecting".
protected  void setInputStream(java.io.InputStream in)
          Set stream to be used for reading incoming network data.
protected  void setOutputStream(java.io.OutputStream out)
          Set stream to be used for writing outgoing network data.
protected  void setUrl(IpUrl url)
          Set url used to create the connection that this object represents
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

UNCONNECTED

public static final int UNCONNECTED
A connection status mode that means that the connection object has not yet been connected to a local or remote address using the listen or connect methods.

See Also:
Constant Field Values

CONNECTING

public static final int CONNECTING
A connection status mode which means that a connection to a remote process is in progress but not yet finished.

See Also:
Constant Field Values

CONNECTED

public static final int CONNECTED
A connection status mode which means that the connection object has been successfully connected to a remote address via a call to connect or as a result of accepting an incoming connection after having called listen.

See Also:
Constant Field Values

LISTEN

public static final int LISTEN
A connection mode that means that the connection object is listening for incoming connections on a local address as a result of the listen method being called.

See Also:
Constant Field Values

CLOSED_LOCALLY

public static final int CLOSED_LOCALLY
A connection mode that means that the connection has been closed by the local process. A closed connection can not be used to send or receive events, or accept incoming connections.

See Also:
Constant Field Values

CLOSED_REMOTELY

public static final int CLOSED_REMOTELY
A connection mode that means that the connection has been closed by the remote process. A closed connection can not be used to send or receive events, or accept incoming connections.

See Also:
Constant Field Values

BROKEN

public static final int BROKEN
A connection mode that means that the connection does not work properly for some reason, for instance due to a network error or the remote process having crashed. A broken connection can not be used to send or receive events, or accept incoming connections.

See Also:
Constant Field Values
Constructor Detail

IpConnection

protected IpConnection(int status)
Create a new connection object. Should be called by sub-class to set some attributes common to all protocol connection instances.

Parameters:
status - The current status of the connection object
Method Detail

run

public void run()
Connection thread's run method which checks for incoming connections if connection is in "listen mode", receives data chunks if the connection is in "connected mode", or performs connect if mode is "connecting".

Specified by:
run in interface java.lang.Runnable

protoConnect

protected abstract void protoConnect(IpUrl url)
                              throws IpInvalidUrlException,
                                     java.io.IOException
Method to be implemented by sub-classes. Connect to a remote address specified by a url. The call blocks until the connection attempt completes or fails.

Parameters:
url - The url that specifies where to connect
Throws:
IpInvalidUrlException - if the url doesn't specify a valid connection address
java.io.IOException - if a protocol specific error occured

protoListen

protected abstract IpUrl protoListen(IpUrl url)
                              throws IpInvalidUrlException,
                                     java.io.IOException
Method to be implemented by sub-classes. Start listening for connection attempts on local address specified by the given url.

Parameters:
url - A url specifying a local address
Returns:
A url that a remote process can use to connect to the local address
Throws:
IpInvalidUrlException - if the url doesn't identify a local address
java.io.IOException - if a protocol specific error occured

protoAccept

protected abstract IpConnection protoAccept()
                                     throws java.io.IOException
Accept an incoming connection

Throws:
java.io.IOException

getUrl

public IpUrl getUrl()
Get url used to create the connection that this object represents

Returns:
Url used to create this connection object

setUrl

protected void setUrl(IpUrl url)
Set url used to create the connection that this object represents


setOutputStream

protected void setOutputStream(java.io.OutputStream out)
Set stream to be used for writing outgoing network data. Is called by sub-classes, and is only needed if they don't override the _send method.


setInputStream

protected void setInputStream(java.io.InputStream in)
Set stream to be used for reading incoming network data. Is called by sub-classes that don't handle incoming network data themselves.


getRemoteProcessId

public IpIdentifier getRemoteProcessId()
Get the identifier of the remote process to which this connection object is connected

Returns:
The identifier of the remote process

getConnectionUrl

public IpUrl getConnectionUrl()
Get url that can be used to connect to the local address monitored by this connection, assuming that the listen method has been called on the connection object.

Returns:
Connection url if connection is listening to incoming connections, null otherwise

getSilence

public long getSilence()
Get time (in milliseconds) since any data was last received using this connection.

Returns:
Milliseconds since any data was received, or -1 if connection i in listen mode

getStatus

public int getStatus()
Get the current connection status mode. The value returned should be one of:
 IpConnection.UNCONNECTED
 IpConnection.CONNECTING
 IpConnection.CONNECTED
 IpConnection.LISTEN
 IpConnection.CLOSED
 IpConnection.BROKEN


_send

protected void _send(IpNetworkEvent event)
              throws java.io.IOException
Method that does the actual work of sending data to the network. May be over-ridden by connection sub-classes.

Parameters:
event - The event to send
Throws:
java.io.IOException - if a protocol specific error occured

notifyBrokenConnection

protected void notifyBrokenConnection()
Should be called by protocol specific connection subclasses when this connection has been broken.


notifyBytesSent

protected void notifyBytesSent(int nrOfBytes)
Should be called by sub-classes to report that data has been sent to the network.

Parameters:
nrOfBytes - The number of data bytes that was sent

getTotalBytesSent

public int getTotalBytesSent()
Return the number of bytes that has been sent on this connection since it was created. Note that this only includes bytes sent by PART and not additional bytes sent by underlying protocol stacks.

Returns:
Total number of bytes sent since connection was established

notifyBytesReceived

protected void notifyBytesReceived(int nrOfBytes)
Should be called by sub-classes to report that data has been received from the network.

Parameters:
nrOfBytes - The number of data bytes that was received

getTotalBytesReceived

public int getTotalBytesReceived()
Return the number of bytes that has been received on this connection since it was created. Note that this only includes bytes ereceived by PART and not additional bytes received by underlying protocol stacks.

Returns:
Total number of bytes received since connection was established

notifyReceivedEvent

protected void notifyReceivedEvent(IpNetworkEvent event)
Should be called by protocol specific connection subclasses when an event has been received on this connection.

Parameters:
event - The event that was received

close

protected abstract void close()
                       throws java.io.IOException
Method to be implemented by sub-classes. Close connection in a protocol specific manner. Should not set the connection status. If an IOException is not thrown, it is assumed that the close was successful.

Throws:
java.io.IOException - if the close failed for some reason

_outgoingEventsAreCached

protected abstract boolean _outgoingEventsAreCached()
Check if events sent via the send method are cached locally.