hypercast.adapters
Class UDP_UnicastAdapter

java.lang.Object
  |
  +--hypercast.adapters.UDP_UnicastAdapter
All Implemented Interfaces:
I_IPv4_UnicastAdapter, I_Stats, I_UnicastAdapter
Direct Known Subclasses:
UDP_MulticastAdapter

public class UDP_UnicastAdapter
extends java.lang.Object
implements I_IPv4_UnicastAdapter

This class implements all the functions related with a UDP unicast adapter.

There are three threads:
1. UnicastReceiver thread receives data from a unicast address and puts it into a buffer
2. UDP_MsgProcessor thread reads Message from MessageBuffer, then pass it to protocol node (I_AdapterCallback) for procession
3. UDP_TimeEventProcessor thread reads TimeEvent in this adapter, then pass it to protocol node (I_AdapterCallback) for procession. The Timer is started/stoped explicitly outside this class.

There is a FIFO style MessageBuffer in charge of storing messages received from UnicastReceiver (and MulticastReceiver if this class is extended by UDP_MulticastAdapter) threads. This class supports INETv4TwoPorts type physical address.


Field Summary
 
Fields inherited from interface hypercast.I_IPv4_UnicastAdapter
MAX_RETRIES_TO_OPEN_SOCKET
 
Fields inherited from interface hypercast.I_UnicastAdapter
CLOSED, STARTED, STARTING, STOPPED, STOPPING
 
Constructor Summary
UDP_UnicastAdapter(hypercast.HyperCastConfig config, java.net.DatagramSocket usock, java.lang.String prefix)
          Constructs a UDP_UnicastAdapter object with a HyperCastConfig and DataGramSocket object.
UDP_UnicastAdapter(hypercast.HyperCastConfig config, hypercast.I_UnicastAdapter scktAdpt, java.lang.String AddrString, java.lang.String prefix)
          Constructs a UDP_UnicastAdapter object.
 
Method Summary
 void clearTimer(java.lang.Object timerid)
          Clear a Timer event.
 void close()
          Close stops the adapter if it has not already been stopped.
 boolean createDatagramSocket(int portNum, java.net.InetAddress ipAddr)
          Creates a DatagramSocket.
 hypercast.I_PhysicalAddress createPhysicalAddress()
          Creates a new physical address.
 hypercast.I_PhysicalAddress createPhysicalAddress(byte[] byteAddressAndPorts, int offset)
          Creates physical address from a byte array of address - the inverse of toByteArray.
 hypercast.I_PhysicalAddress createPhysicalAddress(java.lang.String addrString)
          Creates physical address object from a string.
 hypercast.I_UnderlayAddress createUnderlayAddress()
          Creates a new underlay address.
 hypercast.I_UnderlayAddress createUnderlayAddress(byte[] byteAddressAndPort, int offset)
          Creates underlay address from a byte array of address - the inverse of toByteArray.
 hypercast.I_UnderlayAddress createUnderlayAddress(java.lang.String addrString)
          Creates underlay address object from a string.
 long getCurrentTime()
          Returns the time as given by System.currentTimeMillis().
 java.lang.Object getInfo()
          Get information of this adapter.
 java.lang.String getIPString(java.net.InetAddress addr)
          Get string representation of IP address in a InetAddress object.
 int getNumInQueue()
          Returns the number of unprocessed messages in BoundedBuffer.
 int getPhysicalAddressSize()
          Returns the size of physical address, when carried in a packet, recognized by this adapter.
 org.w3c.dom.Element[] getReadSchema(org.w3c.dom.Document doc, org.apache.xpath.XPath xpath)
          Return the schema element which represents the root of the sub-tree, specified by the given xpath, in read schema tree.
 org.w3c.dom.Element[] getStats(org.w3c.dom.Document doc, org.apache.xpath.XPath xpath)
          Return the result of query for the statistics specified by the given xpath.
 java.lang.String getStatsName()
          Gets the element name of this I_Stats instance.
 long getTimer(java.lang.Object timerid)
          Get a Timer event.
 int getUnderlayAddressSize()
          Returns the size of underlay address, when carried in a packet, recognized by this adapter.
 long getUReceivedBytes()
          Returns the number of received bytes of unicast packets from this adapter.
 long getUReceivedPackets()
          Returns the number of received unicast packets from this adapter.
 long getUSentBytes()
          Returns the number of bytes of unicast packets sent to this adapter.
 long getUSentPackets()
          Returns the number of unicast packets sent to this adapter.
 org.w3c.dom.Element[] getWriteSchema(org.w3c.dom.Document doc, org.apache.xpath.XPath xpath)
          Return the schema element which represents the root of the sub-tree, specified by the given xpath, in write schema tree.
 boolean ProcessingSocketInitialization(hypercast.I_UnicastAdapter ScktAdpter, int portNodeAdpt, int portScktAdpt, java.net.InetAddress ipAddr)
          Process the initialization of underlay network sockets.
 boolean Reset(java.lang.Object infoObj)
          Resets the local unicast socket.
 void sendUnicastMessage(hypercast.I_NetworkAddress dstaddr, hypercast.I_Message msg)
          Sends a message to a unicast address.
 void setCallback(hypercast.I_AdapterCallback node)
          Sets the I_AdapterCallback object for this Adapter.
 org.w3c.dom.Element[] setStats(org.w3c.dom.Document doc, org.apache.xpath.XPath xpath, org.w3c.dom.Element newValue)
          Set the statistics specified by the given xpath.
 void setStatsName(java.lang.String name)
          Assigns a name to this I_Stats instance.
 void setTimer(java.lang.Object timer_id, long time_ms)
          Set a Timer event.
 void Start()
          Starts all the threads in this Adapter
 void Stop()
          Stops all these threads on this Adapter.
 void Stop(long delay)
          Stops all these threads on this Adapter after a delay.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

