Skip to content
Snippets Groups Projects
Commit 8a708230 authored by J. M. Bailey's avatar J. M. Bailey
Browse files

Use threaded net access class, DisThreadedNetIF

parent 1b330074
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ import com.google.common.primitives.Longs; ...@@ -4,7 +4,7 @@ import com.google.common.primitives.Longs;
import edu.nps.moves.dis7.Pdu; import edu.nps.moves.dis7.Pdu;
import edu.nps.moves.dis7.enumerations.DISPDUType; import edu.nps.moves.dis7.enumerations.DISPDUType;
import edu.nps.moves.dis7.util.DisNetworking; import edu.nps.moves.dis7.util.DisNetworking;
import edu.nps.moves.dis7.util.DisNetworking.BuffAndLength; import edu.nps.moves.dis7.util.DisThreadedNetIF;
import edu.nps.moves.dis7.util.PduFactory; import edu.nps.moves.dis7.util.PduFactory;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
...@@ -31,8 +31,8 @@ public class Recorder implements PduReceiver ...@@ -31,8 +31,8 @@ public class Recorder implements PduReceiver
private BufferedWriter bwr; private BufferedWriter bwr;
private File logFile; private File logFile;
private DisNetworking disnet;
private Thread receiverThrd; private DisThreadedNetIF disnetworking;
public Recorder() throws IOException public Recorder() throws IOException
{ {
...@@ -44,25 +44,8 @@ public class Recorder implements PduReceiver ...@@ -44,25 +44,8 @@ public class Recorder implements PduReceiver
logFile = makeFile(new File(outputDir).toPath(), DEFAULT_FILEPREFIX+DISLOG_FILE_TAIL ); logFile = makeFile(new File(outputDir).toPath(), DEFAULT_FILEPREFIX+DISLOG_FILE_TAIL );
bwr = new BufferedWriter(new FileWriter(logFile)); bwr = new BufferedWriter(new FileWriter(logFile));
disnet = new DisNetworking(port, mcastaddr); disnetworking = new DisThreadedNetIF(port, mcastaddr);
// Start a thread to receive and record pdus disnetworking.addRawListener(bAndL->receivePdu(bAndL.buff,bAndL.length));
receiverThrd = new Thread(()->{
int count = 1;
while(!Thread.interrupted()){
try {
BuffAndLength blen = disnet.receiveRawPdu();
//System.out.println(""+ count++ +" Got pdu from DisNetworking");
receivePdu(blen.buff,blen.length);
}
catch(IOException ex) {
// this is the normal exception if you do disnet.stop() System.err.println("Exception receiving Pdu: "+ex.getLocalizedMessage());
}
}
});
receiverThrd.setPriority(Thread.NORM_PRIORITY);
receiverThrd.setDaemon(true);
receiverThrd.start();
} }
public void startResume() public void startResume()
...@@ -82,23 +65,18 @@ public class Recorder implements PduReceiver ...@@ -82,23 +65,18 @@ public class Recorder implements PduReceiver
return null; return null;
} }
public void end() public File end() throws IOException
{ {
disnet.stop(); disnetworking.kill();
receiverThrd.interrupt();
writeFooter();
try { bwr.flush();
writeFooter(); bwr.close();
bwr.flush(); System.out.println();
bwr.close(); System.out.println("Recorder log file closed");
System.out.println(); return logFile;
System.out.println("Recorder log file closed");
}
catch (IOException ex) {
System.out.println("IOException closing file: " + ex.getMessage());
}
} }
Long startNanoTime = null; Long startNanoTime = null;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
Base64.Encoder encdr = Base64.getEncoder(); Base64.Encoder encdr = Base64.getEncoder();
...@@ -136,9 +114,8 @@ public class Recorder implements PduReceiver ...@@ -136,9 +114,8 @@ public class Recorder implements PduReceiver
System.err.println("Fatal exception writing DIS log file in Recorder.start()"); System.err.println("Fatal exception writing DIS log file in Recorder.start()");
throw new RuntimeException(ex); throw new RuntimeException(ex);
} }
System.out.print(++pduCount + "\r"); //System.out.println("Recorder: "+ ++pduCount);
//bwr.flush();
sb.setLength(0); sb.setLength(0);
} }
...@@ -218,7 +195,12 @@ public class Recorder implements PduReceiver ...@@ -218,7 +195,12 @@ public class Recorder implements PduReceiver
}); });
sleep(2000); sleep(2000);
recorder.end(); try {
recorder.end();
}
catch(Exception ex) {
System.err.println("Exception closing recorder: "+ex.getClass().getSimpleName()+": "+ex.getLocalizedMessage());
}
} }
private static void sleep(long ms) private static void sleep(long ms)
......
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