Update DetectionResultsAnalyzer to also print results to a file. Convert Layer2Signat...
authorJanus Varmarken <varmarken@gmail.com>
Sat, 19 Jan 2019 09:15:37 +0000 (01:15 -0800)
committerJanus Varmarken <varmarken@gmail.com>
Sat, 19 Jan 2019 09:15:37 +0000 (01:15 -0800)
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/detection/layer2/Layer2SignatureDetector.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/evaluation/DetectionResultsAnalyzer.java

index 3c2e062..cd6f737 100644 (file)
@@ -6,6 +6,7 @@ import edu.uci.iotproject.detection.AbstractClusterMatcher;
 import edu.uci.iotproject.detection.ClusterMatcherObserver;
 import edu.uci.iotproject.detection.SignatureDetectorObserver;
 import edu.uci.iotproject.io.PcapHandleReader;
+import edu.uci.iotproject.io.PrintWriterUtils;
 import edu.uci.iotproject.trafficreassembly.layer2.Layer2FlowReassembler;
 import edu.uci.iotproject.util.PrintUtils;
 import org.jgrapht.GraphPath;
@@ -22,19 +23,18 @@ import java.time.Duration;
 import java.util.*;
 
 /**
- * TODO add class documentation.
+ * Performs layer 2 signature detection.
  *
- * @author Janus Varmarken
+ * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
+ * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
  */
 public class Layer2SignatureDetector implements PacketListener, ClusterMatcherObserver {
 
     /**
      * If set to {@code true}, output written to the results file is also dumped to standard out.
      */
-    private static boolean STD_OUT_OUTPUT = true;
+    private static boolean DUPLICATE_OUTPUT_TO_STD_OUT = true;
 
-
-    // Main method only intended for easier debugging.
     public static void main(String[] args) throws PcapNativeException, NotOpenException, IOException {
         if (args.length < 4) {
             String errMsg = String.format("Usage: %s inputPcapFile onSignatureFile offSignatureFile resultsFile [stdOut]" +
@@ -52,28 +52,18 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb
         final String offSignatureFile = args[2];
         final String resultsFile = args[3];
         if (args.length == 5) {
-            STD_OUT_OUTPUT = Boolean.parseBoolean(args[4]);
+            DUPLICATE_OUTPUT_TO_STD_OUT = Boolean.parseBoolean(args[4]);
         }
 
-//        String pcapFileBaseDir = "/Users/varmarken/temp/UCI IoT Project/layer2/evaluation/experimental_result/standalone";
-//        String signatureFilesBaseDir = "/Users/varmarken/temp/UCI IoT Project/layer2/evaluation/experimental_result/standalone";
-//        String pcapFile = pcapFileBaseDir + "/tplink-plug/wlan1/tplink-plug.wlan1.local.pcap";
-//        String onSignatureFile = signatureFilesBaseDir + "/tplink-plug/signatures/tplink-plug-onSignature-device-side.sig";
-//        String offSignatureFile = signatureFilesBaseDir + "/tplink-plug/signatures/tplink-plug-offSignature-device-side.sig";
-
-//        String pcapFile = "/Users/varmarken/temp/UCI IoT Project/layer2/kwikset-doorlock.wlan1.local.pcap";
-//        String onSignatureFile = "/Users/varmarken/temp/UCI IoT Project/layer2/kwikset-doorlock-onSignature-phone-side.sig";
-//        String offSignatureFile = "/Users/varmarken/temp/UCI IoT Project/layer2/kwikset-doorlock-offSignature-phone-side.sig";
-
         // Prepare file outputter.
         File outputFile = new File(resultsFile);
         outputFile.getParentFile().mkdirs();
         final PrintWriter resultsWriter = new PrintWriter(new FileWriter(outputFile));
         // Include metadata as comments at the top
-        outputLine("# Detection results for:", resultsWriter);
-        outputLine("# - inputPcapFile: " + pcapFile, resultsWriter);
-        outputLine("# - onSignatureFile: " + onSignatureFile, resultsWriter);
-        outputLine("# - onSignatureFile: " + offSignatureFile, resultsWriter);
+        PrintWriterUtils.println("# Detection results for:", resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT);
+        PrintWriterUtils.println("# - inputPcapFile: " + pcapFile, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT);
+        PrintWriterUtils.println("# - onSignatureFile: " + onSignatureFile, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT);
+        PrintWriterUtils.println("# - onSignatureFile: " + offSignatureFile, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT);
         resultsWriter.flush();
 
         // Create signature detectors and add observers that output their detected events.
@@ -81,11 +71,11 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb
         Layer2SignatureDetector offDetector = new Layer2SignatureDetector(PrintUtils.deserializeSignatureFromFile(offSignatureFile));
         onDetector.addObserver((signature, match) -> {
             UserAction event = new UserAction(UserAction.Type.TOGGLE_ON, match.get(0).get(0).getTimestamp());
-            outputLine(event.toString(), resultsWriter);
+            PrintWriterUtils.println(event, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT);
         });
         offDetector.addObserver((signature, match) -> {
             UserAction event = new UserAction(UserAction.Type.TOGGLE_OFF, match.get(0).get(0).getTimestamp());
-            outputLine(event.toString(), resultsWriter);
+            PrintWriterUtils.println(event, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT);
         });
 
         // Load the PCAP file
@@ -104,13 +94,6 @@ public class Layer2SignatureDetector implements PacketListener, ClusterMatcherOb
         resultsWriter.close();
     }
 
-    private static void outputLine(String line, PrintWriter pw) {
-        if (STD_OUT_OUTPUT) {
-            System.out.println(line);
-        }
-        pw.println(line);
-    }
-
     /**
      * The signature that this {@link Layer2SignatureDetector} is searching for.
      */
index 51f4623..34063d5 100644 (file)
@@ -2,12 +2,10 @@ package edu.uci.iotproject.evaluation;
 
 import edu.uci.iotproject.analysis.TriggerTrafficExtractor;
 import edu.uci.iotproject.analysis.UserAction;
+import edu.uci.iotproject.io.PrintWriterUtils;
 import edu.uci.iotproject.io.TriggerTimesFileReader;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileReader;
-import java.io.IOException;
+import java.io.*;
 import java.time.Instant;
 import java.util.ArrayList;
 import java.util.List;
@@ -21,10 +19,27 @@ import java.util.Optional;
  */
 public class DetectionResultsAnalyzer {
 
+    private static boolean DUPLICATE_OUTPUT_TO_STD_OUT = true;
+
     public static void main(String[] args) throws IOException {
+        if (args.length < 3) {
+            String errMsg = String.format("Usage: %s triggerTimesFile detectionOutputFile [stdOut]" +
+                            "\n - triggerTimesFile: the file that contains the timestamps for the user actions" +
+                            "\n - detectionOutputFile: the file that contains the detected events" +
+                            "\n - analysisResultsFile: where to write the results of the detection analysis" +
+                            "\n - stdOut: optional true/false literal indicating if output should also be printed to std out; default is true",
+                    DetectionResultsAnalyzer.class.getSimpleName());
+            return;
+        }
+        String triggerTimesFile = args[0];
+        File detectionOutputFile = new File(args[1]);
+        String analysisResultsFile = args[2];
+        if (args.length > 3) {
+            DUPLICATE_OUTPUT_TO_STD_OUT = Boolean.parseBoolean(args[3]);
+        }
+
         // -------------------------------------- Parse the input files --------------------------------------
 
-        String triggerTimesFile = args[0];
         // Read the trigger times.
         // The trigger times file does not contain event types as we initially assumed that we would just be alternating
         // between ON and OFF.
@@ -37,7 +52,6 @@ public class DetectionResultsAnalyzer {
             triggers.add(new UserAction(actionType, triggerTimestamps.get(i)));
         }
         // Read the detection output file, assuming a format as specified in UserAction.toString()
-        File detectionOutputFile = new File(args[1]);
         List<UserAction> detectedEvents = new ArrayList<>();
         try (BufferedReader br = new BufferedReader(new FileReader(detectionOutputFile))) {
             String s;
@@ -73,18 +87,22 @@ public class DetectionResultsAnalyzer {
         List<UserAction> falsePositives = new ArrayList<>();
         falsePositives.addAll(detectedEvents);
         falsePositives.removeAll(truePositives);
-        // Print the results...
-        System.out.println("---------- False negatives (events that where not detected) ----------");
+
+        // Output the results...
+        PrintWriter outputter = new PrintWriter(new FileWriter(analysisResultsFile));
+        PrintWriterUtils.println("---------- False negatives (events that where not detected) ----------", outputter, DUPLICATE_OUTPUT_TO_STD_OUT);
         for (UserAction missing : triggers) {
-            System.out.println(missing);
+            PrintWriterUtils.println(missing, outputter, DUPLICATE_OUTPUT_TO_STD_OUT);
         }
-        System.out.println("Total of " + Integer.toString(triggers.size()));
-        System.out.println();
-        System.out.println("---------- False positives (detected, but no matching trigger) ----------");
+        PrintWriterUtils.println("Total of " + Integer.toString(triggers.size()), outputter, DUPLICATE_OUTPUT_TO_STD_OUT);
+        PrintWriterUtils.printEmptyLine(outputter, DUPLICATE_OUTPUT_TO_STD_OUT);
+        PrintWriterUtils.println("---------- False positives (detected, but no matching trigger) ----------", outputter, DUPLICATE_OUTPUT_TO_STD_OUT);
         for (UserAction fp : falsePositives) {
-            System.out.println(fp);
+            PrintWriterUtils.println(fp, outputter, DUPLICATE_OUTPUT_TO_STD_OUT);
         }
-        System.out.println("Total of " + Integer.toString(falsePositives.size()));
+        PrintWriterUtils.println("Total of " + Integer.toString(falsePositives.size()), outputter, DUPLICATE_OUTPUT_TO_STD_OUT);
+        outputter.flush();
+        outputter.close();
     }
 
 }