Browse Source

Fixes some Bugs

* Possible infinite loops if Port.TriggerInterval = 0
* PacketCollector not adding packets, if link wasn't registered, but
Device is sending via the link.
Andreas T. Meyer-Berg 5 years ago
parent
commit
e2b111f60e

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

@@ -1,69 +1,98 @@
-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()));
-				}
-			}
-			
-		}
-	}
-}
+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);
+					System.out.println("Devices:");
+					for(SmartDevice d:linkDevices)
+						System.out.println(d.getName());
+					/**
+					 * 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()));
+				}
+			}
+			
+		}
+	}
+	
+	/**
+	 * Adds a packetCollector, which will collect packages from now on
+	 * @param collector new package collector
+	 */
+	public void addPacketCollector(PacketCollector collector){
+		if(!collectors.contains(collector))
+			collectors.add(collector);
+	}
+	
+	/**
+	 * Returns the packet collectors, which are collecting packets
+	 * @return active packet collectors
+	 */
+	public LinkedList<PacketCollector> getPacketCollectors(){
+		return collectors;
+	}
+	
+	/**
+	 * Removes the given packet collector, it will no longer collect packets
+	 * @param collector collector to be removed
+	 */
+	public void removePacketCollector(PacketCollector collector){
+		collectors.remove(collector);
+	}
+}

+ 149 - 148
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/PacketCollector.java

