protocol
Class DatagramChannel

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

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

Binds the UDP port. Each peer is associated with one DatagramChannel object.

Author:
Mikica B Kocic

Field Summary
private static int INBOUND_UDP_BUFFER_SIZE
          The default size for the inbound UDP buffer
private  int localPort
          The local UDP port where datagram socket is bound
private  java.lang.Thread pduReceiverThread
          The UDP receiver worker thread
private  RemotePeer remotePeer
          Current remote peer receiving datagrams from UDP socket
private  boolean running
          Indicates that worker thread should be running (receiving datagrams)
private  java.net.DatagramSocket udpReceiver
          The UDP receiver socket
private  SymmetricCipher usedPduCipher
          Currently used symmetric cipher
 
Constructor Summary
DatagramChannel(int localPort)
          Constructor for the DatagramChannel object
 
Method Summary
 void addNewPeer(RemotePeer remotePeer)
          Adds new peer to receive incoming PDUs
 int getLocalPort()
          Returns used local UDP port.
 RemotePeer getRemotePeer()
          Returns the remote peer
 SymmetricCipher getUsedSymmetricCipher()
          Gets current cipher used for PDU ciphering
 boolean hasRemotePeer()
          Returns if there is active remote peer
 boolean isPearDead(int maxIdleTimeMillis)
          Returns if peer seems to be dead (we are not receiving PDUs from it).
protected  void packetDump(byte[] octets, int len, java.net.InetAddress addr, int port, boolean incoming)
          Dumps information of a frame (in bytes) to standard error.
 void removePeer()
          Detaches peer and all its call from the UDP receiver
 void run()
          Receives (and deciphers) PDUs from remote peers in a loop.
 void send(OctetBuffer pdu, java.net.InetAddress peerAddr, int peerPort)
          Encrypts and sends PDUs to remote peer
 void stop()
          Stops PDU receiver thread
 void useSymmetricCipher(SymmetricCipher cipherEngine)
          Sets cipher to be used for PDU ciphering
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

INBOUND_UDP_BUFFER_SIZE

private static final int INBOUND_UDP_BUFFER_SIZE
The default size for the inbound UDP buffer

See Also:
Constant Field Values

localPort

private int localPort
The local UDP port where datagram socket is bound


udpReceiver

private java.net.DatagramSocket udpReceiver
The UDP receiver socket


pduReceiverThread

private java.lang.Thread pduReceiverThread
The UDP receiver worker thread


running

private volatile boolean running
Indicates that worker thread should be running (receiving datagrams)


remotePeer

private RemotePeer remotePeer
Current remote peer receiving datagrams from UDP socket


usedPduCipher

private SymmetricCipher usedPduCipher
Currently used symmetric cipher

Constructor Detail

DatagramChannel

public DatagramChannel(int localPort)
Constructor for the DatagramChannel object

Parameters:
localPort - local UDP port receiving peer's PDUs
Throws:
java.net.SocketException - Thrown if the UDP socket cannot be created
java.net.UnknownHostException - Thrown if the remoteHost cannot be resolved
Method Detail

getLocalPort

public int getLocalPort()
Returns used local UDP port.


useSymmetricCipher

public void useSymmetricCipher(SymmetricCipher cipherEngine)
Sets cipher to be used for PDU ciphering


getUsedSymmetricCipher

public SymmetricCipher getUsedSymmetricCipher()
Gets current cipher used for PDU ciphering


addNewPeer

public void addNewPeer(RemotePeer remotePeer)
Adds new peer to receive incoming PDUs


hasRemotePeer

public boolean hasRemotePeer()
Returns if there is active remote peer


getRemotePeer

public RemotePeer getRemotePeer()
Returns the remote peer


isPearDead

public boolean isPearDead(int maxIdleTimeMillis)
Returns if peer seems to be dead (we are not receiving PDUs from it).


removePeer

public void removePeer()
Detaches peer and all its call from the UDP receiver


stop

public void stop()
Stops PDU receiver thread


run

public void run()
Receives (and deciphers) PDUs from remote peers in a loop. Dispatches inbound PDUs to associated instance of the RemotePeer that will handle PDUs.

Specified by:
run in interface java.lang.Runnable

send

public void send(OctetBuffer pdu,
                 java.net.InetAddress peerAddr,
                 int peerPort)
Encrypts and sends PDUs to remote peer


packetDump

protected void packetDump(byte[] octets,
                          int len,
                          java.net.InetAddress addr,
                          int port,
                          boolean incoming)
Dumps information of a frame (in bytes) to standard error.

Parameters:
octets - The octets of the in- or outgoing PDU
len - The size of PDU
addr - The remote host address
port - The port number
incoming - Indicates if it is inbound (true) or outgoing (false) PDU