瀏覽代碼

Expands the Core Package by a simple Simulation

SimpleLink, SimplePacket, SimpleConnection represent proof-of-concept
versions of the new interfaces.
Andreas T. Meyer-Berg 5 年之前
父節點
當前提交
a37bc07827

+ 4 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/Main.java

@@ -2,6 +2,7 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim;
 
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SimulationManager;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.view.MainFrame;
 
 /**
@@ -25,6 +26,7 @@ public class Main {
 	 */
 	static Controller c;
 	
+	static SimulationManager sim;
 	/**
 	 * Starts the program
 	 * @param args will be ignored
@@ -34,6 +36,8 @@ public class Main {
 		m = new Model();
 		c = new Controller(m);
 	    v = new MainFrame(m, c);
+	    sim = new SimulationManager(m);
+	    sim.simulateTimeIntervall(0, 10);
 	}
 
 }

+ 10 - 10
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Connection.java

@@ -1,25 +1,25 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
-import java.util.ArrayList;
-import java.util.List;
+import java.util.Collection;
 
 /**
  * Connection between two or more SmartDevices, which sends packages according to a specified protocol
  * 
  * @author Andreas T. Meyer-Berg
  */
-public class Connection {
+public interface Connection {
 	
 	/**
-	 * Devices communicating via this connection
+	 * return the Link, which the connection uses
+	 * @return
 	 */
-	List<SmartDevice> devices;
+	public Link getLink();
 	
 	/**
-	 * Creates a new Connection, without participants
+	 * Simulate Time Step
+	 * @param startTime
+	 * @param duration
+	 * @return packets that were sent
 	 */
-	public Connection() {
-		devices = new ArrayList<SmartDevice>();
-	}
-
+	public Collection<Packet> simulateTimeIntervall(long startTime, long duration);
 }

+ 20 - 38
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Link.java

@@ -1,66 +1,48 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
-import java.util.List;
-import java.util.ArrayList;
+import java.util.Collection;
 
 /**
  * Physical Link medium for SmartDevices, which connects two or more devices and allows communication between them
  *
  * @author Andreas T. Meyer-Berg
  */
-public class Link {
-
-	/**
-	 * name of the connection
-	 */
-	private String name;
-	
-	/**
-	 * Devices connected by this Link
-	 */
-	private List<SmartDevice> devices;
-
-	/**
-	 * Initializes a Link with name and an empty devices list.
-	 * @param name
-	 */
-	public Link(String name){
-		this.name = name;
-		this.devices = new ArrayList<SmartDevice>();
-	}
-	
+public interface Link {
 	/**
 	 * @return the name
 	 */
-	public String getName() {
-		return name;
-	}
+	public String getName();
 
 	/**
 	 * @param name the name to set
 	 */
-	public void setName(String name) {
-		this.name = name;
-	}
+	public void setName(String name);
 
 	/**
 	 * @return the devices
 	 */
-	public List<SmartDevice> getDevices() {
-		return devices;
-	}
+	public Collection<SmartDevice> getDevices();
 
 	/**
 	 * @param device the devices to add
 	 */
-	public void addDevice(SmartDevice device) {
-		this.devices.add(device);
-	}
+	public void addDevice(SmartDevice device);
 	
 	/**
 	 * @param device the devices to remove
 	 */
-	public void removeDevice(SmartDevice device) {
-		this.devices.remove(device);
-	}
+	public void removeDevice(SmartDevice device);
+	
+	/**
+	 * Simulates an intervall starting at startTime for a given duration
+	 * @param startTime 
+	 * @param duration
+	 */
+	public void simulateTimeIntervall(long startTime, long duration);
+	
+	/**
+	 * Returns all packets which where sent during the last Simulation time step
+	 * @return packets, which were sent during the last time step
+	 */
+	public Collection<Packet> getPackets();
 }

+ 30 - 12
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Model.java

@@ -39,26 +39,44 @@ public class Model {
 	 */
 	public Model() {
 		
-		setWidth(640);
-		setHeight(480);
+		setWidth(1200);
+		setHeight(700);
 		setDepth(480);
 		
 		devices = new ArrayList<SmartDevice>();
 		connectionNetworks = new ArrayList<Link>();
-		
-		SmartDevice A = new SmartDevice("SmartTV");		
-		A.setX(200);
-		A.setY(150);
-		SmartDevice B = new SmartDevice("SmartDoor");
-		B.setX(400);
-		B.setY(150);
-		SmartDevice C = new SmartDevice("SmartLight");
-		C.setX(300);
-		C.setY(300);
+		SmartDevice A = null, B = null, C = null;
+		for(int i = 0; i<5; i++){
+		A = new SmartDevice("SmartTV"+i);		
+		A.setX((int)(Math.random()*width-2*device_visualization_radius)+device_visualization_radius);
+		A.setY((int)(Math.random()*width-2*device_visualization_radius)+device_visualization_radius);
+		B = new SmartDevice("SmartDoor"+i);
+		B.setX((int)(Math.random()*width-2*device_visualization_radius)+device_visualization_radius);
+		B.setY((int)(Math.random()*width-2*device_visualization_radius)+device_visualization_radius);
+		C = new SmartDevice("SmartLight"+i);
+		C.setX((int)(Math.random()*width-2*device_visualization_radius)+device_visualization_radius);
+		C.setY((int)(Math.random()*width-2*device_visualization_radius)+device_visualization_radius);
 		
 		addDevices(A);
 		addDevices(B);
 		addDevices(C);
+		}
+		setWidth(640);
+		setHeight(480);
+		setDepth(480);
+		
+		Link link = new SimpleLink("SimpleWifi");
+		link.addDevice(A);
+		link.addDevice(B);
+		link.addDevice(C);
+		A.addLink(link);
+		B.addLink(link);
+		C.addLink(link);
+		
+		SimpleConnection s = new SimpleConnection(A, B, link);
+		A.addConnection(s);
+		B.addConnection(s);
+		this.addConnectionNetwork(link);
 	}
 
 	/**

+ 13 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Packet.java

@@ -11,4 +11,17 @@ public abstract class Packet {
 	 * @return Byte representation of the current packet
 	 */
 	public abstract Byte[] dumpBytes();
+	
+
+	/**
+	 * 
+	 * @return Textual Representation of the Package
+	 */
+	public abstract String getTextualRepresentation();
+	
+	/**
+	 * 
+	 * @return Textual Representation of the Package
+	 */
+	public abstract String getPayload();
 }

+ 13 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/PacketSniffer.java

@@ -1,5 +1,7 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
+import java.util.Collection;
+
 /**
  * Interface for collection/capturing of packets
  * 
@@ -7,4 +9,15 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  */
 public interface PacketSniffer {
 
+	/**
+	 * Add packets, that were sent
+	 * @param received
+	 */
+	public void addPackets(Collection<Packet> received);
+	
+	/**
+	 * Returns the Packets, that were sent.
+	 * @return
+	 */
+	public Collection<Packet> getPackets();
 }

+ 5 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Protocol.java

@@ -7,4 +7,9 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
  */
 public interface Protocol {
 
+	/**
+	 * Generates the next Packet
+	 * @return next Packet
+	 */
+	public Packet generatePaket();
 }

+ 28 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimpleConnection.java

@@ -0,0 +1,28 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+public class SimpleConnection implements Connection {
+
+	SmartDevice source;
+	SmartDevice destination;
+	Link link;
+	public SimpleConnection(SmartDevice src, SmartDevice dest, Link link) {
+		source = src;
+		destination = dest;
+		this.link = link;
+	}
+	@Override
+	public Link getLink() {
+		return link;
+	}
+
+	@Override
+	public Collection<Packet> simulateTimeIntervall(long startTime, long duration) {
+		LinkedList<Packet> list = new LinkedList<Packet>();
+		list.add(new SimplePacket(startTime, source, destination));
+		return list;
+	}
+
+}

+ 85 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimpleLink.java

@@ -0,0 +1,85 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.ArrayList;
+
+/**
+ * Simple Implementation of {@link Link}, which allows connection of multiple devices
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class SimpleLink implements Link {
+
+	/**
+	 * name of the connection
+	 */
+	private String name;
+	
+	/**
+	 * Devices connected by this Link
+	 */
+	private ArrayList<SmartDevice> devices;
+
+	private LinkedList<Packet> packets;
+	/**
+	 * Initializes a simple Link with name and an empty devices list.
+	 * @param name
+	 */
+	public SimpleLink(String name){
+		this.name = name;
+		this.devices = new ArrayList<SmartDevice>();
+		this.packets = new LinkedList<Packet>();
+	}
+	
+	
+	/**
+	 * @return the name
+	 */
+	public String getName() {
+		return name;
+	}
+
+	/**
+	 * @param name the name to set
+	 */
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	/**
+	 * @return the devices
+	 */
+	public Collection<SmartDevice> getDevices() {
+		return devices;
+	}
+
+	/**
+	 * @param device the devices to add
+	 */
+	public void addDevice(SmartDevice device) {
+		this.devices.add(device);
+	}
+	
+	/**
+	 * @param device the devices to remove
+	 */
+	public void removeDevice(SmartDevice device) {
+		this.devices.remove(device);
+	}
+
+
+	@Override
+	public void simulateTimeIntervall(long startTime, long duration) {
+		packets.clear();
+		for(SmartDevice sd: devices)
+			for(Connection c:sd.getConnections())
+				packets.addAll(c.simulateTimeIntervall(startTime, duration));
+	}
+
+
+	@Override
+	public Collection<Packet> getPackets() {
+		return packets;
+	}
+}

+ 45 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimplePacket.java

@@ -0,0 +1,45 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+/**
+ * Simple Dummy Packet for testing purposes
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class SimplePacket extends Packet {
+
+	/**
+	 * Time the packet was sent
+	 */
+	long time;
+	/**
+	 * source and destination
+	 */
+	SmartDevice source, destination;
+	
+	/**
+	 * Creates a new dummy packet
+	 * @param time
+	 * @param source
+	 * @param destination
+	 */
+	public SimplePacket(long time, SmartDevice source, SmartDevice destination) {
+		this.time = time;
+		this.source = source;
+		this.destination = destination;
+	}
+	@Override
+	public Byte[] dumpBytes() {
+		return this.dumpBytes();
+	}
+
+	@Override
+	public String getTextualRepresentation() {
+		return "[SimplePacket:time-"+time+";source:"+source.getName()+";dest:"+destination.getName()+"]";
+	}
+
+	@Override
+	public String getPayload() {
+		return "Empty";
+	}
+
+}

