From: rtrimana Date: Wed, 26 Sep 2018 18:58:03 +0000 (-0700) Subject: Adding the serializer/deserializer for the entire signature. X-Git-Url: http://plrg.eecs.uci.edu/git/?p=pingpong.git;a=commitdiff_plain;h=f4b2558593967afe3007a21486292441faa3145c Adding the serializer/deserializer for the entire signature. --- diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java index 4e1c565..00b0b9b 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/Main.java @@ -300,7 +300,7 @@ public class Main { count = 0; for (List> ll : ppListOfListListOn) { PrintUtils.serializeClustersIntoFile("./onSignature" + ++count + ".sig", ll); - ppListOfListReadOn.add(PrintUtils.serializeClustersFromFile("./onSignature" + count + ".sig")); + ppListOfListReadOn.add(PrintUtils.deserializeClustersFromFile("./onSignature" + count + ".sig")); } System.out.println("========================================"); @@ -324,7 +324,7 @@ public class Main { count = 0; for (List> ll : ppListOfListListOff) { PrintUtils.serializeClustersIntoFile("./offSignature" + ++count + ".sig", ll); - ppListOfListReadOff.add(PrintUtils.serializeClustersFromFile("./offSignature" + count + ".sig")); + ppListOfListReadOff.add(PrintUtils.deserializeClustersFromFile("./offSignature" + count + ".sig")); } System.out.println("========================================"); // ============================================================================================================ diff --git a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PrintUtils.java b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PrintUtils.java index ba782ea..3d3a3be 100644 --- a/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PrintUtils.java +++ b/Code/Projects/SmartPlugDetector/src/main/java/edu/uci/iotproject/util/PrintUtils.java @@ -64,6 +64,30 @@ public class PrintUtils { } } + /** + * Write the signature {@code List>>} into a file. + * + * After the DBSCAN algorithm derives the clusters from pairs, we save the signature in the form of list of + * packet pairs. We harvest the pairs and transform them back into a list of PcapPacket objects. + * We do not maintain the pairs in the form of {@code Cluster} objects because there might be + * a situation where we could combine multiple PcapPacketPair objects into a longer signature, i.e., a string of + * PcapPacket objects and not just a pair. + * + * @param fileName The path of the file in {@link String}. We could leave this one {@code null} if we wanted the + * default file name {@code SERIALIZABLE_FILE_PATH}. + * @param signature The {@link Cluster} objects in the form of list of {@code PcapPacket} objects. + */ + public static void serializeSignatureIntoFile(String fileName, List>> signature) { + if (fileName == null) + fileName = SERIALIZABLE_FILE_PATH; + try (ObjectOutputStream oos = + new ObjectOutputStream(new FileOutputStream(fileName))) { + oos.writeObject(signature); + } catch (Exception ex) { + ex.printStackTrace(); + } + } + /** * Read the list of list of packet pairs {@code List>} from a file. * @@ -77,7 +101,7 @@ public class PrintUtils { * default file name {@code SERIALIZABLE_FILE_PATH}. * @return The list of list of {@link Cluster} objects ({@code List>}) that is read from file. */ - public static List> serializeClustersFromFile(String fileName) { + public static List> deserializeClustersFromFile(String fileName) { if (fileName == null) fileName = SERIALIZABLE_FILE_PATH; List> ppListOfList = null; @@ -91,6 +115,34 @@ public class PrintUtils { return ppListOfList; } + /** + * Read the list of list of packet pairs {@code List>>} from a file. + * + * After the DBSCAN algorithm derives the clusters from pairs, we save the signature in the form of list of + * packet pairs. We harvest the pairs and transform them back into a list of PcapPacket objects. + * We do not maintain the pairs in the form of {@code Cluster} objects because there might be + * a situation where we could combine multiple PcapPacketPair objects into a longer signature, i.e., a string of + * PcapPacket objects and not just a pair. + * + * @param fileName The path of the file in {@link String}. We could leave this one {@code null} if we wanted the + * default file name {@code SERIALIZABLE_FILE_PATH}. + * @return The list of list of list of {@link Cluster} objects ({@code List>>}) + * that is read from file. + */ + public static List>> deserializeSignatureFromFile(String fileName) { + if (fileName == null) + fileName = SERIALIZABLE_FILE_PATH; + List>> ppListOfListOfList = null; + try (ObjectInputStream ois = + new ObjectInputStream(new FileInputStream(fileName))) { + ppListOfListOfList = (List>>) ois.readObject(); + } catch (Exception ex) { + ex.printStackTrace(); + } + + return ppListOfListOfList; + } + /** * Converts a {@code PcapPacketPair} into a CSV string containing the packet lengths of the two packets in the pair. *