浏览代码

Adds PacketCollector class

A packet collector should allow collecting of packets from one or more
devices and links and keep these packets until they are processed by
other sources like ManipulationAlgorithms oder MachineLearning
interfaces.
Andreas T. Meyer-Berg 6 年之前
父节点
当前提交
30cfd987f8

+ 128 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/PacketCollector.java

@@ -0,0 +1,128 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import java.util.Collection;
+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 LinkedList<Packet> collectedPackets = new 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);
+	}
+
+	/**
+	 * 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);
+	}
+	
+	/**
+	 * 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<? extends 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) {
+		collectedPackets.addAll(packets);
+	}
+	
+	/**
+	 * Returns the packets which were collected
+	 * @return collected packets
+	 */
+	public LinkedList<Packet> getPackets(){
+		return collectedPackets;
+	}
+	
+	/**
+	 * Resets the collected Packets list, by clearing the list.
+	 */
+	public void resetPackets(){
+		collectedPackets.clear();
+	}
+}

+ 4 - 3
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/util/package-info.java

@@ -1,8 +1,9 @@
 /**
+ * A utility package for small classes, which are used in the UI. Like
+ * {@link de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.JavaFileFilter},
+ * {@link de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.IntegerInputValidator}
+ * and {@link de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util.LinkToolTip}.
  * 
- */
-/**
- *
  * @author Andreas T. Meyer-Berg
  */
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.view.util;