1 package edu.uci.iotproject;
3 import org.pcap4j.core.PcapHandle;
4 import org.pcap4j.core.PcapPacket;
6 import java.io.FileNotFoundException;
7 import java.io.PrintWriter;
8 import java.io.UnsupportedEncodingException;
11 * Models a (TCP) conversation/connection/session/flow (packet's belonging to the same session between a client and a
13 * Holds a pair of packet lengths from {@link PcapPacket}s identified as pertaining to the flow.
14 * Here we consider pairs of packet lengths, e.g., from device to cloud and cloud to device.
15 * We collect these pairs of data points as signatures that we can plot on a graph.
17 * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
18 * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
20 public class ConversationPair {
22 /* Begin instance properties */
24 * The PrintWriter object that writes data points into file
26 private PrintWriter pw;
29 * The direction of conversation
30 * true = device to server to device
32 private Direction direction;
35 * If this is the first packet processed then the value is true (it is false otherwise).
37 private boolean firstPacket;
40 * Four possible directions of conversations.
41 * E.g., DEVICE_TO_SERVER means the conversation is started from
42 * a device-server packet and then a server-device as a response.
43 * SERVER_TO_DEVICE means the conversation is started from a
44 * server-device packet and then a device-server packet as a response.
45 * The same pattern applies to PHONE_TO_SERVER and SERVER_TO_PHONE
48 public enum Direction {
56 * Constructs a ConversationPair object.
57 * @param fileName The file name to write data points into.
58 * @param direction The direction of the first packet of the pair.
60 public ConversationPair(String fileName, Direction direction) {
62 this.pw = new PrintWriter(fileName, "UTF-8");
63 this.direction = direction;
64 this.firstPacket = true;
65 } catch(UnsupportedEncodingException |
66 FileNotFoundException e) {
72 * Writes conversation pair's packet lengths.
73 * @param packet The {@link PcapPacket} object that has packet information.
74 * @param fromClient If true then this packet comes from client, e.g., device.
75 * @param fromServer If true then this packet comes from server.
77 public void writeConversationPair(PcapPacket packet, boolean fromClient, boolean fromServer) {
79 // Write device data point first and then server
80 if (direction == Direction.DEVICE_TO_SERVER || direction == Direction.PHONE_TO_SERVER) {
81 if (fromClient && firstPacket) { // first packet
82 pw.print(packet.getTimestamp() + ", " + packet.getPayload().length() + ", ");
83 System.out.print(packet.getTimestamp() + ", " + packet.getPayload().length() + ", ");
85 } else if (fromServer && !firstPacket) { // second packet
86 pw.println(packet.getPayload().length());
87 System.out.println(packet.getPayload().length());
90 // Write server data point first and then device
91 } else if (direction == Direction.SERVER_TO_DEVICE || direction == Direction.SERVER_TO_PHONE) {
92 if (fromServer && firstPacket) { // first packet
93 pw.print(packet.getTimestamp() + ", " + packet.getPayload().length() + ", ");
95 } else if (fromClient && !firstPacket) { // second packet
96 pw.println(packet.getPayload().length());
103 * Close the PrintWriter object.
105 public void close() {