Browse Source

Adds PacketCollectionManager to Simulation

Andreas T. Meyer-Berg 5 years ago
parent
commit
21552a484c

+ 69 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/PacketCollectionManager.java

@@ -0,0 +1,69 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.stream.Collectors;
+
+/**
+ * The PacketCollectionManager, which stores all the active {@link PacketCollector} and adds their packets after each {@link SimulationManager#simulateTimeIntervall(long, long)} step.
+ * 
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class PacketCollectionManager {
+	/**
+	 * All collectors registered in the framework
+	 */
+	private LinkedList<PacketCollector> collectors = new LinkedList<PacketCollector>();
+	/**
+	 * Model used by the framework
+	 */
+	private Model model;
+	
+	/**
+	 * Initializes a PacketCollectionManager for the framework
+	 */
+	public PacketCollectionManager(Model model) {
+		this.model = model;
+	}
+	
+	/**
+	 * Let all packet collectors collect their packets
+	 */
+	public void collectPackets(){
+		/**
+		 * Let all collectors collect
+		 */
+		for(PacketCollector col: collectors){
+			col.resetPackets();
+			/**
+			 * Add all links, which packets should be collected
+			 */
+			Collection<Link> links = col.getLinks();
+			/**
+			 * Devices which packets should be collected
+			 */
+			LinkedList<SmartDevice> devices = new LinkedList<SmartDevice>(col.getDevices());
+			for(Link link: model.getConnectionNetworks()){
+				/**
+				 * Collect all packets of the links, which should be collected
+				 */
+				if(links.contains(link)){
+					col.addPackets(link, link.getPackets());
+				}else if(!devices.isEmpty()){
+					/**
+					 * Devices which are part of the link and should be collected by the PacketCollector
+					 */
+					LinkedList<SmartDevice> linkDevices = new LinkedList<SmartDevice>(link.getDevices());
+					linkDevices.retainAll(devices);
+					/**
+					 * Check packets just if devices, which are part of the link, should be collected
+					 */
+					if(!linkDevices.isEmpty())
+						col.addPackets(link, link.getPackets().stream().filter(col.getFilter()).collect(Collectors.toList()));
+				}
+			}
+			
+		}
+	}
+}

+ 25 - 5
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/PacketCollector.java

@@ -1,6 +1,7 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.function.Predicate;
 
@@ -29,7 +30,7 @@ public class PacketCollector {
 	/**
 	 * Packets which were collected by this packet collector
 	 */
-	private LinkedList<Packet> collectedPackets = new LinkedList<Packet>();
+	private HashMap<Link,LinkedList<Packet>> collectedPackets = new HashMap<Link, LinkedList<Packet>>();
 
 	/**
 	 * Adds a new Link, which packets should be collected. All packets send via
@@ -39,8 +40,10 @@ public class PacketCollector {
 	 *            link, which packets should be collected.
 	 */
 	public void addLink(Link link) {
-		if(!links.contains(link))
+		if(!links.contains(link)){
 			links.add(link);
+			collectedPackets.put(link, new LinkedList<Packet>());
+		}
 	}
 
 	/**
@@ -52,6 +55,7 @@ public class PacketCollector {
 	 */
 	public void removeLink(Link link) {
 		links.remove(link);
+		collectedPackets.remove(link).clear();
 	}
 	
 	/**
@@ -94,7 +98,7 @@ public class PacketCollector {
 	 * @return true if it was sent from or to one of the devices stored in this
 	 *         packet collector
 	 */
-	public Predicate<? extends Packet> getFilter() {
+	public Predicate<? super Packet> getFilter() {
 		return p -> /* filter devices where source or destination is null */
 		(p.getSource() != null && p.getDestination() != null && p.getSource().getOwner() != null
 				&& p.getDestination().getOwner() != null)
@@ -108,17 +112,33 @@ public class PacketCollector {
 	 * @param packets packets which were sent
 	 */
 	public void addPackets(Link link, Collection<Packet> packets) {
-		collectedPackets.addAll(packets);
+		if(link == null)
+			return;
+		LinkedList<Packet> packetsOfLink = collectedPackets.get(link);
+		if(packetsOfLink !=null)
+			packetsOfLink.addAll(packets);
+		else if(links.contains(link))
+			collectedPackets.put(link, new LinkedList<>(packets));
+			
 	}
 	
 	/**
 	 * Returns the packets which were collected
 	 * @return collected packets
 	 */
-	public LinkedList<Packet> getPackets(){
+	public HashMap<Link,LinkedList<Packet>> getPackets(){
 		return collectedPackets;
 	}
 	
+	/**
+	 * Return the collected Packets of the link
+	 * @param link Link, which packets should be returned
+	 * @return packets, collected on the given link
+	 */
+	public LinkedList<Packet> getPacketsOfLink(Link link){
+		return collectedPackets.get(link);
+	}
+	
 	/**
 	 * Resets the collected Packets list, by clearing the list.
 	 */

+ 16 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimulationManager.java

@@ -84,7 +84,12 @@ public class SimulationManager extends Observable {
 	 * ManipulationAlgorithm Instances, which should run in each timestep
 	 */
 	private LinkedList<NetworkManipulationAlgorithm> algos = new LinkedList<NetworkManipulationAlgorithm>();
-
+	
+	/**
+	 * PacketCollectionManager which manages the collection of packets
+	 */
+	private PacketCollectionManager packetCollectionManager;
+	
 	/**
 	 * Creates a new Simulationmanager
 	 * 
@@ -94,6 +99,7 @@ public class SimulationManager extends Observable {
 	public SimulationManager(Model model) {
 		this.model = model;
 		this.controller = null;
+		this.packetCollectionManager = new PacketCollectionManager(model);
 		timer = new Timer(0, t -> simulateTimeStep());
 	}
 
@@ -147,6 +153,7 @@ public class SimulationManager extends Observable {
 			model.notifyObservers();
 		}
 		runAlgorithms(startTime);
+		packetCollectionManager.collectPackets();
 		exportPacketsOfLastTimeStep();
 	}
 
@@ -458,4 +465,12 @@ public class SimulationManager extends Observable {
 	public void removeAlgo(NetworkManipulationAlgorithm algo){
 		algos.remove(algo);
 	}
+	
+	/**
+	 * Returns the PacketCollectionManager of the simulation
+	 * @return PacketCollectionManager, which contains all the different packet collectors
+	 */
+	public PacketCollectionManager getPacketCollectionManager(){
+		return packetCollectionManager;
+	}
 }