UDP_UnicastAdapter

public UDP_UnicastAdapter(hypercast.HyperCastConfig config,
                          hypercast.I_UnicastAdapter scktAdpt,
                          java.lang.String AddrString,
                          java.lang.String prefix)
Constructs a UDP_UnicastAdapter object.

Parameters:
config - an HyperCastConfig object.
scktAdpt - object representing the Socket Adapter. It is set to null when creating the Socket Adapter.
AddrString - string containing physical address information.
prefix - property prefix.

UDP_UnicastAdapter

public UDP_UnicastAdapter(hypercast.HyperCastConfig config,
                          java.net.DatagramSocket usock,
                          java.lang.String prefix)
Constructs a UDP_UnicastAdapter object with a HyperCastConfig and DataGramSocket object.

Parameters:
config - an HyperCastConfig object.
usock - a DatagramSocket object.
prefix - property prefix.
Method Detail

ProcessingSocketInitialization

public boolean ProcessingSocketInitialization(hypercast.I_UnicastAdapter ScktAdpter,
                                              int portNodeAdpt,
                                              int portScktAdpt,
                                              java.net.InetAddress ipAddr)
Process the initialization of underlay network sockets.

Parameters:
ScktAdpter - A I_UnicastAdapter object representing Socket Adapter.
portNodeAdpt - port number of the socket in Node Adapter.
portScktAdpt - port number assigned to the socket in Socket Adapter.
ipAddr - IP address to which the socket is bounded.

createDatagramSocket

public boolean createDatagramSocket(int portNum,
                                    java.net.InetAddress ipAddr)
Creates a DatagramSocket.

Parameters:
portNum - the port number for the UDP unicast socket. If portN is set to 0, the port will be the minimal available port shosen by system; If portN is -1, the port number is chosen randomly.
ipAddr - IP address to which the created socket is bounded. If it is null, IP address will be chosen by system.

getIPString

public java.lang.String getIPString(java.net.InetAddress addr)
Get string representation of IP address in a InetAddress object.

Parameters:
addr - An InetAddress object.

getInfo

public java.lang.Object getInfo()
Get information of this adapter.

Specified by:
getInfo in interface I_UnicastAdapter

Reset

public boolean Reset(java.lang.Object infoObj)
Resets the local unicast socket.

Specified by:
Reset in interface I_UnicastAdapter
Parameters:
infoObj - object containing information used to finish reset.

setCallback

public void setCallback(hypercast.I_AdapterCallback node)
Sets the I_AdapterCallback object for this Adapter.

Specified by:
setCallback in interface I_UnicastAdapter
Parameters:
node - an I_AdapterCallback object.

setTimer

public void setTimer(java.lang.Object timer_id,
                     long time_ms)
Set a Timer event.

Specified by:
setTimer in interface I_UnicastAdapter
Parameters:
timer_id - a time event ID
time_ms - a time in ms

