|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmil.navy.nps.dis.PduElement
mil.navy.nps.dis.ProtocolDataUnit
Top-level abstract (uninstantiated) class for all PDUs.
I generally declare instance variables to be protected, which means that they are directly accessible from this class and all subclasses. Those who believe in full-jackboot mode will want them declared private, so subclasses can't access them either.
The accessor methods (getProtocolVersion et al) are declared public, so that anyone, including those outside this package, can access them. You should always go through accessor methods when setting values inside an object. Direct access from outside the class can cause mysterious errors that are very hard to track down.
Note that, due to garbage collection, we don't have to worry about the status of orphaned instance variables. For example, this operation will cause memory leaks in C++ or Obj-C:
protocolVersion = newProtocolVersion;In C++, this operation would leave an orphaned object; the _old_ object that was held by protocolVersion would have no valid pointers to it, but would still take up memory. With GC, the old, orphaned protocolVersion's memory will be scavanged and returned to use.
Accessor methods return a copy of the thing they're getting, not the actual thing. This prevents violations of encapsulation. If this weren't the case, we might see something like this:
this would return another pointer to the same object contained inside of
aPdu. addOne() would modify the value inside of aPdu, a violation of
encapsulation. So instead we implement getLength() like this:
myLength = aPdu.getLength();
myLength.addOne();
this creates an identical copy of length and returns that. The calling
method can modify that to its heart's content without affecting aPdu.
For the same reason, the clone() method should make copies of all the
instance variables. Otherwise, the "new" object will have pointers
shared with the old object. The clone() operation also helps prevent
big dependency meshes for GC.
public UnsignedByte getLength()
{ return (UnsignedByte)length.clone();
}
Serialization should make a call to the superclass before it does its own instance variables.
Note that we do not include the padding variables in the instance variables of objects. Padding is used only in the serialized, external representation of a PDU. The user would have no reason to do anything with padding in an object. Since it is only an artifact of serialization, references to padding are limited to the serialization/deserialization methods.
Also, the length, a field in the header portion, is calculated on the fly rather than saved and set. The header has a fixed length, 18 bytes. To find the size of a PDU, implement the length() method in all the subclasses. This should make a call to super, do any local calculations required, then return the correct number. Your code might look like :
public int length() {
return super.length() + 110 + aVector.size * anElement.length();
}
This adds up the length of the superclass, such as the PDU header, the basic length of the subclass (such as the entity state PDU), and adds the length of any variable number of parameters attached to the PDU.
PduElement
,
SerializationInterface
Field Summary | |
static boolean |
DEBUG
Debugging flag; when set true, turns on verbose diagnostic, statements that prints in the java console. |
protected UnsignedByte |
exerciseID
Exercise Identifier Field. |
protected UnsignedByte |
pduType
Type of PDU (entity state, message, etc). |
protected UnsignedByte |
protocolFamily
Indicates the family of protocols which the PDU belongs to. |
protected UnsignedByte |
protocolVersion
Current version of the protocol being used. |
static int |
sizeOf
Constant value--size of a PDU header when written out; sizeOf = 12 bytes . |
protected long |
timeReceived
timeReceived is the time since 1970. |
protected UnsignedInt |
timestamp
Time Stamp, either relative or UTC This field shall specify the time which the data in the PDU is valid. |
Constructor Summary | |
ProtocolDataUnit()
Constructor for a default protocol data unit. |
Method Summary | |
static ProtocolDataUnit |
byteArrayToPdu(byte[] pByteArray)
Returns a PDU completely read in from the byte array in parameter. |
java.lang.Object |
clone()
Makes deep copies of all the instance variables, so we don't have two objects pointing to the same data. |
static ProtocolDataUnit |
datagramToPdu(java.net.DatagramPacket pDatagramPacket)
Legacy compatability method |
protected static void |
debug(java.lang.String pDiagnostic)
Debugging output. |
void |
deSerialize(java.io.DataInputStream inputStream)
Deserialize our data from the input stream. |
UnsignedByte |
getExerciseID()
|
static java.lang.StringBuffer |
getPaddingOfLength(int pIndent)
In a number of places we need to generate strings of a specific length of spaces. |
UnsignedByte |
getPduType()
|
UnsignedByte |
getProtocolFamily()
|
UnsignedByte |
getProtocolVersion()
|
boolean |
getRtpHeaderEnabled()
|
long |
getTimeReceived()
get the time the PDU was received. |
UnsignedInt |
getTimestamp()
|
double |
getVRMLTimestamp()
Returns the timestamp, INTERPRETED IN THE VRML DEFINITION OF TIME. |
int |
length()
Returns the length of the PDU header. |
void |
makeTimestampCurrent()
This methods sets the Timestamp to the current Time. |
abstract java.lang.String |
pduName()
Returns the name of the PDU--eg, Entity State, Fire, Comment, etc-- as a String. |
void |
printValues(int indentLevel,
java.io.PrintStream printStream)
Prints the generated serialized object for debugging. |
void |
serialize(java.io.DataOutputStream outputStream)
Serialize our data out to the stream. |
void |
setExerciseID(int pExerciseID)
|
void |
setExerciseID(UnsignedByte pExerciseID)
|
void |
setPduType(short pPduType)
|
void |
setPduType(UnsignedByte pPduType)
|
void |
setProtocolFamily(int pProtocolFamily)
|
void |
setProtocolFamily(UnsignedByte pProtocolFamily)
|
void |
setProtocolVersion(int pProtocolVersion)
|
void |
setProtocolVersion(UnsignedByte pProtocolVersion)
|
void |
setRtpHeaderEnabled(boolean pRtpHeaderEnabled)
|
static void |
setSimulationStartTime(long pStartTime)
Accessor method that sets the simulation start time. |
void |
setTimeReceived(long pTimeReceived)
Set the time the PDU was received |
void |
setTimestamp(long pTimestamp)
|
void |
setTimestamp(UnsignedInt pTimestamp)
|
void |
stampTimeReceived()
Stamp the current time into the timeReceived field |
java.lang.String |
toString()
|
protected static void |
trace(java.lang.String pDiagnostic)
Guaranteed debugging output. |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final boolean DEBUG
protected UnsignedByte protocolVersion
protocolVersion=5
No default value is set.
protected UnsignedByte exerciseID
protected UnsignedByte pduType
PduTypeField
protected UnsignedByte protocolFamily
protected UnsignedInt timestamp
protected long timeReceived
public static final int sizeOf
sizeOf = 12 bytes
.
Constructor Detail |
public ProtocolDataUnit()
Method Detail |
public boolean getRtpHeaderEnabled()
public void setRtpHeaderEnabled(boolean pRtpHeaderEnabled)
protected static void debug(java.lang.String pDiagnostic)
protected static void trace(java.lang.String pDiagnostic)
public java.lang.Object clone()
PduElement
clone
in class PduElement
Object
public static java.lang.StringBuffer getPaddingOfLength(int pIndent)
public void printValues(int indentLevel, java.io.PrintStream printStream)
PduElement
printValues
in class PduElement
indentLevel
- number of spaces to indent for visibilitypublic void makeTimestampCurrent()
public double getVRMLTimestamp()
public static void setSimulationStartTime(long pStartTime)
public abstract java.lang.String pduName()
String
public java.lang.String toString()
public UnsignedByte getProtocolVersion()
public void setProtocolVersion(UnsignedByte pProtocolVersion)
public void setProtocolVersion(int pProtocolVersion)
public UnsignedByte getExerciseID()
public void setExerciseID(UnsignedByte pExerciseID)
public void setExerciseID(int pExerciseID)
public UnsignedByte getPduType()
for legal values
public void setPduType(UnsignedByte pPduType)
for legal values
public void setPduType(short pPduType)
for legal values
public UnsignedByte getProtocolFamily()
public void setProtocolFamily(UnsignedByte pProtocolFamily)
public void setProtocolFamily(int pProtocolFamily)
public UnsignedInt getTimestamp()
public void setTimestamp(UnsignedInt pTimestamp)
public void setTimestamp(long pTimestamp)
public int length()
length
in class PduElement
public void serialize(java.io.DataOutputStream outputStream)
PduElement
super.Serialize()
to make sure
the superclasse's data is serialized out. The order
in which instance variables are serialized is significant. They must
be serialized in the same order they appear in the DIS
spec.
Prints out some information during execution if debugging flag is set.
serialize
in interface SerializationInterface
serialize
in class PduElement
outputStream
- the stream to which this object is serializedpublic void deSerialize(java.io.DataInputStream inputStream)
PduElement
super.deSerialize
to make sure
the superclass's data are properly affected. The order
in which instance variables are serialized is significant. They must
be deSerialized in the same order as they have been serialized as specified by the DIS spec.
deSerialize
in interface SerializationInterface
deSerialize
in class PduElement
inputStream
- the stream from which this object is initializedpublic static ProtocolDataUnit datagramToPdu(java.net.DatagramPacket pDatagramPacket)
public static ProtocolDataUnit byteArrayToPdu(byte[] pByteArray)
ProtocolDataUnit
|
^
EntityStatePDU
The deserialize message goes first to the EntityStatePdu. The ESPDU
immediately calls the deserialize method in its superclass, ProtocolDataUnit.
PDU deserializes the first few instance variables, then returns. The ESPDU
then deserializes its ivars.
Prints out some information during exectuion if debugging flag is set.
pByteArray
- The data packet of bytes
java.lang.RuntimeException
- if an IO error occurs.public long getTimeReceived()
public void setTimeReceived(long pTimeReceived)
public void stampTimeReceived()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |