diff --git a/src/edu/nps/moves/dis7/examples/PduReaderPlayer.java b/src/edu/nps/moves/dis7/examples/PduReaderPlayer.java index db25342793f56fc99f29b3580279ade0035ac5de..c8d5e734a3cc9fdfd0ebc5cb39a655aeacff7622 100644 --- a/src/edu/nps/moves/dis7/examples/PduReaderPlayer.java +++ b/src/edu/nps/moves/dis7/examples/PduReaderPlayer.java @@ -52,8 +52,8 @@ public class PduReaderPlayer System.out.println("Beginning pdu playback from directory " + outDir); try { - PduPlayer player = new PduPlayer(mcast, port, new File(outDir).toPath()); - player.startResume(); + PduPlayer pduPlayer = new PduPlayer(mcast, port, new File(outDir).toPath()); + pduPlayer.startResume(); mystate state = mystate.RUNNING; Scanner scan = new Scanner(System.in); @@ -61,15 +61,15 @@ public class PduReaderPlayer System.out.println("Type p/enter to pause, r/enter to resume, q/enter to quit"); String line = scan.nextLine(); if (line.equalsIgnoreCase("p") && state == mystate.RUNNING) { - player.stopPause(); + pduPlayer.stopPause(); state = mystate.PAUSED; } else if (line.equalsIgnoreCase("r") && state == mystate.PAUSED) { - player.startResume(); + pduPlayer.startResume(); state = mystate.RUNNING; } else if (line.equalsIgnoreCase("q")) { - player.end(); + pduPlayer.end(); break; } } diff --git a/src/edu/nps/moves/dis7/utilities/stream/PduPlayer.java b/src/edu/nps/moves/dis7/utilities/stream/PduPlayer.java index d8c7e401efb4e08ad1536c0628023199c159c299..d29d8a7ebf4fd87ea8f54e6cc6a3399a0c38e2ad 100644 --- a/src/edu/nps/moves/dis7/utilities/stream/PduPlayer.java +++ b/src/edu/nps/moves/dis7/utilities/stream/PduPlayer.java @@ -16,6 +16,9 @@ import java.util.Arrays; import java.util.Base64; import static edu.nps.moves.dis7.utilities.stream.PduRecorder.*; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; +import java.util.regex.Pattern; public class PduPlayer { @@ -43,7 +46,7 @@ public class PduPlayer private Integer scenarioPduCount = null; private boolean showPduCountsOneTime = false; private int pduCount = 0; - private DatagramSocket dsock; + private DatagramSocket datagramSocket; private BufferedReader brdr; private Long startNanoTime = null; private boolean paused = false; @@ -77,8 +80,8 @@ public class PduPlayer //Arrays.sort(fs, Comparator.comparing(File::getName)); if(netSend) - dsock = new DatagramSocket(); - Base64.Decoder decdr = Base64.getDecoder(); + datagramSocket = new DatagramSocket(); + Base64.Decoder base64Decoder = Base64.getDecoder(); for (File f : fs) { System.out.println("Replaying " + f.getAbsolutePath()); @@ -99,7 +102,19 @@ public class PduPlayer } else { - String[] sa = line.split(","); + //---Code Tobi for Plain Text--- + if (line.contains("#")) line = line.substring(0,line.indexOf("#")-1); //Delete appended Comments + //String[] sa = line.split(","); + //Pattern splitting needed for playback of unencoded streams + String REGEX = "\\],\\["; + Pattern pattern = Pattern.compile(REGEX); + String[] sa; + sa = pattern.split(line); + //Add the "]" to the end of sa[0]. It was taken off by the split + sa[0] = sa[0].concat("]"); + //Add the "]" to the end of sa[0]. It was taken off by the split + sa[1] = "[".concat(sa[1]); + //------------------------------ if (sa.length != 2) { System.err.println("Error: parsing error. Line follows."); System.err.println(line); @@ -109,7 +124,41 @@ public class PduPlayer if (startNanoTime == null) startNanoTime = System.nanoTime(); - byte[] pduTimeBytes = decdr.decode(sa[0]); + //byte[] pduTimeBytes = decdr.decode(sa[0]); + + //---Code Tobi for Plain Text--- + byte[] pduTimeBytes = null; + String[] splitString; + + //Split first String into multiple Strings cotaining integers + REGEX = ","; + pattern = Pattern.compile(REGEX); + + sa[0] = sa[0].substring(1, sa[0].length() - 1); + + splitString = pattern.split(sa[0]); + + //Define an array to store the in values from the string and initalize it to a value drifferent from NULL + int[] arr = new int[splitString.length]; + + String tempString = ""; + + //Test + for (int x = 0; x < splitString.length; x++) { + + tempString = splitString[x].trim(); + + int tempInt = Integer.parseInt(tempString); + arr[x] = tempInt; + } + // Credit: https://stackoverflow.com/questions/1086054/how-to-convert-int-to-byte + ByteBuffer byteBuffer1 = ByteBuffer.allocate(arr.length * 4); + IntBuffer intBuffer = byteBuffer1.asIntBuffer(); + intBuffer.put(arr); + + pduTimeBytes = byteBuffer1.array(); + + //------------------------------ long pduTimeInterval = Longs.fromByteArray(pduTimeBytes); // This is a relative number in nanoseconds of the time of packet reception minus first packet reception for scenario. @@ -122,21 +171,72 @@ public class PduPlayer sleep(sleepTime / 1000000L, (int) (sleepTime % 1000000L)); } - byte[] buffer = decdr.decode(sa[1]); + //byte[] buffer = decdr.decode(sa[1]); + //---Code Tobi for Plain Text--- + //Handle the second String + // Split second String into multiple Strings cotaining integers + REGEX = ","; + pattern = Pattern.compile(REGEX); + + sa[1] = sa[1].substring(1, sa[1].length() - 1); + + splitString = pattern.split(sa[1]); + + //Define an array to store the in values from the string and initalize it to a value drifferent from NULL + arr = new int[splitString.length]; + + //trim spaces, if any + tempString = ""; + + //Test + for (int x = 0; x < splitString.length; x++) { + + tempString = splitString[x].trim(); + + int tempInt = Integer.parseInt(tempString); + arr[x] = tempInt; + + //System.out.println(tempInt); + } + + // Credit: https://stackoverflow.com/questions/1086054/how-to-convert-int-to-byte + ByteBuffer byteBuffer2 = ByteBuffer.allocate(arr.length * 4); + intBuffer = byteBuffer2.asIntBuffer(); + intBuffer.put(arr); + + byte[] buffer = byteBuffer2.array(); + + //When the byteBuffer stores the arry of Integers into the byte array it store a 7 as 0 0 0 7. + //Therefore a shortBuffer is created where only every forth value is stored. + //it must be done with modulo instead of testing for "0" because a "0" could be there as value and not as padding + byte[] bufferShort = new byte[byteBuffer2.array().length / 4]; + + int bufferShortCounter = 0; + + for (int i = 1; i < byteBuffer2.array().length; i++) { + + if (((i + 1) % 4) == 0) { + + bufferShort[bufferShortCounter] = buffer[i]; + bufferShortCounter++; + } + } + //------------------------------ if(netSend) { - DatagramPacket packet = new DatagramPacket(buffer, buffer.length, addr, port); - dsock.send(packet); + DatagramPacket datagramPacket = new DatagramPacket(bufferShort, bufferShort.length, addr, port); + datagramSocket.send(datagramPacket); + System.out.println("Sent PDU "); } if(rawListener != null) { rawListener.receiveBytes(buffer); } pduCount++; if (scenarioPduCount != null) - scenarioPduCount++; + scenarioPduCount++; if (showPduCountsOneTime || pduCount % 5 == 0) - showCounts(); + showCounts(); } line = brdr.readLine(); @@ -183,9 +283,9 @@ public class PduPlayer private void closer() { try { - if (dsock != null) { - dsock.close(); - dsock = null; + if (datagramSocket != null) { + datagramSocket.close(); + datagramSocket = null; } if (brdr != null) { brdr.close();