protocol
Class RemotePeer

java.lang.Object
  extended by protocol.RemotePeer
All Implemented Interfaces:
java.lang.Runnable

public class RemotePeer
extends java.lang.Object
implements java.lang.Runnable

Encapsulates the link between the UDP channel and a CallContext. UDP channel receives packets from all remote peers and dispatches them to particular RemotePeer handler. RemotePeer might have multiple calls in real PBX, howerever, this implementation allows only single CallContext per RemotePeer.

Author:
Mikica B Kocic

Field Summary
private  CallContext call
          The call context
protected  java.util.Vector<byte[]> inboundPDUs
          The queue of incoming PDUs from remote peer via our UDP channel
private  long lastReceiverTimestamp
          The time-stamp of the last received PDU
protected  java.lang.Thread pduReceiverThread
          The receiving process thread.
private  java.net.InetAddress remoteAddr
          Remote IP address where to send PDUs
private  int remotePort
          Remote UDP port where to send PDUs
private  java.lang.String remoteUserId
          PBXClient's (chat server's) User ID of the remote peer
private  DatagramChannel socket
          The UDP channel
private  boolean transmitting
          Indicates whether communication with peer is active or not
 
Constructor Summary
RemotePeer(DatagramChannel socket, java.lang.String remoteUserId, java.net.InetAddress remoteAddr, int remotePort)
          Constructor for the RemotePeer object
 
Method Summary
 void addIncomingPDU(byte[] data)
          Adds an incoming PDUs (as bytes) to the PDUs queue.
 void addNewCall(CallContext call)
          Adds the new (not owned) call to the peer.
 void cleanUp()
          Stops transmitting and c cleans up resources (local and used by the calls).
 java.lang.String getRemoteUserId()
          Returns remote name (remote userid) of the peer
(package private)  void parsePDU(byte[] octets)
          Deals with newly received PDU octets.
 long receiverIdleTime()
          Returns elapsed time since last received packet
 void run()
          Manages the incoming PDUs stored in the PDUs queue.
 void send(OctetBuffer pdu)
          Sends packet to remote peer over datagram channel
private  void startReceiver()
          This method starts the receiver thread for inbound PDUs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

socket

private DatagramChannel socket
The UDP channel


call

private CallContext call
The call context


remoteUserId

private java.lang.String remoteUserId
PBXClient's (chat server's) User ID of the remote peer


remoteAddr

private java.net.InetAddress remoteAddr
Remote IP address where to send PDUs


remotePort

private int remotePort
Remote UDP port where to send PDUs


inboundPDUs

protected java.util.Vector<byte[]> inboundPDUs
The queue of incoming PDUs from remote peer via our UDP channel


pduReceiverThread

protected java.lang.Thread pduReceiverThread
The receiving process thread.


transmitting

private volatile boolean transmitting
Indicates whether communication with peer is active or not


lastReceiverTimestamp

private long lastReceiverTimestamp
The time-stamp of the last received PDU

Constructor Detail

RemotePeer

public RemotePeer(DatagramChannel socket,
                  java.lang.String remoteUserId,
                  java.net.InetAddress remoteAddr,
                  int remotePort)
Constructor for the RemotePeer object

Parameters:
socket - instance of DatagramChannel used for communication
remoteUserId - peer's user ID
remoteAddr - peer's IP address
remotePort - peer's UDP port listening for ours PDUs
Method Detail

getRemoteUserId

public java.lang.String getRemoteUserId()
Returns remote name (remote userid) of the peer


startReceiver

private void startReceiver()
This method starts the receiver thread for inbound PDUs.


receiverIdleTime

public long receiverIdleTime()
Returns elapsed time since last received packet


addIncomingPDU

public void addIncomingPDU(byte[] data)
Adds an incoming PDUs (as bytes) to the PDUs queue. We are still on the recv thread; this data was received by binder and passed to us via friend. The PDUs are stored in the queue and we deal with them on our own thread to relief the recv thread. In other words, this is the last thing we do on the recv thread!

Parameters:
data - The PDU octets

run

public void run()
Manages the incoming PDUs stored in the PDUs queue. This thread is started by startReceiver() and run separately from the binder's receiver thread.

Specified by:
run in interface java.lang.Runnable

parsePDU

void parsePDU(byte[] octets)
Deals with newly received PDU octets. This method encapsulates them into a instance of PDU class, deal with internal counters, sends an acknowledgement and notifies the PDU it has arrived.


addNewCall

public void addNewCall(CallContext call)
Adds the new (not owned) call to the peer.


cleanUp

public void cleanUp()
Stops transmitting and c cleans up resources (local and used by the calls).


send

public void send(OctetBuffer pdu)
Sends packet to remote peer over datagram channel