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

Redo threading, add stop, pause methods

parent b5184987
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,11 @@ import java.util.Base64; ...@@ -18,6 +18,11 @@ import java.util.Base64;
public class Recorder implements PduReceiver public class Recorder implements PduReceiver
{ {
static String DEFAULT_OUTDIR = "./pdulog";
static String DEFAULT_FILEPREFIX = "Pdusave";
static String DEFAULT_MCAST = "230.0.0.0";
static int DEFAULT_PORT = 3000;
static String DISLOG_FILE_TAIL = ".dislog"; static String DISLOG_FILE_TAIL = ".dislog";
public static String COMMENT_MARKER = "!"; public static String COMMENT_MARKER = "!";
...@@ -25,24 +30,88 @@ public class Recorder implements PduReceiver ...@@ -25,24 +30,88 @@ public class Recorder implements PduReceiver
static String STOP_COMMENT_MARKER = COMMENT_MARKER + "End" + COMMENT_MARKER; static String STOP_COMMENT_MARKER = COMMENT_MARKER + "End" + COMMENT_MARKER;
private BufferedWriter bwr; private BufferedWriter bwr;
private File logFile; private File logFile;
private DisNetworking disnet;
public Recorder(Path outputDir, String filename) throws IOException private Thread receiverThrd;
public Recorder() throws IOException
{ {
logFile = makeFile(outputDir, filename + DISLOG_FILE_TAIL); this(DEFAULT_OUTDIR,DEFAULT_MCAST,DEFAULT_PORT);
bwr = new BufferedWriter(new FileWriter(logFile));
} }
public Recorder(String outputDir, String mcastaddr, int port) throws IOException
{
logFile = makeFile(new File(outputDir).toPath(), DEFAULT_FILEPREFIX+DISLOG_FILE_TAIL );
bwr = new BufferedWriter(new FileWriter(logFile));
disnet = new DisNetworking(port, mcastaddr);
// Start a thread to receive and record pdus
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()
{
dosave = true;
}
public void stopPause()
{
dosave = false;
}
public String getOutputFile()
{
if(logFile != null)
return logFile.getAbsolutePath();
return null;
}
public void end()
{
disnet.stop();
receiverThrd.interrupt();
try {
writeFooter();
bwr.flush();
bwr.close();
System.out.println();
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();
int pduCount = 0; int pduCount = 0;
boolean headerWritten = false; boolean headerWritten = false;
boolean dosave = true;
@Override @Override
public void receivePdu(byte[] buff, int len) public void receivePdu(byte[] buff, int len)
{ {
if(!dosave)
return;
long packetRcvNanoTime = System.nanoTime(); long packetRcvNanoTime = System.nanoTime();
if (startNanoTime == null) if (startNanoTime == null)
startNanoTime = packetRcvNanoTime; startNanoTime = packetRcvNanoTime;
...@@ -72,19 +141,6 @@ public class Recorder implements PduReceiver ...@@ -72,19 +141,6 @@ public class Recorder implements PduReceiver
//bwr.flush(); //bwr.flush();
sb.setLength(0); sb.setLength(0);
} }
public void stopAndClose()
{
try {
writeFooter();
bwr.flush();
bwr.close();
System.out.println("Recorder log file closed");
}
catch (IOException ex) {
System.out.println("IOException closing file: " + ex.getMessage());
}
}
public String getLogFile() public String getLogFile()
{ {
...@@ -132,7 +188,7 @@ public class Recorder implements PduReceiver ...@@ -132,7 +188,7 @@ public class Recorder implements PduReceiver
return f; return f;
} }
/* Example usage */ /* Example test usage */
public static void main(String[] args) public static void main(String[] args)
{ {
PduFactory factory = new PduFactory(); //default appid, country, etc. PduFactory factory = new PduFactory(); //default appid, country, etc.
...@@ -142,31 +198,11 @@ public class Recorder implements PduReceiver ...@@ -142,31 +198,11 @@ public class Recorder implements PduReceiver
String filename = "Pdusave"; String filename = "Pdusave";
Recorder recorder; Recorder recorder;
try{recorder = new Recorder(path, filename);} catch(IOException ex) { try{recorder = new Recorder();} catch(IOException ex) {
System.err.println("Exception creating recorder: "+ex.getLocalizedMessage()); System.err.println("Exception creating recorder: "+ex.getLocalizedMessage());
return; return;
} }
// Start a thread to receive and record pdus
Thread receiverThrd = new Thread(()->{
int count = 1;
while(true){
try {
BuffAndLength blen = disnet.receiveRawPdu();
System.out.println(""+ count++ +" Got pdu from DisNetworking");
recorder.receivePdu(blen.buff,blen.length);
}
catch(IOException ex) {
System.err.println("Exception receiving Pdu: "+ex.getLocalizedMessage());
}
}
});
receiverThrd.setPriority(Thread.NORM_PRIORITY);
receiverThrd.setDaemon(true);
receiverThrd.start();
DISPDUType all[] = DISPDUType.values(); DISPDUType all[] = DISPDUType.values();
Arrays.stream(all).forEach(typ-> { Arrays.stream(all).forEach(typ-> {
if(typ != DISPDUType.OTHER) { if(typ != DISPDUType.OTHER) {
...@@ -182,8 +218,7 @@ public class Recorder implements PduReceiver ...@@ -182,8 +218,7 @@ public class Recorder implements PduReceiver
}); });
sleep(2000); sleep(2000);
receiverThrd.interrupt(); recorder.end();
recorder.stopAndClose();
} }
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