Cleanup: Refactor PcapHandleReader to io package.
authorJanus Varmarken <varmarken@gmail.com>
Fri, 20 Jul 2018 21:39:54 +0000 (14:39 -0700)
committerJanus Varmarken <varmarken@gmail.com>
Fri, 20 Jul 2018 21:39:54 +0000 (14:39 -0700)
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/PcapHandleReader.java [deleted file]
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/TriggerTrafficExtractor.java
Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/io/PcapHandleReader.java [new file with mode: 0644]

diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/PcapHandleReader.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/PcapHandleReader.java
deleted file mode 100644 (file)
index e8fc4f5..0000000
+++ /dev/null
@@ -1,71 +0,0 @@
-package edu.uci.iotproject.analysis;
-
-import org.pcap4j.core.*;
-
-import java.io.EOFException;
-import java.util.concurrent.TimeoutException;
-
-/**
- * Reads packets from a {@link PcapHandle} (online or offline) and delivers those packets that pass the test exercised
- * by the provided {@link PcapPacketFilter} onto the provided {@link PacketListener}s.
- *
- * @author Janus Varmarken
- */
-public class PcapHandleReader {
-
-    private final PcapPacketFilter mPacketFilter;
-    private final PcapHandle mHandle;
-    private final PacketListener[] mPacketListeners;
-
-    /**
-     * Create a {@code PcapHandleReader}.
-     * @param handle An <em>open</em> {@link PcapHandle} that packets will be read from.
-     * @param packetFilter A {@link PcapPacketFilter} that dictates which of the packets read from {@code handle} should
-     *                     be delivered to {@code packetListeners}. Note that while a value of {@code null} is not
-     *                     permitted here, the caller can instead simply provide an implementation that always returns
-     *                     {@code true} if they want to include all packets read from {@code handle}.
-     * @param packetListeners One or more {@link PacketListener}s to which those packets read from {@code handle} that
-     *                        pass through {@code packetFilter} are delivered.
-     */
-    public PcapHandleReader(PcapHandle handle, PcapPacketFilter packetFilter, PacketListener... packetListeners) {
-        mHandle = handle;
-        mPacketFilter = packetFilter;
-        mPacketListeners = packetListeners;
-    }
-
-
-    /**
-     * Start reading (and filtering) packets from the provided {@link PcapHandle}.
-     * @throws PcapNativeException if an error occurs in the pcap native library.
-     * @throws NotOpenException if the provided {@code PcapHandle} is not open.
-     * @throws TimeoutException if packets are being read from a live capture and the timeout expired.
-     */
-    public void readFromHandle() throws PcapNativeException, NotOpenException, TimeoutException {
-        try {
-            PcapPacket prevPacket = null;
-            PcapPacket packet;
-            while ((packet = mHandle.getNextPacketEx()) != null) {
-                if (prevPacket != null && packet.getTimestamp().isBefore(prevPacket.getTimestamp())) {
-                    System.out.println("Out-of-order (in terms of timestamp) packet detected");
-                    /*
-                    // Fail early if assumption doesn't hold.
-                    mHandle.close();
-                    throw new AssertionError("Packets not in ascending temporal order");
-                    */
-                }
-                if (mPacketFilter.shouldIncludePacket(packet)) {
-                    // Packet accepted for inclusion; deliver it to observing client code.
-                    for (PacketListener consumer : mPacketListeners) {
-                        consumer.gotPacket(packet);
-                    }
-                }
-                prevPacket = packet;
-            }
-        } catch (EOFException eof) {
-            // Reached end of file. All good.
-            System.out.println(String.format("%s: finished reading pcap file", getClass().getSimpleName()));
-        }
-        mHandle.close();
-    }
-
-}
diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/io/PcapHandleReader.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/io/PcapHandleReader.java
new file mode 100644 (file)
index 0000000..2c387f3
--- /dev/null
@@ -0,0 +1,73 @@
+package edu.uci.iotproject.io;
+
+import edu.uci.iotproject.analysis.PcapPacketFilter;
+import org.pcap4j.core.*;
+
+import java.io.EOFException;
+import java.util.concurrent.TimeoutException;
+
+/**
+ * Reads packets from a {@link PcapHandle} (online or offline) and delivers those packets that pass the test exercised
+ * by the provided {@link PcapPacketFilter} onto the provided {@link PacketListener}s.
+ *
+ * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
+ * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
+ */
+public class PcapHandleReader {
+
+    private final PcapPacketFilter mPacketFilter;
+    private final PcapHandle mHandle;
+    private final PacketListener[] mPacketListeners;
+
+    /**
+     * Create a {@code PcapHandleReader}.
+     * @param handle An <em>open</em> {@link PcapHandle} that packets will be read from.
+     * @param packetFilter A {@link PcapPacketFilter} that dictates which of the packets read from {@code handle} should
+     *                     be delivered to {@code packetListeners}. Note that while a value of {@code null} is not
+     *                     permitted here, the caller can instead simply provide an implementation that always returns
+     *                     {@code true} if they want to include all packets read from {@code handle}.
+     * @param packetListeners One or more {@link PacketListener}s to which those packets read from {@code handle} that
+     *                        pass through {@code packetFilter} are delivered.
+     */
+    public PcapHandleReader(PcapHandle handle, PcapPacketFilter packetFilter, PacketListener... packetListeners) {
+        mHandle = handle;
+        mPacketFilter = packetFilter;
+        mPacketListeners = packetListeners;
+    }
+
+
+    /**
+     * Start reading (and filtering) packets from the provided {@link PcapHandle}.
+     * @throws PcapNativeException if an error occurs in the pcap native library.
+     * @throws NotOpenException if the provided {@code PcapHandle} is not open.
+     * @throws TimeoutException if packets are being read from a live capture and the timeout expired.
+     */
+    public void readFromHandle() throws PcapNativeException, NotOpenException, TimeoutException {
+        try {
+            PcapPacket prevPacket = null;
+            PcapPacket packet;
+            while ((packet = mHandle.getNextPacketEx()) != null) {
+                if (prevPacket != null && packet.getTimestamp().isBefore(prevPacket.getTimestamp())) {
+                    System.out.println("Out-of-order (in terms of timestamp) packet detected");
+                    /*
+                    // Fail early if assumption doesn't hold.
+                    mHandle.close();
+                    throw new AssertionError("Packets not in ascending temporal order");
+                    */
+                }
+                if (mPacketFilter.shouldIncludePacket(packet)) {
+                    // Packet accepted for inclusion; deliver it to observing client code.
+                    for (PacketListener consumer : mPacketListeners) {
+                        consumer.gotPacket(packet);
+                    }
+                }
+                prevPacket = packet;
+            }
+        } catch (EOFException eof) {
+            // Reached end of file. All good.
+            System.out.println(String.format("%s: finished reading pcap file", getClass().getSimpleName()));
+        }
+        mHandle.close();
+    }
+
+}