7d5ddb6faaaaf461eda8afd712267abc674ab0c8
[pingpong.git] / Code / Projects / SmartPlugDetector / src / main / java / edu / uci / iotproject / ConversationPair.java
1 package edu.uci.iotproject;
2
3 import org.pcap4j.core.PcapHandle;
4 import org.pcap4j.core.PcapPacket;
5
6 import java.io.FileNotFoundException;
7 import java.io.PrintWriter;
8 import java.io.UnsupportedEncodingException;
9
10 /**
11  * Models a (TCP) conversation/connection/session/flow (packet's belonging to the same session between a client and a
12  * server).
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.
16  *
17  * @author Janus Varmarken {@literal <jvarmark@uci.edu>}
18  * @author Rahmadi Trimananda {@literal <rtrimana@uci.edu>}
19  */
20 public class ConversationPair {
21
22     /* Begin instance properties */
23     /**
24      * The PrintWriter object that writes data points into file
25      */
26     private PrintWriter pw;
27
28     /**
29      * The direction of conversation
30      * true = device to server to device
31      */
32     private Direction direction;
33
34     /**
35      * If this is the first packet processed then the value is true (it is false otherwise).
36      */
37     private boolean firstPacket;
38
39     /**
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
46      * directions.
47      */
48     public enum Direction {
49         DEVICE_TO_SERVER,
50         SERVER_TO_DEVICE,
51         PHONE_TO_SERVER,
52         SERVER_TO_PHONE
53     }
54
55     /**
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.
59      */
60     public ConversationPair(String fileName, Direction direction) {
61         try {
62             this.pw = new PrintWriter(fileName, "UTF-8");
63             this.direction = direction;
64             this.firstPacket = true;
65         } catch(UnsupportedEncodingException |
66                 FileNotFoundException e) {
67             e.printStackTrace();
68         }
69     }
70
71     /**
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.
76      */
77     public void writeConversationPair(PcapPacket packet, boolean fromClient, boolean fromServer) {
78
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() + ", ");
84                 firstPacket = false;
85             } else if (fromServer && !firstPacket) { // second packet
86                 pw.println(packet.getPayload().length());
87                 System.out.println(packet.getPayload().length());
88                 firstPacket = true;
89             }
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() + ", ");
94                 firstPacket = false;
95             } else if (fromClient && !firstPacket) { // second packet
96                 pw.println(packet.getPayload().length());
97                 firstPacket = true;
98             }
99         }
100     }
101
102     /**
103      * Close the PrintWriter object.
104      */
105     public void close() {
106         pw.close();
107     }
108 }