Adding notes in the source code and scripts.
[pingpong.git] / Code / Projects / PacketLevelSignatureExtractor / src / main / java / edu / uci / iotproject / SignatureGenerator.java
index 0195f1d6671c1027fc861ad7957345f63fb64ee8..786c65f3d4104cd9098af5670dacbf83aab853ac 100644 (file)
@@ -29,9 +29,25 @@ import java.util.stream.Stream;
  * lengths with training data to determine certain events
  * or actions for smart home devices.
  *
+ * Notes:
+ * This first version of software is created based on binary events (i.e., ON/OFF).
+ * We first tested it with the TP-Link plug. The events ON and OFF were generated alternately for 100 times using
+ * the automation scripts (see the automation folder). Thus, this program tries to extract signatures by
+ * assuming that:
+ *
+ * (1) there are 2 types of events (binary), and
+ * (2) the two types are triggered alternately.
+ *
+ * Hence, for non-binary events, we still can trigger the same event for 100 times and PingPong still groups them
+ * in 2 groups of 50 occurrences each. Please keep in mind that the 2 groups are simply named "ON" and "OFF"
+ * in this program although we tested PingPong against other types of events. For non-binary events (e.g., Intensity)
+ * we still trigger the phone app alternately between two values---to have a comprehensive understanding, we alternately
+ * triggered the intensity values of 1% and 100%. We suspect that the variation in lengths are due to the different
+ * lengths of the string "1%" to "100%".
+ *
  * @author Janus Varmarken
  * @author Rahmadi Trimananda (rtrimana@uci.edu)
- * @version 0.1
+ * @version 1.0
  */
 public class SignatureGenerator {
 
@@ -47,6 +63,10 @@ public class SignatureGenerator {
      * Directory for logging.
      */
     private static String LOG_DIRECTORY = "./";
+    /**
+     * Multiplier for cluster bounds.
+     */
+    private static float CLUSTER_BOUNDS_MULTIPLIER = 0.1f;
 
     public static void main(String[] args) throws PcapNativeException, NotOpenException, EOFException,
             TimeoutException, UnknownHostException, IOException {
@@ -214,8 +234,8 @@ public class SignatureGenerator {
         // Perform clustering on conversation logged as part of all ON events.
         // Calculate number of events per type (only ON/only OFF), which means half of the number of all timestamps.
         int numberOfEventsPerType = triggerTimes.size() / 2;
-        int lowerBound = numberOfEventsPerType - (int)(numberOfEventsPerType * 0.1);
-        int upperBound = numberOfEventsPerType + (int)(numberOfEventsPerType * 0.1);
+        int lowerBound = numberOfEventsPerType - (int)(numberOfEventsPerType * CLUSTER_BOUNDS_MULTIPLIER);
+        int upperBound = numberOfEventsPerType + (int)(numberOfEventsPerType * CLUSTER_BOUNDS_MULTIPLIER);
         int minPts = lowerBound;
         DBSCANClusterer<PcapPacketPair> onClusterer = new DBSCANClusterer<>(eps, minPts);
         List<Cluster<PcapPacketPair>> onClusters = onClusterer.cluster(onPairs);
@@ -282,6 +302,12 @@ public class SignatureGenerator {
             }
             PcapPacketUtils.removeSequenceFromSignature(ppListOfListListOn, sequenceToDelete);
         }
+        PrintWriterUtils.println("ON Sequences: ", resultsWriter,
+                DUPLICATE_OUTPUT_TO_STD_OUT);
+        for(List<List<PcapPacket>> listOfList : ppListOfListListOn) {
+            PrintWriterUtils.println(listOfList.get(0).get(0).length() + "...", resultsWriter,
+                    DUPLICATE_OUTPUT_TO_STD_OUT);
+        }
         ppListOfListListOn = PcapPacketUtils.sortSequences(ppListOfListListOn);
         PrintWriterUtils.println("Concatenated and sorted ON signature sequences...", resultsWriter,
                 DUPLICATE_OUTPUT_TO_STD_OUT);
@@ -297,6 +323,12 @@ public class SignatureGenerator {
             }
             PcapPacketUtils.removeSequenceFromSignature(ppListOfListListOff, sequenceToDelete);
         }
+        PrintWriterUtils.println("OFF Sequences: ", resultsWriter,
+                DUPLICATE_OUTPUT_TO_STD_OUT);
+        for(List<List<PcapPacket>> listOfList : ppListOfListListOff) {
+            PrintWriterUtils.println(listOfList.get(0).get(0).length() + "...", resultsWriter,
+                    DUPLICATE_OUTPUT_TO_STD_OUT);
+        }
         ppListOfListListOff = PcapPacketUtils.sortSequences(ppListOfListListOff);
         PrintWriterUtils.println("Concatenated and sorted OFF signature sequences...", resultsWriter,
                 DUPLICATE_OUTPUT_TO_STD_OUT);
@@ -316,6 +348,9 @@ public class SignatureGenerator {
         PrintWriterUtils.println("========================================", resultsWriter,
                 DUPLICATE_OUTPUT_TO_STD_OUT);
         PcapPacketUtils.printSignatures(ppListOfListListOff, resultsWriter, DUPLICATE_OUTPUT_TO_STD_OUT);
+        // Clean signatures from null elements
+        PcapPacketUtils.cleanSignature(ppListOfListListOn);
+        PcapPacketUtils.cleanSignature(ppListOfListListOff);
         // Printing signatures into files
         PrintUtils.serializeIntoFile(onSignatureFile, ppListOfListListOn);
         PrintUtils.serializeIntoFile(offSignatureFile, ppListOfListListOff);