Conversation: ensure packets are added to list in descending order of their timestamps.
authorJanus Varmarken <varmarken@gmail.com>
Wed, 18 Jul 2018 01:52:52 +0000 (18:52 -0700)
committerJanus Varmarken <varmarken@gmail.com>
Wed, 18 Jul 2018 01:52:52 +0000 (18:52 -0700)
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Conversation.java

index b9f3e07..05d97e3 100644 (file)
@@ -135,6 +135,15 @@ public class Conversation {
         addSeqNumber(packet);
         // Finally add packet to list of packets pertaining to this conversation.
         mPackets.add(packet);
         addSeqNumber(packet);
         // Finally add packet to list of packets pertaining to this conversation.
         mPackets.add(packet);
+        // Preserve order of packets in list: sort according to timestamp.
+        if (mPackets.size() > 1 &&
+                mPackets.get(mPackets.size()-1).getTimestamp().isBefore(mPackets.get(mPackets.size()-2).getTimestamp())) {
+            Collections.sort(mPackets, (o1, o2) -> {
+                if (o1.getTimestamp().isBefore(o2.getTimestamp())) { return -1; }
+                else if (o2.getTimestamp().isBefore(o1.getTimestamp())) { return 1; }
+                else { return 0; }
+            });
+        }
     }
 
     /**
     }
 
     /**