Skip to content
Snippets Groups Projects
Commit 8f93b538 authored by Brutzman, Don's avatar Brutzman, Don
Browse files

improved marshalling byte checks, espdu.copy() still failing

parent be2322ac
No related branches found
No related tags found
No related merge requests found
......@@ -48,7 +48,6 @@ import java.nio.ByteBuffer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
......@@ -134,6 +133,20 @@ public class PduTrack
}
return latestLocation;
}
/**
* Get individual Pdu from pduList, index must not exceed existing pduList size
* @param index for pdu of interest
* @return pdu of interest
*/
public Pdu getPdu(int index) throws IndexOutOfBoundsException
{
if ((index >= pduList.size()) || (index < 0))
{
System.out.println (TRACE_PREFIX + "getPdu(" + index + ") out of bounds, pduList.size()=" + pduList.size());
// then throw exception
}
return pduList.get(index);
}
/**
* @return the pduList
*/
......@@ -168,8 +181,9 @@ public class PduTrack
{
if (newPdu.getPduType() == DisPduType.ENTITY_STATE)
{
EntityStatePdu deepCopyEspdu = new EntityStatePdu();
deepCopyEspdu.setTimestamp (((EntityStatePdu)newPdu).getTimestamp());
// EntityStatePdu deepCopyEspdu = new EntityStatePdu();
EntityStatePdu deepCopyEspdu = pduFactory.makeEntityStatePdu();
deepCopyEspdu.setTimestamp (((EntityStatePdu)newPdu).getTimestamp());
deepCopyEspdu.setMarking (((EntityStatePdu)newPdu).getMarking());
deepCopyEspdu.setEntityID (((EntityStatePdu)newPdu).getEntityID());
deepCopyEspdu.setForceId (((EntityStatePdu)newPdu).getForceId());
......@@ -719,6 +733,57 @@ public class PduTrack
}
return this;
}
/** whether or not to insert commas between hex values
* */
private boolean insertCommas = true;
/**
* @return whether or not to insert commas between hex values
*/
public boolean hasInsertCommas() {
return insertCommas;
}
/**
* @param insertCommas the insertCommas value to set
*/
public void setInsertCommas(boolean insertCommas) {
this.insertCommas = insertCommas;
}
/**
* Convert byte array to hex string
* @param bytes input data
* @param insertCommas whether to insert commas between hex values
* @return hex string
*/
public String bytesToHex(byte[] bytes, boolean insertCommas)
{
this.setInsertCommas(insertCommas);
return bytesToHex(bytes);
}
/**
* Convert byte array to hex string
* @param bytes input data
* @see <a href="https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java">https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java</a>
* @return hex string
*/
public static String bytesToHex(byte[] bytes)
{
boolean insertCommas = true;
final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[bytes.length * 2];
StringBuilder sb = new StringBuilder();
for (int j = 0; j < bytes.length; j++) {
int v = bytes[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
// if (!(hexChars[j * 2] == '0')) // omit leading zero
sb.append(hexChars[j * 2]);
sb.append(hexChars[j * 2 + 1]);
if (insertCommas && (j < bytes.length - 1))
sb.append(", ");
}
return sb.toString();
}
/**
* Report current PDU information to console
* @param anEspdu EntityStatePdu of interest
......@@ -726,7 +791,13 @@ public class PduTrack
*/
public PduTrack reportPdu(EntityStatePdu anEspdu)
{
System.out.println (String.format("%s", anEspdu.getMarkingString().trim()) + ", " +
System.out.println (
String.format("%08d", anEspdu.getTimestamp()) + ", " +
String.format("%s", anEspdu.getMarkingString().trim()) + ", " +
"EntityID=(" +
anEspdu.getEntityID().getSiteID() + ", " +
anEspdu.getEntityID().getApplicationID() + ", " +
anEspdu.getEntityID().getEntityID() + ") " +
"location=(" +
String.format("%4.1f", anEspdu.getEntityLocation().getX()) + ", " +
String.format("%4.1f", anEspdu.getEntityLocation().getY()) + ", " +
......@@ -749,27 +820,27 @@ public class PduTrack
pduTrack.setAuthor("Don Brutzman");
pduTrack.setX3dModelIdentifier("https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/-/blob/master/examples/src/OpenDis7Examples/PduTrackInterpolation.x3d");
EntityID entityID_1 = new EntityID();
entityID_1.setSiteID(1).setApplicationID(2).setEntityID(3); // made-up example ID;
EntityID entityID_123 = new EntityID();
entityID_123.setSiteID(1).setApplicationID(2).setEntityID(3); // made-up example ID;
// TODO someday, use enumerations; is there a unique site triplet for MOVES Institute?
for (int i = 0; i < TOTAL_PDUS; i++)
for (int i = 0; i < TOTAL_PDUS; i++) // create espdus and add each to track pduList
{
// EntityStatePdu espdu = new EntityStatePdu();
EntityStatePdu espdu = pduFactory.makeEntityStatePdu();
EntityStatePdu espdu = pduFactory.makeEntityStatePdu(); // TODO check Pdu.Type
espdu.setTimestamp(i);
espdu.setMarking("ESPDU " + i);
espdu.setEntityLocation(i, i, i);
espdu.setEntityOrientation(0, (float)(45.0 * Math.PI / 180.0), 0);
espdu.setEntityID(entityID_1);
espdu.setEntityID(entityID_123);
espdu.setForceId(ForceID.FRIENDLY);
espdu.setEntityType(new _001Poseidon()); // note import statement above
pduTrack.addPdu(espdu);
reportPdu(espdu);
}
// test track operations
pduTrack.reversePdus(); // test
pduTrack.sortPdus(); // restore
// System.out.println(TRACE_PREFIX + "reversePdus() then sortPdus() to test track operations");
// pduTrack.reversePdus(); // test
// pduTrack.sortPdus(); // restore
pduTrack.createRawWaypoints(); // copies all ESPDU points to waypoints
System.out.println(TRACE_PREFIX + "getEspduCount()=" + pduTrack.getEspduCount());
......@@ -777,39 +848,36 @@ public class PduTrack
System.out.println(TRACE_PREFIX + "getTotalDurationSeconds()=" + pduTrack.getTotalDurationSeconds());
System.out.println("=================================");
System.out.println("Marshalling checks");
System.out.println("PduTrack pduList marshalling checks");
System.out.println();
try
{
int BYTE_BUFFER_SIZE = 400; // TODO what is expected max buffer size?
// int BYTE_BUFFER_SIZE = 400; // TODO what is expected max buffer size?
for (int i = 0; i < TOTAL_PDUS; i++)
{
EntityStatePdu espdu = new EntityStatePdu();
byte[] byteArray = new byte[BYTE_BUFFER_SIZE];
byteArray = espdu.marshal();
// https://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a-hex-string-in-java
final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
char[] hexChars = new char[byteArray.length * 2];
StringBuilder sb = new StringBuilder();
for (int j = 0; j < byteArray.length; j++) {
int v = byteArray[j] & 0xFF;
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
if (!(hexChars[j * 2] == '0'))
sb.append(hexChars[j * 2]);
sb.append(hexChars[j * 2 + 1]);
if (j < byteArray.length - 1)
sb.append(", ");
}
System.out.println("espdu.marshal(): [" + sb.toString() + "]");
// EntityStatePdu espdu = new EntityStatePdu();
// EntityStatePdu espdu = pduFactory.makeEntityStatePdu(); // TODO check Pdu.Type
Pdu pdu = pduTrack.getPduList().get(i);
if (!(pdu instanceof EntityStatePdu))
continue;
EntityStatePdu espdu = (EntityStatePdu) pdu;
byte[] byteArray = espdu.marshal();
reportPdu(espdu);
System.out.println("espdu.marshal(): " + bytesToHex(byteArray));
System.err.flush(); System.out.flush();
ByteBuffer byteBuffer = ByteBuffer.allocate(300);
ByteBuffer byteBuffer = ByteBuffer.allocate(byteArray.length);
espdu.marshal(byteBuffer);
System.out.println("espdu.marshal(byteBuffer): " + Arrays.toString(byteBuffer.array()));
reportPdu(espdu);
System.out.println("espdu.marshal(byteBuffer): " + bytesToHex(byteBuffer.array()));
System.err.flush(); System.out.flush();
byteBuffer = ByteBuffer.allocate(byteArray.length); // TODO is there a better way to reset?
espdu.copy().marshal(byteBuffer);
System.out.println("espdu.copy().marshal(byteBuffer):" + Arrays.toString(byteBuffer.array()));
reportPdu(espdu.copy());
System.out.println("espdu.copy().marshal(byteBuffer):" + bytesToHex(byteBuffer.array()));
System.err.flush(); System.out.flush();
System.out.println("= = = = = = = = = = = = = = = = =");
}
}
catch(Exception e)
......@@ -841,4 +909,5 @@ public class PduTrack
System.out.println("*** PduTrack main() self test complete.");
}
}
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment