From: Janus Varmarken Date: Fri, 20 Jul 2018 21:39:54 +0000 (-0700) Subject: Cleanup: Refactor PcapHandleReader to io package. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=d140c34fc0c61854cb3383bbb59b6c620796b3b7;p=pingpong.git Cleanup: Refactor PcapHandleReader to io package. --- 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 index e8fc4f5..0000000 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/PcapHandleReader.java +++ /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 open {@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/analysis/TriggerTrafficExtractor.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/TriggerTrafficExtractor.java index 86cce07..c0b553c 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/TriggerTrafficExtractor.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/analysis/TriggerTrafficExtractor.java @@ -1,5 +1,6 @@ package edu.uci.iotproject.analysis; +import edu.uci.iotproject.io.PcapHandleReader; import org.pcap4j.core.*; import java.time.Instant; 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 index 0000000..2c387f3 --- /dev/null +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/io/PcapHandleReader.java @@ -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 } + * @author Rahmadi Trimananda {@literal } + */ +public class PcapHandleReader { + + private final PcapPacketFilter mPacketFilter; + private final PcapHandle mHandle; + private final PacketListener[] mPacketListeners; + + /** + * Create a {@code PcapHandleReader}. + * @param handle An open {@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(); + } + +}