info.jedi.net
Class CommunicationPort

java.lang.Object
  |
  +--info.jedi.net.CommunicationPort

public class CommunicationPort
extends java.lang.Object

This class handles reliable communication to and from a remote location. Here location refers to a specific internet address and port. It keeps track of all the messages currently being sent and those that have recently been received, to make sure that messages are resent if they are not acknowledged and to make sure that no message is neglected or received more than once.

See Also:
ReliableHandler

Field Summary
static int MAX_SENT
          The maximum number of messages in the sending window at once (for all CommunicationPorts in this VM).
 
Constructor Summary
CommunicationPort(java.net.InetAddress address, int port, ReliableHandler reliableHandler)
          Creates a Communication port associated with this address and port
 
Method Summary
 boolean add(OutputPacket packet)
          Adds an OutputPacket to the sending queue.
 java.net.InetAddress address()
          the address this communication port is associated with
 boolean hasReceived(InputPacket packet)
          Checks to see if a given InputPacket has been received before.
 int port()
          the port this communication port is associated with
 void remove(int id)
          Removes an OutputPacket from the sending queue.
protected  void resend(OutputPacket packet)
          Resends a particular packet
 void resendAll()
          resends all messages to the address & port associated with this object that have not yet been acknowledged.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_SENT

public static final int MAX_SENT
The maximum number of messages in the sending window at once (for all CommunicationPorts in this VM). Must be a power of two for the code to function properly.
Constructor Detail

CommunicationPort

public CommunicationPort(java.net.InetAddress address,
                         int port,
                         ReliableHandler reliableHandler)
Creates a Communication port associated with this address and port
Method Detail

address

public java.net.InetAddress address()
the address this communication port is associated with

port

public int port()
the port this communication port is associated with

resendAll

public void resendAll()
               throws java.io.IOException
resends all messages to the address & port associated with this object that have not yet been acknowledged. Will send only messages within MAX_SENT of the leastIDSent value. If any messages are older than their timeout, a TimeoutException will be reported for them. In the course of execution, a new value for leastIDSent will be generated. It will be the least id of any message that is greater than the previous value of leastIDSent, or if none exists the least id of any message. exception IOException if there is an error in sending any of the packets.

hasReceived

public boolean hasReceived(InputPacket packet)
Checks to see if a given InputPacket has been received before.
Returns:
true if it has been recevied, false if not.

add

public boolean add(OutputPacket packet)
Adds an OutputPacket to the sending queue. This packet will be resent as necessary until it is removed.
Returns:
true if it's OK to send this packet immediately
See Also:
remove(int)

remove

public void remove(int id)
Removes an OutputPacket from the sending queue. This usually means it has been acknowledged or else it has timed out.
Parameters:
id - the id of the packet to remove.

resend

protected void resend(OutputPacket packet)
               throws java.io.IOException
Resends a particular packet