getTimer

public long getTimer(java.lang.Object timerid)
Get a Timer event.

Specified by:
getTimer in interface I_UnicastAdapter
Parameters:
timerid - a time event ID
Returns:
time in ms

clearTimer

public void clearTimer(java.lang.Object timerid)
Clear a Timer event.

Specified by:
clearTimer in interface I_UnicastAdapter
Parameters:
timerid - a time event ID

getCurrentTime

public long getCurrentTime()
Returns the time as given by System.currentTimeMillis().

Specified by:
getCurrentTime in interface I_UnicastAdapter

Start

public void Start()
Starts all the threads in this Adapter

Specified by:
Start in interface I_UnicastAdapter

Stop

public void Stop()
Stops all these threads on this Adapter. Callbacks may still occur while this function is being executed; it will only return when it can guarantee no more Callbacks will be called.

Specified by:
Stop in interface I_UnicastAdapter

Stop

public void Stop(long delay)
Stops all these threads on this Adapter after a delay. Callbacks may still occur while this function is being executed; it will only return when it can guarantee no more Callbacks will be called.

Specified by:
Stop in interface I_UnicastAdapter
Parameters:
delay - delay in milliseconds

close

public void close()
Description copied from interface: I_UnicastAdapter
Close stops the adapter if it has not already been stopped. It then closes the physical sockets. This should only be called if the adapter will no longer be used. After a close call, any further calls to adapter methods will return an IllegalStateException. If a delayed stop was called before the close method is called, then close will block until that delayed stop is completed. A closed adapter is always stopped as well.

Specified by:
close in interface I_UnicastAdapter

sendUnicastMessage

public void sendUnicastMessage(hypercast.I_NetworkAddress dstaddr,
                               hypercast.I_Message msg)
Sends a message to a unicast address.


1. Create a byte array containing the information
2. Create a DatagramPacket
3. Send Packet.

Specified by:
sendUnicastMessage in interface I_UnicastAdapter
Parameters:
dstaddr - destination physical address
msg - the message to be sent

createPhysicalAddress

public hypercast.I_PhysicalAddress createPhysicalAddress(byte[] byteAddressAndPorts,
                                                         int offset)
Creates physical address from a byte array of address - the inverse of toByteArray. The first 4 bytes reprsent the IP address, and the last 8 bytes represent the Node Adapter port number and Socket Adapter port number.

Specified by:
createPhysicalAddress in interface I_UnicastAdapter
Parameters:
byteAddressAndPorts - an byte array containing a physical address
offset - the position in byteAddressAndPort from where availabe data is read.
Returns:
a new instance of INETv4TwoPorts
See Also:
INETv4TwoPorts.toByteArray()

createPhysicalAddress

public hypercast.I_PhysicalAddress createPhysicalAddress(java.lang.String addrString)
Creates physical address object from a string.

Specified by:
createPhysicalAddress in interface I_UnicastAdapter
Parameters:
addrString - an string representing a physical address.
Returns:
a new instance of INETv4TwoPorts

createPhysicalAddress

public hypercast.I_PhysicalAddress createPhysicalAddress()
Creates a new physical address.

Specified by:
createPhysicalAddress in interface I_UnicastAdapter
Returns:
a new instance of INETv4TwoPorts

getPhysicalAddressSize

public int getPhysicalAddressSize()
Returns the size of physical address, when carried in a packet, recognized by this adapter.

Specified by:
getPhysicalAddressSize in interface I_UnicastAdapter

createUnderlayAddress

public hypercast.I_UnderlayAddress createUnderlayAddress(byte[] byteAddressAndPort,
                                                         int offset)
Creates underlay address from a byte array of address - the inverse of toByteArray. The first 4 bytes reprsent the IP address, and the last 4 bytes represent the port number.

Specified by:
createUnderlayAddress in interface I_UnicastAdapter
Parameters:
byteAddressAndPort - an byte array containing a physical address
offset - the position in byteAddressAndPort from where availabe data is read.
Returns:
a new instance of INETv4AndOnePort
See Also:
INETv4OnePort.toByteArray()

createUnderlayAddress

public hypercast.I_UnderlayAddress createUnderlayAddress(java.lang.String addrString)
Creates underlay address object from a string.

Specified by:
createUnderlayAddress in interface I_UnicastAdapter
Parameters:
addrString - an string representing a physical address.
Returns:
a new instance of INETv4AndOnePort

