/* File: CollisionPdu.java CVS Info: $Id: CollisionPdu.java,v 1.2 1998/01/27 18:43:58 mcgredo Exp $ Compiler: jdk 1.3 */ package mil.navy.nps.dis; // package to which we belong import mil.navy.nps.util.*; // General-purpose utilities import mil.navy.nps.disEnumerations.*; // Enumerations for DIS import java.lang.*; import java.util.*; // utility stuff we need import java.io.*; // input/output for serialization /** * Report collision between entities. * *@version 1.0 *@author Don McGregor (http://www.npsnet.org/~mcgredo) * *
* As with other PDUs, it knows how to serialize and deserialize itself * from the wire. It also knows how to clone itself, and knows how to * calculate its size when sent to the wire.
* *
sizeOf = 60 bytes
*/
public static final int sizeOf = 60; // size of CollisionPDU (bytes), AS WRITTEN TO WIRE
/**
*Default constructor--fills with zeros for all values.
*/
public CollisionPdu()
{
super.setPduType(PduTypeField.COLLISION);
issuingEntityID = new EntityID();
collidingEntityID = new EntityID();
eventID = new EventID();
collisionType = new UnsignedByte();
velocity = new LinearVelocity();
mass = (float)0.0;
location = new EntityCoordinate();
}
public Object clone()
{
/** make a deep copy, so we don't have two objects sharing pointers to the same data (see details).
* The cloning makes possible to have two PDUs describing the same event but with different values.
* The user of this function is advised, therefore, to take proper care when using the return value
* of this function.
*/
CollisionPdu newCollisionPdu = (CollisionPdu)super.clone();
newCollisionPdu.setIssuingEntityID(this.getIssuingEntityID());
newCollisionPdu.setCollidingEntityID(this.getCollidingEntityID());
newCollisionPdu.setEventID(this.getEventID());
newCollisionPdu.setCollisionType(this.getCollisionType());
newCollisionPdu.setLinearVelocity(this.getLinearVelocity());
newCollisionPdu.setMass(this.getMass());
newCollisionPdu.setLocation(this.getLocation());
return newCollisionPdu;
}
/**
*@exception RuntimeException when IO error occurs.
*/
public void serialize(DataOutputStream outputStream)
{
/** write the data to an output stream in the order specified by the DIS
standard. */
UnsignedByte padding = new UnsignedByte(0);
super.serialize(outputStream); // write out header info
issuingEntityID.serialize(outputStream);
collidingEntityID.serialize(outputStream);
eventID.serialize(outputStream);
collisionType.serialize(outputStream);
padding.serialize(outputStream);
velocity.serialize(outputStream);
try {
outputStream.writeFloat(mass);
}
catch(IOException ioError) {
throw new RuntimeException("Exception in CollisionPdu. Error writing to wire.");
}
location.serialize(outputStream);
return;
}
/**
*@exception RuntimeException when IO error occurs.
*/
public void deSerialize(DataInputStream inputStream)
{
/**
deserialize the data from the input stream. By calling the superclass,
the data is read into the buffer first. Then we deserialize each of our
ivars to the appropriate variable stream in the order specified by the DIS
standard. */
UnsignedByte padding = new UnsignedByte(0);
super.deSerialize(inputStream);
issuingEntityID.deSerialize(inputStream);
collidingEntityID.deSerialize(inputStream);
eventID.deSerialize(inputStream);
collisionType.deSerialize(inputStream);
padding.deSerialize(inputStream);
velocity.deSerialize(inputStream);
try {
mass = inputStream.readFloat();
}
catch (IOException ioError) {
throw new RuntimeException("Exception in CollisionPdu. Error reading from wire.");
}
location.deSerialize(inputStream);
return;
}
/** returns the size of the class (return sizeOf)
*/
public int length()
{ return sizeOf; }
/** returns new String("Collision PDU").
The string is allocated dynamically.
*/
public String pduName()
{ return new String("Collision PDU"); }
/** Prints the valules of the PDU fields. this is important for debugging.
*/
public void printValues(int indentLevel, PrintStream printStream)
{
/** print the values of the object out, with correct level of
indentation on the page. */
StringBuffer indent = ProtocolDataUnit.getPaddingOfLength(indentLevel);
int idx, superclassIndent = indentLevel;
printStream.println();
printStream.println("Collision PDU-");
if(superclassIndent > 0)
superclassIndent -= 1;
super.printValues(superclassIndent, printStream);
issuingEntityID.printValues(indentLevel, printStream);
collidingEntityID.printValues(indentLevel, printStream);
eventID.printValues(indentLevel, printStream);
printStream.println(indent + "Collision type: " + collisionType.intValue());
velocity.printValues(indentLevel, printStream);
printStream.println(indent + "Mass: " + mass);
location.printValues(indentLevel, printStream);
return;
}
//Accessor methods
/** Returns the ID of the issuing entity.
*/
public EntityID getIssuingEntityID()
{ return (EntityID)issuingEntityID.clone(); }
/** Sets the ID of the issuing entity.
*/
public void setIssuingEntityID(EntityID pIssuingEntityID)
{ issuingEntityID = pIssuingEntityID; }
/** Returns the ID of the colliding entity.
*/
public EntityID getCollidingEntityID()
{ return (EntityID)collidingEntityID.clone(); }
/** Sets the ID of the colliding entity.
*/
public void setCollidingEntityID(EntityID pCollidingEntityID)
{ collidingEntityID = pCollidingEntityID; }
/** Returns the ID of the event.
*/
public EventID getEventID()
{ return (EventID)eventID.clone(); }
/** Sets the ID of the event.
*/
public void setEventID(EventID pEventID)
{ eventID = pEventID; }
/** Returns the type of the collision.
*/
public UnsignedByte getCollisionType()
{ return (UnsignedByte)collisionType.clone(); }
/** Sets the type of the collision.
*/
public void setCollisionType(mil.navy.nps.util.UnsignedByte pCollisionType)
{ collisionType = pCollisionType; }
/** Returns the linear velocity (safe deep copy).
*/
public LinearVelocity getLinearVelocity()
{ return (LinearVelocity)velocity.clone(); }
/** Sets the linear velocity given a lienar velocity
*/
public void setLinearVelocity(LinearVelocity pLinearVelocity)
{ velocity = pLinearVelocity; }
/** Sets the linear velocity given each of the xyz velocities
*/
public void setLinearVelocity(float x, float y, float z)
{ velocity = new LinearVelocity(x, y, z); }
/** Returns the mass of the colliding entity
*/
public float getMass()
{ return mass; }
/** Sets the mass of the colliding entity
*/
public void setMass(float pMass)
{ mass = pMass; }
/** Returns location of the colliding entity (safe deep copy).
*/
public EntityCoordinate getLocation()
{ return (EntityCoordinate)location.clone(); }
/** Sets location of the colliding entity.
*/
public void setLocation(EntityCoordinate pLocation)
{ location = pLocation; }
/** Sets location of the colliding entity given (x,y,z).
*/
public void setLocation(float x, float y, float z)
{ location = new EntityCoordinate(x, y, z); }
} // end of class CollisionPdu