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

numerous improvements working with DisThreadedNetworkInterface; now an...

numerous improvements working with DisThreadedNetworkInterface; now an implementable superclass for easy extensions
parent 66c06ea6
No related branches found
No related tags found
No related merge requests found
...@@ -29,7 +29,7 @@ import org.apache.commons.io.FilenameUtils; ...@@ -29,7 +29,7 @@ import org.apache.commons.io.FilenameUtils;
* *
* @author Mike Bailey, jmbailey@nps.edu * @author Mike Bailey, jmbailey@nps.edu
*/ */
public class PduRecorder implements PduReceiver public class PduRecorder // implements PduReceiver
{ {
/** Default multicast group address <code>239.1.2.3</code> for send and receive connections. /** Default multicast group address <code>239.1.2.3</code> for send and receive connections.
* @see <a href="https://en.wikipedia.org/wiki/Multicast_address">https://en.wikipedia.org/wiki/Multicast_address</a> */ * @see <a href="https://en.wikipedia.org/wiki/Multicast_address">https://en.wikipedia.org/wiki/Multicast_address</a> */
...@@ -272,8 +272,7 @@ public class PduRecorder implements PduReceiver ...@@ -272,8 +272,7 @@ public class PduRecorder implements PduReceiver
if (disThreadedNetworkInterface != null) if (disThreadedNetworkInterface != null)
{ {
disThreadedNetworkInterface.removeRawListener(disRawPduListener); disThreadedNetworkInterface.removeRawListener(disRawPduListener);
disThreadedNetworkInterface.setKillSentinel(); disThreadedNetworkInterface.close();
disThreadedNetworkInterface = null; // ensure reset upon re-start
} }
if (includeHeaders) if (includeHeaders)
{ {
...@@ -299,7 +298,7 @@ public class PduRecorder implements PduReceiver ...@@ -299,7 +298,7 @@ public class PduRecorder implements PduReceiver
* @param newBuffer byte array for receiving data * @param newBuffer byte array for receiving data
* @param newLength length of byte array * @param newLength length of byte array
*/ */
@Override // @Override
public void receivePdu(byte[] newBuffer, int newLength) public void receivePdu(byte[] newBuffer, int newLength)
{ {
if (java.util.Arrays.equals(newBuffer, oldBuffer)) if (java.util.Arrays.equals(newBuffer, oldBuffer))
...@@ -312,8 +311,9 @@ public class PduRecorder implements PduReceiver ...@@ -312,8 +311,9 @@ public class PduRecorder implements PduReceiver
if (startNanoTime == null) if (startNanoTime == null)
startNanoTime = packetReceivedNanoTime; startNanoTime = packetReceivedNanoTime;
// DIS timestamp is 8 bytes in length, converted from Java long time into byte array
byte[] timeByteArray = Longs.toByteArray(packetReceivedNanoTime - startNanoTime); byte[] timeByteArray = Longs.toByteArray(packetReceivedNanoTime - startNanoTime);
//System.out.println("wrote time "+(packetRcvNanoTime - startNanoTime)); //System.out.println("wrote time "+(packetReceivedNanoTime - startNanoTime)); // debug
byte[] byteBufferSized = Arrays.copyOf(newBuffer, newLength); byte[] byteBufferSized = Arrays.copyOf(newBuffer, newLength);
DisPduType pduType; DisPduType pduType;
...@@ -331,20 +331,39 @@ public class PduRecorder implements PduReceiver ...@@ -331,20 +331,39 @@ public class PduRecorder implements PduReceiver
break; break;
case ENCODING_BASE64: case ENCODING_BASE64:
sb.append(base64Encoder.encodeToString(timeByteArray)); byte[] mergedByteArray = Arrays.copyOf(timeByteArray, timeByteArray.length + byteBufferSized.length);
sb.append(','); System.arraycopy(byteBufferSized, 0, mergedByteArray, timeByteArray.length, byteBufferSized.length);
sb.append(base64Encoder.encodeToString(byteBufferSized)); sb.append(base64Encoder.encodeToString(mergedByteArray));
/*
// from Rick
// https://stackoverflow.com/questions/5683486/how-to-combine-two-byte-arrays
// public static byte[] concat(byte[] a, byte[] b) {​​​​​​​
// int lenA = a.length;
// int lenB = b.length;
// byte[] c = Arrays.copyOf(a, lenA + lenB);
// System.arraycopy(b, 0, c, lenA, lenB);
// return c;
// }​​​​​​​
// prior approach did not catenate before converting to base64. instead catenating results (which might not be correct)
// sb.append(base64Encoder.encodeToString(timeByteArray));
// sb.append(base64Encoder.encodeToString(byteBufferSized));
*/
break; break;
case ENCODING_PLAINTEXT: case ENCODING_PLAINTEXT:
// by Tobias Brennenstuhl Spring 2020 // by Tobias Brennenstuhl Spring 2020
sb.append(Arrays.toString(timeByteArray).replace(" ", "")); // Remove square brackets to end up with pure CSV
sb.append(Arrays.toString(timeByteArray).replace(" ", "").replace("[","").replace("]",""));
sb.append(','); sb.append(',');
sb.append(Arrays.toString(byteBufferSized).replace(" ", "")); sb.append(Arrays.toString(byteBufferSized).replace(" ", "").replace("[","").replace("]",""));
sb.append(" # "); sb.append(" # ");
pduType = DisPduType.getEnumForValue(Byte.toUnsignedInt(byteBufferSized[2])); // 3rd byte pduType = DisPduType.getEnumForValue(Byte.toUnsignedInt(byteBufferSized[2])); // 3rd byte
sb.append(pduType); sb.append(pduType);
break; break;
// TODO test reader still works without extra [square brackets]
// TODO add option for TSV vice CSV
default: default:
if (ENCODING_OPTIONS_LIST.contains(encodingPduLog)) if (ENCODING_OPTIONS_LIST.contains(encodingPduLog))
...@@ -368,8 +387,10 @@ public class PduRecorder implements PduReceiver ...@@ -368,8 +387,10 @@ public class PduRecorder implements PduReceiver
((PrintWriter)logFileWriter).println(); ((PrintWriter)logFileWriter).println();
} }
} }
catch (IOException ex) { catch (IOException ex)
throw new RuntimeException("Fatal exception writing DIS log file in PduRecorder thread, encodingPduLog=" + encodingPduLog + ": " + ex); {
ex.printStackTrace();
throw new RuntimeException("Fatal exception writing DIS log file in PduRecorder thread, encodingPduLog=" + encodingPduLog + ": " + ex);
} }
if (false) // debug if (false) // debug
System.out.println("PduRecorder: pduCount="+ pduCount); System.out.println("PduRecorder: pduCount="+ pduCount);
...@@ -387,7 +408,7 @@ public class PduRecorder implements PduReceiver ...@@ -387,7 +408,7 @@ public class PduRecorder implements PduReceiver
} }
/** /**
* Deprecated, use getDisThreadedNetworkInterface() instead * Deprecated due to class rename, use getDisThreadedNetworkInterface() instead
* @see getDisThreadedNetworkInterface() * @see getDisThreadedNetworkInterface()
* @return an instance of this DisThreadedNetworkInterface * @return an instance of this DisThreadedNetworkInterface
*/ */
...@@ -551,7 +572,7 @@ public class PduRecorder implements PduReceiver ...@@ -551,7 +572,7 @@ public class PduRecorder implements PduReceiver
try { try {
Pdu nextPdu = factory.createPdu(allPDUTypesArray[i]); Pdu nextPdu = factory.createPdu(allPDUTypesArray[i]);
disNetworkInterface.send(nextPdu); disNetworkInterface.send(nextPdu);
Thread.sleep (50L); // let send/receive threads and streams catch up Thread.sleep (100L); // let send/receive threads and streams catch up
} }
catch (InterruptedException ex) { catch (InterruptedException ex) {
System.err.println("Exception sending Pdu " + pduTypeValue + ": " + ex.getLocalizedMessage()); System.err.println("Exception sending Pdu " + pduTypeValue + ": " + ex.getLocalizedMessage());
......
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