@@ -1,148 +1,149 @@
-package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.function.Predicate;
-
-/**
- * A packet collector allows collection of {@link Packet}s from one or multiple {@link Link}s or {@link SmartDevice}s.<br>
- * The Packets are stored per Link.<br>
- * All packets which are sent from/to one of the devices can be filtered by the
- * {@link #getFilter() getFilter()}.<br>
- * Packets should be collected by adding them via the
- * {@link #addPackets(Link, Collection)} method.<br>
- * 
- * @author Andreas T. Meyer-Berg
- */
-public class PacketCollector {
-	/**
-	 * Devices which packets should be collected. Just devices from/to the
-	 * device will be collected.
-	 */
-	private LinkedList<SmartDevice> devices = new LinkedList<SmartDevice>();
-
-	/**
-	 * All packets which are sent via this link are collected.
-	 */
-	private LinkedList<Link> links = new LinkedList<Link>();
-
-	/**
-	 * Packets which were collected by this packet collector
-	 */
-	private HashMap<Link,LinkedList<Packet>> collectedPackets = new HashMap<Link, LinkedList<Packet>>();
-
-	/**
-	 * Adds a new Link, which packets should be collected. All packets send via
-	 * the link will be collected from now on.
-	 * 
-	 * @param link
-	 *            link, which packets should be collected.
-	 */
-	public void addLink(Link link) {
-		if(!links.contains(link)){
-			links.add(link);
-			collectedPackets.put(link, new LinkedList<Packet>());
-		}
-	}
-
-	/**
-	 * Remove link from Links that should be collected. Packets sent via this
-	 * Link will no longer be collected. Just if a separate Device which
-	 * contains the link should be collected.
-	 * 
-	 * @param link link which packets should no longer be collected
-	 */
-	public void removeLink(Link link) {
-		links.remove(link);
-		collectedPackets.remove(link).clear();
-	}
-	
-	/**
-	 * Returns all links, which should be collected by this PacketCollector
-	 * @return links, which packets should be collected
-	 */
-	public Collection<Link> getLinks() {
-		return links;
-	}
-
-	/**
-	 * Adds device, which packets should be collected. All packets sent to or received from this device will be collected.
-	 * @param device device, which packets should be collected
-	 */
-	public void addDevice(SmartDevice device) {
-		if(!devices.contains(device))
-			devices.add(device);
-	}
-
-	/**
-	 * Remove device. Packets sent to or from this device will no longer be collected. Unless it participates in one of the links.
-	 * @param device device to be removed
-	 */
-	public void removeDevice(SmartDevice device) {
-		devices.remove(device);
-	}
-
-	/**
-	 * Returns all devices which packets are being collected by this Packet Collector.
-	 * @return devices, which packets are being collected
-	 */
-	public Collection<SmartDevice> getDevices() {
-		return devices;
-	}
-
-	/**
-	 * Predicate which is true if the given packet was sent from or to one of
-	 * the devices, stored in this PacketCollector
-	 * 
-	 * @return true if it was sent from or to one of the devices stored in this
-	 *         packet collector
-	 */
-	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)
-				/* return true if */
-				&& devices.stream().anyMatch(d -> d == p.getSource().getOwner() || d == p.getDestination().getOwner());
-	}
-
-	/**
-	 * Adds packets which should be collected by this PacketCollector.
-	 * @param link link, which the packets were sent on
-	 * @param packets packets which were sent
-	 */
-	public void addPackets(Link link, Collection<Packet> 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 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.
-	 */
-	public void resetPackets(){
-		collectedPackets.clear();
-	}
-}
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.function.Predicate;
+
+/**
+ * A packet collector allows collection of {@link Packet}s from one or multiple {@link Link}s or {@link SmartDevice}s.<br>
+ * The Packets are stored per Link.<br>
+ * All packets which are sent from/to one of the devices can be filtered by the
+ * {@link #getFilter() getFilter()}.<br>
+ * Packets should be collected by adding them via the
+ * {@link #addPackets(Link, Collection)} method.<br>
+ * 
+ * @author Andreas T. Meyer-Berg
+ */
+public class PacketCollector {
+	/**
+	 * Devices which packets should be collected. Just devices from/to the
+	 * device will be collected.
+	 */
+	private LinkedList<SmartDevice> devices = new LinkedList<SmartDevice>();
+
+	/**
+	 * All packets which are sent via this link are collected.
+	 */
+	private LinkedList<Link> links = new LinkedList<Link>();
+
+	/**
+	 * Packets which were collected by this packet collector
+	 */
+	private HashMap<Link,LinkedList<Packet>> collectedPackets = new HashMap<Link, LinkedList<Packet>>();
+
+	/**
+	 * Adds a new Link, which packets should be collected. All packets send via
+	 * the link will be collected from now on.
+	 * 
+	 * @param link
+	 *            link, which packets should be collected.
+	 */
+	public void addLink(Link link) {
+		if(!links.contains(link)){
+			links.add(link);
+			collectedPackets.put(link, new LinkedList<Packet>());
+		}
+	}
+
+	/**
+	 * Remove link from Links that should be collected. Packets sent via this
+	 * Link will no longer be collected. Just if a separate Device which
+	 * contains the link should be collected.
+	 * 
+	 * @param link link which packets should no longer be collected
+	 */
+	public void removeLink(Link link) {
+		links.remove(link);
+		collectedPackets.remove(link).clear();
+	}
+	
+	/**
+	 * Returns all links, which should be collected by this PacketCollector
+	 * @return links, which packets should be collected
+	 */
+	public Collection<Link> getLinks() {
+		return links;
+	}
+
+	/**
+	 * Adds device, which packets should be collected. All packets sent to or received from this device will be collected.
+	 * @param device device, which packets should be collected
+	 */
+	public void addDevice(SmartDevice device) {
+		if(!devices.contains(device))
+			devices.add(device);
+	}
+
+	/**
+	 * Remove device. Packets sent to or from this device will no longer be collected. Unless it participates in one of the links.
+	 * @param device device to be removed
+	 */
+	public void removeDevice(SmartDevice device) {
+		devices.remove(device);
+	}
+
+	/**
+	 * Returns all devices which packets are being collected by this Packet Collector.
+	 * @return devices, which packets are being collected
+	 */
+	public Collection<SmartDevice> getDevices() {
+		return devices;
+	}
+
+	/**
+	 * Predicate which is true if the given packet was sent from or to one of
+	 * the devices, stored in this PacketCollector
+	 * 
+	 * @return true if it was sent from or to one of the devices stored in this
+	 *         packet collector
+	 */
+	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)
+				/* return true if */
+				/*&& (devices.contains(p.getSource().getOwner())||devices.contains(p.getDestination().getOwner()))*/
+				 && devices.stream().anyMatch(d -> d == p.getSource().getOwner() || d == p.getDestination().getOwner());
+	}
+
+	/**
+	 * Adds packets which should be collected by this PacketCollector.
+	 * @param link link, which the packets were sent on
+	 * @param packets packets which were sent
+	 */
+	public void addPackets(Link link, Collection<Packet> packets) {
+		if(link == null)
+			return;
+		LinkedList<Packet> packetsOfLink = collectedPackets.get(link);
+		if(packetsOfLink !=null)
+			packetsOfLink.addAll(packets);
+		
+		collectedPackets.put(link, new LinkedList<>(packets));
+			
+	}
+	
+	/**
+	 * Returns the packets which were collected
+	 * @return collected packets
+	 */
+	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.
+	 */
+	public void resetPackets(){
+		collectedPackets.clear();
+	}
+}

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

@@ -136,7 +136,7 @@ public class Port {
 	 */
 	public void setTriggerInterval(long triggerInterval) {
 		if(triggerInterval<=0)
-			this.triggerInterval = (long)0;
+			this.triggerInterval = (long)1;
 		else
 		    this.triggerInterval = triggerInterval;
 	}