Skip to content
Snippets Groups Projects
Commit 02e523c1 authored by brutzman's avatar brutzman
Browse files

sample code for COMMENTS PDU, some library debugging needed

parent d4e2099f
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ import java.io.*; ...@@ -6,6 +6,7 @@ import java.io.*;
import edu.nps.moves.dis7.*; import edu.nps.moves.dis7.*;
import edu.nps.moves.dis7.enumerations.*; import edu.nps.moves.dis7.enumerations.*;
import edu.nps.moves.dis7.util.*; import edu.nps.moves.dis7.util.*;
import java.util.ArrayList;
public class AllPduReceiver public class AllPduReceiver
{ {
...@@ -45,25 +46,38 @@ public class AllPduReceiver ...@@ -45,25 +46,38 @@ public class AllPduReceiver
socket.receive(packet); socket.receive(packet);
Pdu pdu = factory.createPdu(packet.getData()); Pdu pdu = factory.createPdu(packet.getData());
if (pdu != null) { if (pdu != null)
DISPDUType currentPduType = pdu.getPduType(); //short currentPduType = pdu.getPduType(); {
String currentPduTypeName = pdu.getClass().getName(); DISPDUType currentPduType = pdu.getPduType(); //short currentPduType = pdu.getPduType();
DISProtocolFamily currentProtocolFamilyID = pdu.getProtocolFamily(); //short currentProtocolFamilyID = pdu.getProtocolFamily(); String currentPduTypeName = pdu.getClass().getName();
String currentPduFamilyName = pdu.getClass().getSuperclass().getSimpleName(); DISProtocolFamily currentProtocolFamilyID = pdu.getProtocolFamily(); //short currentProtocolFamilyID = pdu.getProtocolFamily();
String currentPduFamilyName = pdu.getClass().getSuperclass().getSimpleName();
StringBuilder message = new StringBuilder(); StringBuilder message = new StringBuilder();
message.append("received DIS PDU "); message.append("received DIS PDU ");
if (currentPduType.getValue() < 10) if (currentPduType.getValue() < 10)
message.append(" "); message.append(" "); // column spacing
message.append(currentPduType.getValue()); message.append(currentPduType.getValue());
String currentPduTypePadded = String.format("%-34s", currentPduType); // - indicates right padding of whitespace String currentPduTypePadded = String.format("%-34s", currentPduType); // - indicates right padding of whitespace
message.append(" " ).append(currentPduTypePadded); message.append(" " ).append(currentPduTypePadded);
String currentPduTypeNamePadded = String.format("%-49s", currentPduTypeName); // - indicates right padding of whitespace String currentPduTypeNamePadded = String.format("%-49s", currentPduTypeName); // - indicates right padding of whitespace
message.append(" of type ").append(currentPduTypeNamePadded); // package.class name message.append(" of type ").append(currentPduTypeNamePadded); // package.class name
message.append(" (protocolFamily ").append(currentProtocolFamilyID); message.append(" (protocolFamily ").append(currentProtocolFamilyID);
// message.append(" ").append(currentPduFamilyName); // class name is also available // message.append(" ").append(currentPduFamilyName); // class name is also available
message.append(")"); message.append(")");
System.out.println(message.toString()); System.out.println(message.toString());
switch (currentPduType) // using enumeration values from edu.​nps.​moves.​dis7.​enumerations.​DISPDUType
{
case COMMENT:
CommentPdu commentPdu = (CommentPdu)pdu; // cast to precise type
ArrayList<VariableDatum> payloadList = (ArrayList)commentPdu.getVariableDatums();
for (VariableDatum variableDatum : payloadList)
{
String nextComment = new String(variableDatum.getVariableDatumValue()); // convert byte[] to String
System.out.println("\"" + nextComment + "\"");
}
}
} }
else else
System.out.println("received packet but pdu is null, packet.getData().length=" + packet.getData().length + ", error..."); System.out.println("received packet but pdu is null, packet.getData().length=" + packet.getData().length + ", error...");
......
...@@ -363,10 +363,32 @@ public class AllPduSender ...@@ -363,10 +363,32 @@ public class AllPduSender
break; break;
case COMMENT: case COMMENT:
aPdu = new CommentPdu(); // aPdu = new CommentPdu(); // default for this switch logic
CommentPdu newCommentPdu = (CommentPdu)aPdu;
VariableDatum newVariableDatum = new VariableDatum(); // see Garrett Loffelman and Pete Severson's code for OpenDis version 4 example
// etc. see Garrett and Pete's code // https://gitlab.nps.edu/Savage/NetworkedGraphicsMV3500/tree/master/assignments/src/MV3500Cohort2018JulySeptember/projects/LoeffelmanSeverson
CommentPdu newCommentPdu = new CommentPdu();
ArrayList<VariableDatum> payloadList = new ArrayList<VariableDatum>();
ArrayList<String> commentsList = new ArrayList<>();
commentsList.add("Hello CommentPDU");
commentsList.add("Here is a new message");
if (!commentsList.isEmpty())
System.out.println("Preparing CommentPDU:");
for (String comment : commentsList)
{
VariableDatum newVariableDatum = new VariableDatum();
newVariableDatum.setVariableDatumValue (comment.getBytes()); // conversion
newVariableDatum.setVariableDatumLength(comment.getBytes().length); // housekeeping
payloadList.add(newVariableDatum);
System.out.println(" \"" + comment + "\"");
}
newCommentPdu.setVariableDatums(payloadList);
aPdu = newCommentPdu; // hand off for sending
break; break;
default: default:
......
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