Encapsulates the link between the UDP channel and a CallContext. More...
Public Member Functions | |
RemotePeer (DatagramChannel socket, String remoteUserId, InetAddress remoteAddr, int remotePort) | |
Constructor for the RemotePeer object. | |
String | getRemoteUserId () |
Returns remote name (remote userid) of the peer. | |
synchronized long | receiverIdleTime () |
Returns elapsed time since last received packet. | |
synchronized void | addIncomingPDU (byte[] data) |
Adds an incoming PDUs (as bytes) to the PDUs queue. | |
void | run () |
Manages the incoming PDUs stored in 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). | |
void | send (OctetBuffer pdu) |
Sends packet to remote peer over datagram channel. | |
Protected Attributes | |
Vector< byte[]> | inboundPDUs = null |
The queue of incoming PDUs from remote peer via our UDP channel. | |
Thread | pduReceiverThread = null |
The receiving process thread. | |
Package Functions | |
void | parsePDU (byte[] octets) |
Deals with newly received PDU octets. | |
Private Member Functions | |
synchronized void | startReceiver () |
This method starts the receiver thread for inbound PDUs. | |
Private Attributes | |
DatagramChannel | socket = null |
The UDP channel. | |
CallContext | call = null |
The call context. | |
String | remoteUserId = null |
PBXClient's (chat server's) User ID of the remote peer. | |
InetAddress | remoteAddr = null |
Remote IP address where to send PDUs. | |
int | remotePort = -1 |
Remote UDP port where to send PDUs. | |
volatile boolean | transmitting = false |
Indicates whether communication with peer is active or not. | |
long | lastReceiverTimestamp = 0 |
The time-stamp of the last received PDU. |
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.
Definition at line 19 of file RemotePeer.java.
protocol.RemotePeer.RemotePeer | ( | DatagramChannel | socket, |
String | remoteUserId, | ||
InetAddress | remoteAddr, | ||
int | remotePort | ||
) |
Constructor for the RemotePeer object.
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 |
Definition at line 56 of file RemotePeer.java.
References protocol.DatagramChannel.addNewPeer(), protocol.RemotePeer.call, protocol.RemotePeer.inboundPDUs, protocol.RemotePeer.remoteAddr, protocol.RemotePeer.remotePort, protocol.RemotePeer.remoteUserId, protocol.RemotePeer.socket, and protocol.RemotePeer.startReceiver().
{ this.remoteUserId = remoteUserId; this.remoteAddr = remoteAddr; this.remotePort = remotePort; this.call = null; this.inboundPDUs = new Vector<byte[]> (); synchronized( socket ) { this.socket = socket; this.socket.addNewPeer( this ); if ( this.remoteAddr != null && this.remotePort > 0 && this.remotePort <= 65535 ) { this.startReceiver (); } } }
synchronized void protocol.RemotePeer.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!
data | The PDU octets |
Definition at line 126 of file RemotePeer.java.
References protocol.RemotePeer.inboundPDUs, protocol.RemotePeer.lastReceiverTimestamp, and protocol.RemotePeer.transmitting.
Referenced by protocol.DatagramChannel.run().
{ if ( ! this.transmitting || data == null ) { return; } this.lastReceiverTimestamp = System.currentTimeMillis (); this.inboundPDUs.addElement( data ); this.notifyAll (); }
void protocol.RemotePeer.addNewCall | ( | CallContext | call ) |
Adds the new (not owned) call to the peer.
Definition at line 228 of file RemotePeer.java.
References protocol.RemotePeer.call, protocol.CallContext.setDestinationCallNumber(), and protocol.CallContext.setSourceCallNumber().
Referenced by protocol.CallContext.CallContext().
void protocol.RemotePeer.cleanUp | ( | ) |
Stops transmitting and c cleans up resources (local and used by the calls).
Definition at line 243 of file RemotePeer.java.
References protocol.RemotePeer.call, protocol.CallContext.cleanUp(), and protocol.RemotePeer.transmitting.
Referenced by protocol.DatagramChannel.removePeer().
{ if ( this.call != null ) { this.call.cleanUp (); this.transmitting = false; } }
String protocol.RemotePeer.getRemoteUserId | ( | ) |
Returns remote name (remote userid) of the peer.
Definition at line 83 of file RemotePeer.java.
References protocol.RemotePeer.remoteUserId.
Referenced by CryptoPhoneApp.executeCommand(), CryptoPhoneApp.mainTimerEvent(), and CryptoPhoneApp.sendInstantMessage().
{ return remoteUserId; }
void protocol.RemotePeer.parsePDU | ( | byte[] | octets ) | [package] |
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.
Definition at line 200 of file RemotePeer.java.
References protocol.RemotePeer.call, protocol.ProtocolDataUnit.create(), protocol.ProtocolDataUnit.destinationCallNumber, protocol.ProtocolDataUnit.onArrivedPDU(), and protocol.ProtocolDataUnit.sourceCallNumber.
Referenced by protocol.RemotePeer.run().
{ if ( this.call != null && octets != null ) { /* Parse PDU */ ProtocolDataUnit pdu = ProtocolDataUnit.create( this.call, octets ); /* Dispatch PDU if it is tagged with valid call numbers. * Note: Dispatching is hard-coded here, but this place might be * a core for real PBX call-id handling. */ if ( pdu != null && pdu.destinationCallNumber == 0x5926 && pdu.sourceCallNumber == 0x3141 ) { pdu.onArrivedPDU (); } else if ( pdu != null ) { Log.warn( "Ignored PDU with destCall# " + pdu.destinationCallNumber + ", srcCalL# " + pdu.sourceCallNumber ); } } }
synchronized long protocol.RemotePeer.receiverIdleTime | ( | ) |
Returns elapsed time since last received packet.
Definition at line 112 of file RemotePeer.java.
References protocol.RemotePeer.lastReceiverTimestamp.
Referenced by protocol.DatagramChannel.isPearDead().
{ return System.currentTimeMillis () - this.lastReceiverTimestamp; }
void protocol.RemotePeer.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.
Definition at line 142 of file RemotePeer.java.
References protocol.RemotePeer.call, protocol.RemotePeer.inboundPDUs, protocol.RemotePeer.parsePDU(), protocol.RemotePeer.socket, and protocol.RemotePeer.transmitting.
{ Log.trace( "Thread started" ); while ( this.transmitting ) { Object[] pdusToSend = new Object[0]; synchronized( this ) { try { this.wait (); } catch( InterruptedException e ) { /* ignored */ } int pduCount = this.inboundPDUs.size (); // Do some smart stuff here? (limit the take to only 20 PDUs?) // You'd hope that normally we'd get 1 or maybe 2 PDUs here. // if ( pduCount > 0 ) { pdusToSend = new Object[ pduCount ]; for ( int i = 0; i < pduCount; ++i ) { pdusToSend[i] = this.inboundPDUs.elementAt(i); } this.inboundPDUs.removeAllElements (); } } // After releasing the lock, let's deal with the list... // we are now on the thread of the call, any time we waste is // our own. // should really sort these into sequence before we dispose of them. for( int i = 0; i < pdusToSend.length; ++i ) { try { parsePDU( (byte[]) pdusToSend[i] ); } catch( Throwable e ) { Log.error( "ParsePDU failed; " + e.toString () ); Log.where (); } } } this.socket = null; this.call = null; }
void protocol.RemotePeer.send | ( | OctetBuffer | pdu ) |
Sends packet to remote peer over datagram channel.
Definition at line 255 of file RemotePeer.java.
References protocol.RemotePeer.remoteAddr, protocol.RemotePeer.remotePort, protocol.DatagramChannel.send(), protocol.RemotePeer.socket, and protocol.RemotePeer.transmitting.
Referenced by protocol.CallContext.send().
{ if ( this.transmitting ) { this.socket.send( pdu, this.remoteAddr, this.remotePort ); } }
synchronized void protocol.RemotePeer.startReceiver | ( | ) | [private] |
This method starts the receiver thread for inbound PDUs.
Definition at line 91 of file RemotePeer.java.
References protocol.RemotePeer.call, protocol.RemotePeer.lastReceiverTimestamp, protocol.RemotePeer.pduReceiverThread, protocol.RemotePeer.remoteAddr, protocol.RemotePeer.remotePort, protocol.CallContext.resetClock(), and protocol.RemotePeer.transmitting.
Referenced by protocol.RemotePeer.RemotePeer().
{ this.transmitting = true; this.lastReceiverTimestamp = System.currentTimeMillis (); this.pduReceiverThread = new Thread( this, "Peer-" + remoteAddr.getHostAddress() + ":" + remotePort ); this.pduReceiverThread.setPriority( Thread.MAX_PRIORITY - 1 ); if ( this.call != null ) { this.call.resetClock (); } this.pduReceiverThread.start (); }
CallContext protocol.RemotePeer.call = null [private] |
The call context.
Definition at line 25 of file RemotePeer.java.
Referenced by protocol.RemotePeer.addNewCall(), protocol.RemotePeer.cleanUp(), protocol.RemotePeer.parsePDU(), protocol.RemotePeer.RemotePeer(), protocol.RemotePeer.run(), and protocol.RemotePeer.startReceiver().
Vector<byte[]> protocol.RemotePeer.inboundPDUs = null [protected] |
The queue of incoming PDUs from remote peer via our UDP channel.
Definition at line 37 of file RemotePeer.java.
Referenced by protocol.RemotePeer.addIncomingPDU(), protocol.RemotePeer.RemotePeer(), and protocol.RemotePeer.run().
long protocol.RemotePeer.lastReceiverTimestamp = 0 [private] |
The time-stamp of the last received PDU.
Definition at line 46 of file RemotePeer.java.
Referenced by protocol.RemotePeer.addIncomingPDU(), protocol.RemotePeer.receiverIdleTime(), and protocol.RemotePeer.startReceiver().
Thread protocol.RemotePeer.pduReceiverThread = null [protected] |
The receiving process thread.
Definition at line 40 of file RemotePeer.java.
Referenced by protocol.RemotePeer.startReceiver().
InetAddress protocol.RemotePeer.remoteAddr = null [private] |
Remote IP address where to send PDUs.
Definition at line 31 of file RemotePeer.java.
Referenced by protocol.RemotePeer.RemotePeer(), protocol.RemotePeer.send(), and protocol.RemotePeer.startReceiver().
int protocol.RemotePeer.remotePort = -1 [private] |
Remote UDP port where to send PDUs.
Definition at line 34 of file RemotePeer.java.
Referenced by protocol.RemotePeer.RemotePeer(), protocol.RemotePeer.send(), and protocol.RemotePeer.startReceiver().
String protocol.RemotePeer.remoteUserId = null [private] |
PBXClient's (chat server's) User ID of the remote peer.
Definition at line 28 of file RemotePeer.java.
Referenced by protocol.RemotePeer.getRemoteUserId(), and protocol.RemotePeer.RemotePeer().
DatagramChannel protocol.RemotePeer.socket = null [private] |
The UDP channel.
Definition at line 22 of file RemotePeer.java.
Referenced by protocol.RemotePeer.RemotePeer(), protocol.RemotePeer.run(), and protocol.RemotePeer.send().
volatile boolean protocol.RemotePeer.transmitting = false [private] |
Indicates whether communication with peer is active or not.
Definition at line 43 of file RemotePeer.java.
Referenced by protocol.RemotePeer.addIncomingPDU(), protocol.RemotePeer.cleanUp(), protocol.RemotePeer.run(), protocol.RemotePeer.send(), and protocol.RemotePeer.startReceiver().