hypercast.adapters
Class TCP_UDP_Adapter

java.lang.Object
  |
  +--hypercast.adapters.TCP_UDP_Adapter
All Implemented Interfaces:
I_IPv4_UnicastAdapter, I_MulticastAdapter, I_Stats, I_UnicastAdapter

public class TCP_UDP_Adapter
extends java.lang.Object
implements I_MulticastAdapter

This class implements an adapter which supports the folowing three kinds of data transmission: 1. UDP unicast; 2. TCP unicast; 3. UDP multicast. This class employs a UDP_MulticastAdapter object and TCP_UnicastAdapter object to fulfill data transmission task, instead of implementing all required functions by itself. This class has its own Timer object and Timer processor. This class defines a INETv4TwoPorts physical address in which the "port-plus-one" is applied.


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
TCP_UDP_Adapter(hypercast.HyperCastConfig c, hypercast.I_UnicastAdapter scktAdpt, java.lang.String AddrString, java.lang.String prefix)
          Constructs a TCP_UDP_Adapter object with an HyperCastConfig object and an I_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 createDatagramServerSocket(int portNum, java.net.InetAddress ipAddr)
          Creates a DatagramSocket object and a ServerSocket object.
 hypercast.I_PhysicalAddress createPhysicalAddress()
          Creates a new physical address.
 hypercast.I_PhysicalAddress createPhysicalAddress(byte[] byteAddressAndPort, 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()
          Get current system time.
 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.
 hypercast.I_PhysicalAddress getMulticastAddress()
          Get the multicast address used by this adpater.
 int getPhysicalAddressSize()
          Returns the size of physical address, when carried in a packet, of an I_UnicastAdapter object.
 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.
 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 InitializeDatagramServerSocket(hypercast.I_UnicastAdapter ScktAdpter, hypercast.HyperCastConfig config, java.lang.String AddrString)
          Initialize DatagramSocket and ServerSocket used by this adapter.
 boolean Reset(java.lang.Object SocketObj)
          Resets the local unicast socket with a object which implemnents an unicast socket.
 void sendMulticastMessage(hypercast.I_Message msg)
          Sends a message to a multicast address.
 void sendUnicastMessage(hypercast.I_NetworkAddress dstaddr, hypercast.I_Message msg)
          Sends a message to a unicast address.
 void setCallback(hypercast.I_AdapterCallback cb)
          Set I_AdapterCallback object.
 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

TCP_UDP_Adapter

public TCP_UDP_Adapter(hypercast.HyperCastConfig c,
                       hypercast.I_UnicastAdapter scktAdpt,
                       java.lang.String AddrString,
                       java.lang.String prefix)
Constructs a TCP_UDP_Adapter object with an HyperCastConfig object and an I_UnicastAdapter object.

Parameters:
c - a 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.
Method Detail

InitializeDatagramServerSocket

public boolean InitializeDatagramServerSocket(hypercast.I_UnicastAdapter ScktAdpter,
                                              hypercast.HyperCastConfig config,
                                              java.lang.String AddrString)
Initialize DatagramSocket and ServerSocket used by this adapter.

Parameters:
ScktAdpter - A I_UnicastAdapter object representing Socket Adapter. ScktAdpter is null when this method is called in Socket Adapter.
config - a HyperCastConfig object.
AddrString - A string carring physical address information.

createDatagramServerSocket

public boolean createDatagramServerSocket(int portNum,
                                          java.net.InetAddress ipAddr)
Creates a DatagramSocket object and a ServerSocket object.

Parameters:
portNum - the port number for the TCP and UDP unicast sockets. 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 binded.

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 SocketObj)
Resets the local unicast socket with a object which implemnents an unicast socket.

Specified by:
Reset in interface I_UnicastAdapter
Parameters:
SocketObj - object passed to do reset.

setCallback

public void setCallback(hypercast.I_AdapterCallback cb)
Set I_AdapterCallback object.

Specified by:
setCallback in interface I_UnicastAdapter
Parameters:
cb - an object of type I_AdapterCallback

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()
Get current system time.

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

createPhysicalAddress

public hypercast.I_PhysicalAddress createPhysicalAddress(byte[] byteAddressAndPort,
                                                         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:
byteAddressAndPort - 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, of an I_UnicastAdapter object.

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

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)

sendUnicastMessage

public void sendUnicastMessage(hypercast.I_NetworkAddress dstaddr,
                               hypercast.I_Message msg)
Sends a message to a unicast address. There is no protpcol specified in the parameters, by default the message is sent out using UDP unicast protocol.

Specified by:
sendUnicastMessage in interface I_UnicastAdapter

sendMulticastMessage

public void sendMulticastMessage(hypercast.I_Message msg)
Sends a message to a multicast address. It is sent out through the UDP_MulticastAdapter object contained in this adapter.

Specified by:
sendMulticastMessage in interface I_MulticastAdapter

getMulticastAddress

public hypercast.I_PhysicalAddress getMulticastAddress()
Get the multicast address used by this adpater.

Specified by:
getMulticastAddress in interface I_MulticastAdapter