createUnderlayAddress

public hypercast.I_UnderlayAddress createUnderlayAddress()
Creates a new underlay address.

Specified by:
createUnderlayAddress in interface I_UnicastAdapter
Returns:
a new instance of INETv4AndOnePort

getUnderlayAddressSize

public int getUnderlayAddressSize()
Returns the size of underlay address, when carried in a packet, recognized by this adapter.

Specified by:
getUnderlayAddressSize in interface I_UnicastAdapter

getUReceivedPackets

public long getUReceivedPackets()
Returns the number of received unicast packets from this adapter.


getUReceivedBytes

public long getUReceivedBytes()
Returns the number of received bytes of unicast packets from this adapter.


getUSentPackets

public long getUSentPackets()
Returns the number of unicast packets sent to this adapter.


getUSentBytes

public long getUSentBytes()
Returns the number of bytes of unicast packets sent to this adapter.


getNumInQueue

public int getNumInQueue()
Returns the number of unprocessed messages in BoundedBuffer.


getStatsName

public java.lang.String getStatsName()
Description copied from interface: I_Stats
Gets the element name of this I_Stats instance. The element name is originally assigned from the attribute StstsName in the configuration file, and can be changed by this method.

Specified by:
getStatsName in interface I_Stats
See Also:
I_Stats.getStatsName()

setStatsName

public void setStatsName(java.lang.String name)
Description copied from interface: I_Stats
Assigns a name to this I_Stats instance. The assigned name becomes the tag of the element associated to this I_Stats instance on the statistics tree.

Specified by:
setStatsName in interface I_Stats
Parameters:
name - the name assigned to the I_Stats instance.
See Also:
I_Stats.setStatsName(String)

getStats

public org.w3c.dom.Element[] getStats(org.w3c.dom.Document doc,
                                      org.apache.xpath.XPath xpath)
                               throws HyperCastStatsException
Return the result of query for the statistics specified by the given xpath.

Specified by:
getStats in interface I_Stats
Parameters:
doc - Document used for create new elements or nodes.
xpath - XPath instance represents the statistics to be queried.
Throws:
HyperCastStatsException - If the xpath does not specify a valid subtree.
See Also:
I_Stats.getStats(org.w3c.dom.Document, org.apache.xpath.XPath)

setStats

public org.w3c.dom.Element[] setStats(org.w3c.dom.Document doc,
                                      org.apache.xpath.XPath xpath,
                                      org.w3c.dom.Element newValue)
                               throws HyperCastStatsException
Set the statistics specified by the given xpath. The value actually set is returned.

Specified by:
setStats in interface I_Stats
Parameters:
doc - Document used for create new elements or nodes.
xpath - XPath instance represents the statistics to be queried.
newValue - Element representing the value or sub-tree to be set.
Throws:
HyperCastStatsException - If the xpath does not specify a valid subtree or if any part of the the subtree is read-only, or if the newValue does not conform with the expected format.
See Also:
I_Stats.setStats(org.w3c.dom.Document, org.apache.xpath.XPath, org.w3c.dom.Element)

getReadSchema

public org.w3c.dom.Element[] getReadSchema(org.w3c.dom.Document doc,
                                           org.apache.xpath.XPath xpath)
                                    throws HyperCastStatsException
Return the schema element which represents the root of the sub-tree, specified by the given xpath, in read schema tree.

Specified by:
getReadSchema in interface I_Stats
Parameters:
doc - Document used for create new elements or nodes.
xpath - XPath instance representing the statistics which is the root of the sub-tree to be queried.
Throws:
HyperCastStatsException - If some part of the specified subtree does not correctly implement this interface method.
See Also:
I_Stats.getReadSchema(Document, XPath)

getWriteSchema

public org.w3c.dom.Element[] getWriteSchema(org.w3c.dom.Document doc,
                                            org.apache.xpath.XPath xpath)
                                     throws HyperCastStatsException
Return the schema element which represents the root of the sub-tree, specified by the given xpath, in write schema tree.

Specified by:
getWriteSchema in interface I_Stats
Parameters:
doc - Document used for create new elements or nodes.
xpath - XPath instance representing the statistics which is the root of the sub-tree to be queried.
Throws:
HyperCastStatsException - If some part of the specified subtree does not correctly implement this interface method.
See Also:
I_Stats.getWriteSchema(Document, XPath)