Adding skipped packets analysis.
[pingpong.git] / Code / Projects / PacketLevelSignatureExtractor / src / main / java / edu / uci / iotproject / util / PcapPacketUtils.java
index 2f20945921b25c54f354835f1f0d28d03c7d2a35..c1a1a25150b112a6def2e09b867a12772027eeb3 100644 (file)
@@ -1,5 +1,6 @@
 package edu.uci.iotproject.util;
 
+import edu.uci.iotproject.io.PrintWriterUtils;
 import edu.uci.iotproject.trafficreassembly.layer3.Conversation;
 import edu.uci.iotproject.analysis.PcapPacketPair;
 import edu.uci.iotproject.analysis.TcpConversationUtils;
@@ -11,6 +12,7 @@ import org.pcap4j.packet.IpV4Packet;
 import org.pcap4j.packet.TcpPacket;
 import org.pcap4j.util.MacAddress;
 
+import java.io.PrintWriter;
 import java.util.*;
 
 /**
@@ -346,7 +348,8 @@ public final class PcapPacketUtils {
                 if (Math.abs(timestamp1 - timestamp2) < TriggerTrafficExtractor.INCLUSION_WINDOW_MILLIS) {
                     // If these two are within INCLUSION_WINDOW_MILLIS window then compare!
                     compare = p1.get(count1).get(0).getTimestamp().compareTo(p2.get(count2).get(0).getTimestamp());
-                    overlapChecking(compare, comparePrev, p1.get(count1), p2.get(count2));
+                    overlapChecking(compare, comparePrev, p1.get(count1), p2.get(count2),
+                            signatures.indexOf(p1), signatures.indexOf(p2));
                     comparePrev = compare;
                     count1++;
                     count2++;
@@ -370,8 +373,12 @@ public final class PcapPacketUtils {
      * @param comparePrev Previous comparison value between packet sequences p1 and p2
      * @param sequence1 The packet sequence ({@link List} of {@link PcapPacket} objects).
      * @param sequence2 The packet sequence ({@link List} of {@link PcapPacket} objects).
+     * @param indexSequence1 The index of packet sequence ({@link List} of {@link PcapPacket} objects).
+     * @param indexSequence2 The index of packet sequence ({@link List} of {@link PcapPacket} objects).
      */
-    private static void overlapChecking(int compare, int comparePrev, List<PcapPacket> sequence1, List<PcapPacket> sequence2) {
+    private static void overlapChecking(int compare, int comparePrev,
+                                        List<PcapPacket> sequence1, List<PcapPacket> sequence2,
+                                        int indexSequence1, int indexSequence2) {
 
         // Check if p1 occurs before p2 but both have same overlap
         if (comparePrev != 0) { // First time since it is 0
@@ -380,8 +387,8 @@ public final class PcapPacketUtils {
                 // E.g., 111, 222, 333 in one occassion and 222, 333, 111 in the other.
                 throw new Error("OVERLAP WARNING: " + "" +
                         "Two sequences have some overlap. Please remove one of the sequences: " +
-                        sequence1.get(0).length() + "... OR " +
-                        sequence2.get(0).length() + "...");
+                        sequence1.get(0).length() + " with index " + indexSequence1 + " OR " +
+                        sequence2.get(0).length() + " with index " + indexSequence2);
             }
         }
         // Check if p1 is longer than p2 and p2 occurs during the occurrence of p1
@@ -431,26 +438,32 @@ public final class PcapPacketUtils {
      *
      * @param signatures A {@link List} of {@link List} of {@link List} of
      *          {@link PcapPacket} objects that needs to be printed.
+     * @param resultsWriter PrintWriter object to write into log file.
+     * @param printToOutput Boolean to decide whether to print out to screen or just log file.
      */
-    public static void printSignatures(List<List<List<PcapPacket>>> signatures) {
+    public static void printSignatures(List<List<List<PcapPacket>>> signatures, PrintWriter resultsWriter, boolean
+                                       printToOutput) {
 
         // Iterate over the list of all clusters/sequences
         int sequenceCounter = 0;
         for(List<List<PcapPacket>> listListPcapPacket : signatures) {
             // Iterate over every member of a cluster/sequence
-            System.out.print("====== SEQUENCE " + ++sequenceCounter);
-            System.out.println(" - " + listListPcapPacket.size() + " MEMBERS ======");
+            PrintWriterUtils.print("====== SEQUENCE " + ++sequenceCounter, resultsWriter, printToOutput);
+            PrintWriterUtils.println(" - " + listListPcapPacket.size() + " MEMBERS ======", resultsWriter,
+                    printToOutput);
             for(List<PcapPacket> listPcapPacket : listListPcapPacket) {
                 // Print out packet lengths in a sequence
                 int packetCounter = 0;
                 for(PcapPacket pcapPacket : listPcapPacket) {
                     if(pcapPacket != null) {
-                        System.out.print(pcapPacket.length());
+                        PrintWriterUtils.print(pcapPacket.length(), resultsWriter, printToOutput);
                     }
                     if(packetCounter < listPcapPacket.size() - 1) {
-                        System.out.print(" ");  // Provide space if not last packet
+                        // Provide space if not last packet
+                        PrintWriterUtils.print(" ", resultsWriter, printToOutput);
                     } else {
-                        System.out.println();      // Newline if last packet
+                        // Newline if last packet
+                        PrintWriterUtils.println("", resultsWriter, printToOutput);
                     }
                     packetCounter++;
                 }