java.net
Class DConnector

java.lang.Object
java.util.AbstractCollection
java.util.AbstractList
java.util.Vector
java.net.DConnector
- All Implemented Interfaces:
- java.lang.Cloneable, java.util.Collection, java.util.List, java.util.RandomAccess, java.lang.Runnable, java.io.Serializable
- public class DConnector
- extends java.util.Vector
- implements java.lang.Runnable
This class manage the compression layer for the datagram scenario.
It's a thread responsible for packet receiving and handling.
It can guess if the packet is compressed and so should be decompressed (or not) or if it's
an ack for a previously sent packet and should be used to compute statistics.
- Version:
- 1.0
|
Method Summary |
protected void |
addPacket(DatagramPacket packet)
This method clones a received packet and adds it to the packet queue so it can be later retrieved. |
void |
decompress(DatagramPacket p)
This method is responsible for data decompression. |
void |
getPacket(DatagramPacket p)
This method remove a packet from the queue and copy data into the passed datagram. |
boolean |
hasPacket()
This method checks if the system queue is empty or not, so it's used to see if there's some packet
available. |
void |
run()
This is the main loop and it's responsible for all packet receiving and handling activities. |
void |
send(DatagramPacket p)
This method is invoked by the ZDatagramSocketImpl#send 55 method
to perform the real data sending. |
void |
sendAck(ZObject received,
DatagramPacket packet)
This method is responsible for send ack when a packet is received. |
| Methods inherited from class java.util.Vector |
add, add, addAll, addAll, addElement, capacity, clear, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, lastElement, lastIndexOf, lastIndexOf, remove, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeRange, retainAll, set, setElementAt, setSize, size, subList, toArray, toArray, toString, trimToSize |
impl
protected ZDatagramSocketImpl impl
- The socket implementation reference through which we send and receive raw packets.
myself
protected java.lang.Thread myself
notStop
protected boolean notStop
- The main loop is driven by the value of that field. If it becomes true, the loop ends and the thread
exit.
listeners
protected java.util.Hashtable listeners
- List of objects waiting for ack. This hashtable is indexed by sequence number
and contains objects of Listener type.
Every Listener waits for a single ack packet and is responsible for the update
of the statistical interpolator.
compressor
protected Compressor compressor
- Reference to the real compression engine that must follow the
Compressor interface.
- See Also:
- Compressor,
DeflatingCompressor
decompressor
protected Decompressor decompressor
- Reference to the real decompression engine that must follow the
Decompressor interface.
- See Also:
- Decompressor,
InflatingDecompressor
interpolator
protected Interpolator interpolator
- Reference to the real statistical modeler that must follow the
Interpolator interface.
- See Also:
- Interpolator,
LinearInterpolator
container
protected java.io.ByteArrayOutputStream container
deflater
protected java.util.zip.DeflaterOutputStream deflater
DConnector
public DConnector(ZDatagramSocketImpl impl)
throws java.lang.Exception
- This is the only constructor. It's responsible for command-line options
gathering through the properties set.
Actually recognized property set is:
- compressor - to load a different compression engine (default: DeflatingCompressor)
- decompressor - to load a different decompression engine (default: InflatingDecompressor)
- interpolator - to load a different statistical modeler (default: LinearInterpolator)
Classes referenced through properties must be present into the classpath or must be loadable
through invocation of the standard method {#link java.lang.Class#forName(String) forName}.
The method getLevels() 55 is used to retrieve the number of
supported compression level and to initialize the interpolator using the appropriate number of levels.
Finally, the main loop is started through thread creation.
addPacket
protected void addPacket(DatagramPacket packet)
- This method clones a received packet and adds it to the packet queue so it can be later retrieved.
This method is internal use only.
run
public void run()
- This is the main loop and it's responsible for all packet receiving and handling activities.
It can be outlined as follows:
- the raw packet is received through the real socket implementation,
- it's decoded through the class ZObject (that throws AckException in case of an ack packet),
- if it's a compressed packet, it's decompressed and queued and an ack is sent back
- if it's an uncompressed packet, no ack is required because the packet it's not sent by our
socket implementation so the packet simply gets queued,
- if it's an ack packet, the relative listener is awakened,
- any other situation causes the thread to yield, letting the scheduler do its job without
overloading the CPU with an empty while loop.
- Specified by:
run in interface java.lang.Runnable
sendAck
public void sendAck(ZObject received,
DatagramPacket packet)
throws java.io.IOException
- This method is responsible for send ack when a packet is received.
It guess the return address from the second argument, the reference to the received packet and
the sequence number from the decoded ZObject (contained in the same datagram).
It prepares a new ack-type ZObject, ask it to encode itself and send back the so created packet.
hasPacket
public boolean hasPacket()
- This method checks if the system queue is empty or not, so it's used to see if there's some packet
available.
getPacket
public void getPacket(DatagramPacket p)
- This method remove a packet from the queue and copy data into the passed datagram.
send
public void send(DatagramPacket p)
throws java.io.IOException
- This method is invoked by the ZDatagramSocketImpl#send 55 method
to perform the real data sending.
Basically it performs the following tasks:
- ask the interpolator to compute a suitable compression level based on data buffer
dimension,
- set the compression engine to work at that level
- compress data and acquire compression time
- build ZObject, serialize it and send
- install a new Listener object, waiting for ack to update the interpolator
decompress
public void decompress(DatagramPacket p)
throws NoMagicException,
java.io.IOException
- This method is responsible for data decompression. It uses the loaded decompression engine
(or InflatingDecompressor if not provided) and
a ByteArrayInputStream (that renders a byte array as an input stream) to build an
InflaterInputStream that "reads" compressed data and write "uncompressed data".