From 02e523c1547b8b5e0fd0762fd2d4ef5469bf01b7 Mon Sep 17 00:00:00 2001
From: brutzman <brutzman@DESKTOP-2S09UKA>
Date: Fri, 6 Sep 2019 04:39:28 -0700
Subject: [PATCH] sample code for COMMENTS PDU, some library debugging needed

---
 .../src/OpenDis7Examples/AllPduReceiver.java  | 50 ++++++++++++-------
 .../src/OpenDis7Examples/AllPduSender.java    | 30 +++++++++--
 2 files changed, 58 insertions(+), 22 deletions(-)

diff --git a/examples/src/OpenDis7Examples/AllPduReceiver.java b/examples/src/OpenDis7Examples/AllPduReceiver.java
index 99596ea5dc..101cde4519 100644
--- a/examples/src/OpenDis7Examples/AllPduReceiver.java
+++ b/examples/src/OpenDis7Examples/AllPduReceiver.java
@@ -6,6 +6,7 @@ import java.io.*;
 import edu.nps.moves.dis7.*;
 import edu.nps.moves.dis7.enumerations.*;
 import edu.nps.moves.dis7.util.*;
+import java.util.ArrayList;
 
 public class AllPduReceiver
 {
@@ -45,25 +46,38 @@ public class AllPduReceiver
         socket.receive(packet);
 
         Pdu pdu = factory.createPdu(packet.getData());
-        if (pdu != null) {
-          DISPDUType currentPduType = pdu.getPduType(); //short  currentPduType = pdu.getPduType();
-          String currentPduTypeName = pdu.getClass().getName();
-          DISProtocolFamily currentProtocolFamilyID = pdu.getProtocolFamily(); //short  currentProtocolFamilyID = pdu.getProtocolFamily();
-          String currentPduFamilyName = pdu.getClass().getSuperclass().getSimpleName();
+        if (pdu != null)
+        {
+            DISPDUType currentPduType = pdu.getPduType(); //short  currentPduType = pdu.getPduType();
+            String currentPduTypeName = pdu.getClass().getName();
+            DISProtocolFamily currentProtocolFamilyID = pdu.getProtocolFamily(); //short  currentProtocolFamilyID = pdu.getProtocolFamily();
+            String currentPduFamilyName = pdu.getClass().getSuperclass().getSimpleName();
 
-          StringBuilder message = new StringBuilder();
-          message.append("received DIS PDU ");
-          if (currentPduType.getValue() < 10)
-            message.append(" ");
-          message.append(currentPduType.getValue());
-          String currentPduTypePadded     = String.format("%-34s", currentPduType); // - indicates right padding of whitespace
-          message.append(" " ).append(currentPduTypePadded);
-          String currentPduTypeNamePadded = String.format("%-49s", currentPduTypeName); // - indicates right padding of whitespace
-          message.append(" of type ").append(currentPduTypeNamePadded); // package.class name
-          message.append(" (protocolFamily ").append(currentProtocolFamilyID);
-//        message.append(" ").append(currentPduFamilyName); // class name is also available
-         message.append(")");
-          System.out.println(message.toString());
+            StringBuilder message = new StringBuilder();
+            message.append("received DIS PDU ");
+            if (currentPduType.getValue() < 10)
+              message.append(" "); // column spacing
+            message.append(currentPduType.getValue());
+            String currentPduTypePadded     = String.format("%-34s", currentPduType); // - indicates right padding of whitespace
+            message.append(" " ).append(currentPduTypePadded);
+            String currentPduTypeNamePadded = String.format("%-49s", currentPduTypeName); // - indicates right padding of whitespace
+            message.append(" of type ").append(currentPduTypeNamePadded); // package.class name
+            message.append(" (protocolFamily ").append(currentProtocolFamilyID);
+  //        message.append(" ").append(currentPduFamilyName); // class name is also available
+            message.append(")");
+            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
           System.out.println("received packet but pdu is null, packet.getData().length=" + packet.getData().length + ", error...");
diff --git a/examples/src/OpenDis7Examples/AllPduSender.java b/examples/src/OpenDis7Examples/AllPduSender.java
index 0fb8cd5899..ff6ed2397e 100755
--- a/examples/src/OpenDis7Examples/AllPduSender.java
+++ b/examples/src/OpenDis7Examples/AllPduSender.java
@@ -363,10 +363,32 @@ public class AllPduSender
                             break;
 
                         case COMMENT:
-                            aPdu = new CommentPdu();
-                            CommentPdu    newCommentPdu = (CommentPdu)aPdu;
-                            VariableDatum newVariableDatum = new VariableDatum();
-                            // etc. see Garrett and Pete's code
+                            // aPdu = new CommentPdu(); // default for this switch logic
+                            
+                            // see Garrett Loffelman and Pete Severson's code for OpenDis version 4 example
+                            // 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;
 
                         default:
-- 
GitLab