+ 43 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimulationManager.java

@@ -0,0 +1,43 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+/**
+ * Manages the Simulation by calling the relevant methods.
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class SimulationManager {
+	Model model;
+
+	/**
+	 * creates a new Simulationmanger
+	 * 
+	 * @param model
+	 */
+	public SimulationManager(Model model) {
+		this.model = model;
+	}
+
+	/**
+	 * Simulates one time step
+	 * 
+	 * @param startTime
+	 * @param duration
+	 */
+	public void simulateTimeIntervall(long startTime, long duration){
+		//Simulate all Links, and their connections
+		model.getConnectionNetworks().forEach(d -> d.simulateTimeIntervall(startTime, duration));
+		//Simulate SmartDevices - if they need some logic
+		model.getDevices().forEach(d -> d.simulateTimeStep(startTime, duration));
+		//Store Packages/Export Packages etc. 
+		model.getConnectionNetworks().forEach(d->d.getPackets()
+				.forEach(p-> 
+				{
+					if(p == null)
+						System.out.println("Packet: Null");
+					else 
+						System.out.println(p.getTextualRepresentation());
+				}));
+		
+		
+	}
+}

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

@@ -17,9 +17,14 @@ public class SmartDevice {
 	private String name;
 
 	/**
-	 * Connections to other SmartDevices
+	 * Physical connections to other SmartDevices
 	 */
 	private List<Link> links;
+	
+	/**
+	 * Virtual Connections to other SmartDevices
+	 */
+	private List<Connection> connections;
 
 	/**
 	 * Creates a new SmartDevice without links
@@ -30,6 +35,7 @@ public class SmartDevice {
 	public SmartDevice(String name) {
 		this.name = name;
 		links = new ArrayList<Link>();
+		connections = new ArrayList<Connection>();
 	}
 
 	/**
@@ -119,4 +125,26 @@ public class SmartDevice {
 		this.z = z;
 	}
 
+	/**
+	 * @return the connections
+	 */
+	public List<Connection> getConnections() {
+		return connections;
+	}
+
+	/**
+	 * @param connections the connections to set
+	 */
+	public void addConnection(Connection connections) {
+		this.connections.add(connections);
+	}
+
+	/**
+	 * Simulates the next Timestep
+	 * @param startTime
+	 * @param duration
+	 */
+	public void simulateTimeStep(long startTime, long duration) {
+	}
+
 }

+ 2 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/view/VisualisationInteractor.java

@@ -63,7 +63,8 @@ public class VisualisationInteractor implements MouseInputListener, MouseMotionL
 
 	@Override
 	public void mouseClicked(MouseEvent e) {
-
+		//if(e.getButton()==MouseEvent.BUTTON3)//rechtsklick menü etc
+			
 	}
 
 	@Override