Merging changes.
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / Main.java
index 2abf85c460881e51e1df729ee5cb196cddb08b74..98b4c270e94a305d9a3de81fe68055bfeca058a2 100644 (file)
@@ -39,9 +39,9 @@ public class Main {
         // ------------ # Code for extracting traffic generated by a device within x seconds of a trigger # ------------
         // Paths to input and output files (consider supplying these as arguments instead) and IP of the device for
         // which traffic is to be extracted:
-        String path = "/scratch/July-2018"; // Rahmadi
-//        String path = "/Users/varmarken/temp/UCI IoT Project/experiments"; // Janus
-        boolean verbose = false;
+//        String path = "/scratch/July-2018"; // Rahmadi
+        String path = "/Users/varmarken/temp/UCI IoT Project/experiments"; // Janus
+        boolean verbose = true;
         final String onPairsPath = "/scratch/July-2018/on.txt";
         final String offPairsPath = "/scratch/July-2018/off.txt";
 
@@ -205,38 +205,20 @@ public class Main {
         Map<String, Map<String, List<Conversation>>> ons = new HashMap<>();
         // Contains all OFF events: hostname -> sequence identifier -> list of conversations with that sequence
         Map<String, Map<String, List<Conversation>>> offs = new HashMap<>();
-
-        if (verbose) {
-            userActionsToConvsByHostname.forEach((ua, hostnameToConvs) -> {
-                Map<String, Map<String, List<Conversation>>> outer = ua.getType() == Type.TOGGLE_ON ? ons : offs;
-                hostnameToConvs.forEach((host, convs) -> {
-                    Map<String, List<Conversation>> seqsToConvs = TcpConversationUtils.
-                            groupConversationsByPacketSequenceVerbose(convs);
-                    outer.merge(host, seqsToConvs, (oldMap, newMap) -> {
-                        newMap.forEach((sequence, cs) -> oldMap.merge(sequence, cs, (list1, list2) -> {
-                            list1.addAll(list2);
-                            return list1;
-                        }));
-                        return oldMap;
-                    });
-                });
-            });
-        } else {
-            userActionsToConvsByHostname.forEach((ua, hostnameToConvs) -> {
-                Map<String, Map<String, List<Conversation>>> outer = ua.getType() == Type.TOGGLE_ON ? ons : offs;
-                hostnameToConvs.forEach((host, convs) -> {
-                    Map<String, List<Conversation>> seqsToConvs = TcpConversationUtils.
-                            groupConversationsByPacketSequence(convs);
-                    outer.merge(host, seqsToConvs, (oldMap, newMap) -> {
-                        newMap.forEach((sequence, cs) -> oldMap.merge(sequence, cs, (list1, list2) -> {
-                            list1.addAll(list2);
-                            return list1;
-                        }));
-                        return oldMap;
-                    });
+        userActionsToConvsByHostname.forEach((ua, hostnameToConvs) -> {
+            Map<String, Map<String, List<Conversation>>> outer = ua.getType() == Type.TOGGLE_ON ? ons : offs;
+            hostnameToConvs.forEach((host, convs) -> {
+                Map<String, List<Conversation>> seqsToConvs = TcpConversationUtils.
+                        groupConversationsByPacketSequence(convs, verbose);
+                outer.merge(host, seqsToConvs, (oldMap, newMap) -> {
+                    newMap.forEach((sequence, cs) -> oldMap.merge(sequence, cs, (list1, list2) -> {
+                        list1.addAll(list2);
+                        return list1;
+                    }));
+                    return oldMap;
                 });
             });
-        }
+        });
 
 
         // Print out all the pairs into a file for ON events
@@ -399,49 +381,49 @@ public class Main {
         // <<< Some work-in-progress/explorative code that extracts a "representative" sequence >>>
         //
         // Currently need to know relevant hostname in advance :(
-//        String hostname = "events.tplinkra.com";
+        String hostname = "events.tplinkra.com";
 //        String hostname = "rfe-us-west-1.dch.dlink.com";
-//        // Conversations with 'hostname' for ON events.
-//        List<Conversation> onsForHostname = new ArrayList<>();
-//        // Conversations with 'hostname' for OFF events.
-//        List<Conversation> offsForHostname = new ArrayList<>();
-//        // "Unwrap" sequence groupings in ons/offs maps.
-//        ons.get(hostname).forEach((k,v) -> onsForHostname.addAll(v));
-//        offs.get(hostname).forEach((k,v) -> offsForHostname.addAll(v));
-//
-//
-//        Map<String, List<Conversation>> onsForHostnameGroupedByTlsAppDataSequence = TcpConversationUtils.groupConversationsByTlsApplicationDataPacketSequence(onsForHostname);
-//
-//
-//        // Extract representative sequence for ON and OFF by providing the list of conversations with
-//        // 'hostname' observed for each event type (the training data).
-//        SequenceExtraction seqExtraction = new SequenceExtraction();
-////        ExtractedSequence extractedSequenceForOn = seqExtraction.extract(onsForHostname);
-////        ExtractedSequence extractedSequenceForOff = seqExtraction.extract(offsForHostname);
-//
-//        ExtractedSequence extractedSequenceForOn = seqExtraction.extractByTlsAppData(onsForHostname);
-//        ExtractedSequence extractedSequenceForOff = seqExtraction.extractByTlsAppData(offsForHostname);
-//
-//        // Let's check how many ONs align with OFFs and vice versa (that is, how many times an event is incorrectly
-//        // labeled).
-//        int onsLabeledAsOff = 0;
-//        Integer[] representativeOnSeq = TcpConversationUtils.getPacketLengthSequence(extractedSequenceForOn.getRepresentativeSequence());
-//        Integer[] representativeOffSeq = TcpConversationUtils.getPacketLengthSequence(extractedSequenceForOff.getRepresentativeSequence());
-//        SequenceAlignment<Integer> seqAlg = seqExtraction.getAlignmentAlgorithm();
-//        for (Conversation c : onsForHostname) {
-//            Integer[] onSeq = TcpConversationUtils.getPacketLengthSequence(c);
-//            if (seqAlg.calculateAlignment(representativeOffSeq, onSeq) <= extractedSequenceForOff.getMaxAlignmentCost()) {
-//                onsLabeledAsOff++;
-//            }
-//        }
-//        int offsLabeledAsOn = 0;
-//        for (Conversation c : offsForHostname) {
-//            Integer[] offSeq = TcpConversationUtils.getPacketLengthSequence(c);
-//            if (seqAlg.calculateAlignment(representativeOnSeq, offSeq) <= extractedSequenceForOn.getMaxAlignmentCost()) {
-//                offsLabeledAsOn++;
-//            }
-//        }
-//        System.out.println("");
+        // Conversations with 'hostname' for ON events.
+        List<Conversation> onsForHostname = new ArrayList<>();
+        // Conversations with 'hostname' for OFF events.
+        List<Conversation> offsForHostname = new ArrayList<>();
+        // "Unwrap" sequence groupings in ons/offs maps.
+        ons.get(hostname).forEach((k,v) -> onsForHostname.addAll(v));
+        offs.get(hostname).forEach((k,v) -> offsForHostname.addAll(v));
+
+
+        Map<String, List<Conversation>> onsForHostnameGroupedByTlsAppDataSequence = TcpConversationUtils.groupConversationsByTlsApplicationDataPacketSequence(onsForHostname);
+
+
+        // Extract representative sequence for ON and OFF by providing the list of conversations with
+        // 'hostname' observed for each event type (the training data).
+        SequenceExtraction seqExtraction = new SequenceExtraction();
+//        ExtractedSequence extractedSequenceForOn = seqExtraction.extract(onsForHostname);
+//        ExtractedSequence extractedSequenceForOff = seqExtraction.extract(offsForHostname);
+
+        ExtractedSequence extractedSequenceForOn = seqExtraction.extractByTlsAppData(onsForHostname);
+        ExtractedSequence extractedSequenceForOff = seqExtraction.extractByTlsAppData(offsForHostname);
+
+        // Let's check how many ONs align with OFFs and vice versa (that is, how many times an event is incorrectly
+        // labeled).
+        int onsLabeledAsOff = 0;
+        Integer[] representativeOnSeq = TcpConversationUtils.getPacketLengthSequence(extractedSequenceForOn.getRepresentativeSequence());
+        Integer[] representativeOffSeq = TcpConversationUtils.getPacketLengthSequence(extractedSequenceForOff.getRepresentativeSequence());
+        SequenceAlignment<Integer> seqAlg = seqExtraction.getAlignmentAlgorithm();
+        for (Conversation c : onsForHostname) {
+            Integer[] onSeq = TcpConversationUtils.getPacketLengthSequence(c);
+            if (seqAlg.calculateAlignment(representativeOffSeq, onSeq) <= extractedSequenceForOff.getMaxAlignmentCost()) {
+                onsLabeledAsOff++;
+            }
+        }
+        int offsLabeledAsOn = 0;
+        for (Conversation c : offsForHostname) {
+            Integer[] offSeq = TcpConversationUtils.getPacketLengthSequence(c);
+            if (seqAlg.calculateAlignment(representativeOnSeq, offSeq) <= extractedSequenceForOn.getMaxAlignmentCost()) {
+                offsLabeledAsOn++;
+            }
+        }
+        System.out.println("");
         // ================================================================================================