Browse Source

Adds possibility of termination to Connection

Andreas T. Meyer-Berg 6 years ago
parent
commit
8210c03ded

+ 45 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/Controller.java

@@ -1,5 +1,7 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.control;
 
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Link;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 
@@ -139,4 +141,47 @@ public class Controller {
 		sd.setY(scalePos(sd.getY(), 1.0, model.getHeight()));
 		sd.setZ(scalePos(sd.getZ(), 1.0, model.getDepth()));
 	}
+	
+	/**
+	 * Creates a new SmartDevice at the given Position
+	 * @param name name of the smartDevice
+	 * @param x_pos position on the x-Axis
+	 * @param y_pos position on the y-Axis
+	 * @param z_pos position on the z-Axis
+	 */
+	public void createSmartDevice(String name, int x_pos, int y_pos, int z_pos){
+		SmartDevice sd = new SmartDevice(name);
+		sd.setX(scalePos(x_pos, 1.0, model.getWidth()));
+		sd.setY(scalePos(y_pos, 1.0, model.getHeight()));
+		sd.setZ(scalePos(z_pos, 1.0, model.getDepth()));
+		model.addDevices(sd);
+	}
+	
+	/**
+	 * Deletes the given SmartDevice toDelete, removes it from its links and connections.
+	 * @param toDelete the SmartDevice to remove
+	 */
+	public void deleteSmartDevice(SmartDevice toDelete){
+		if(toDelete == null) return;
+		//Remove from Links
+		for(Link link:toDelete.getLinks())
+			removeSmartDeviceFromLink(toDelete, link);
+		
+		//Delete Connections
+		for(Connection c: toDelete.getConnections()){
+			c.removeSmartDevice(toDelete);
+			if(c.getSource()==null){
+				model.addTerminatingConnections(c);
+			}
+		}
+	}
+	
+	/**
+	 * Removes the SmartDevice from the given Link
+	 * @param toRemove
+	 * @param link
+	 */
+	public void removeSmartDeviceFromLink(SmartDevice toRemove, Link link){
+		toRemove.removeLink(link);
+	}
 }

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

@@ -3,29 +3,62 @@ package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 import java.util.Collection;
 
 /**
- * Connection between two or more SmartDevices, which sends packages according to a specified protocol
+ * Connection between two or more SmartDevices, which sends packages according
+ * to a specified protocol
  * 
  * @author Andreas T. Meyer-Berg
  */
 public interface Connection {
-	
+
 	/**
 	 * Returns the Link, which the connection uses
+	 * 
 	 * @return
 	 */
 	public Link getLink();
-	
+
 	/**
 	 * Returns the SmartDevice which started this connection
+	 * 
 	 * @return source SmartDevice
 	 */
 	public SmartDevice getSource();
-	
+
+	/**
+	 * Returns the SmartDevices that are Part of this Connection
+	 * 
+	 * @return SmartDevices participating in this Connection
+	 */
+	public Collection<SmartDevice> getParticipants();
+
+	/**
+	 * Removes the SmartDevice from the Connection. Should create terminating
+	 * packages, that are returned on the next call of simulateTimeIntervall.
+	 * If the Connection will be fully closed, source should be set to null and
+	 * getTerminationPackages should return the lastPackages. If the connection still
+	 * continues, a new Device should become the source.
+	 * The Calling Method should remove the Connection in the SmartDevice and
+	 * add the Connection to Model.terminatedConnections(), if source was set to null.
+	 * 
+	 * @param sd SmartDevice to Remove
+	 */
+	public void removeSmartDevice(SmartDevice sd);
+
 	/**
 	 * Simulate Time Step
+	 * 
 	 * @param startTime
 	 * @param duration
 	 * @return packets that were sent
 	 */
 	public Collection<Packet> simulateTimeIntervall(long startTime, long duration);
+	
+	/**
+	 * Returns the Packets, created to terminate the connection
+	 * 
+	 * @param startTime
+	 * @return packets that were sent
+	 */
+	public Collection<Packet> getTerminationPackages(long startTime);
+
 }

+ 11 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Model.java

@@ -1,6 +1,7 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
 import java.util.ArrayList;
+import java.util.LinkedList;
 import java.util.List;
 
 /**
@@ -12,6 +13,7 @@ public class Model {
 
 	private List<SmartDevice> devices;
 	private List<Link> connectionNetworks;
+	private List<Connection> terminatingConnections;
 	
 	/**
 	 * Width of the smart home model, 0 <= device.x < width
@@ -45,6 +47,7 @@ public class Model {
 		
 		devices = new ArrayList<SmartDevice>();
 		connectionNetworks = new ArrayList<Link>();
+		terminatingConnections = new LinkedList<Connection>();
 	}
 
 	/**
@@ -130,5 +133,13 @@ public class Model {
 	 */
 	public void setDevice_visualization_radius(int device_visualization_radius) {
 		this.device_visualization_radius = device_visualization_radius;
+	}
+	
+	public List<Connection> getTerminatingConnections() {
+		return terminatingConnections;
+	}
+
+	public void addTerminatingConnections(Connection terminatedConnection) {
+		this.terminatingConnections.add(terminatedConnection);
 	}	
 }

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

@@ -1,5 +1,7 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.LinkedList;
 
@@ -10,6 +12,11 @@ public class SimpleConnection implements Connection {
 	Link link;
 	Protocol p;
 	
+	//for Termination
+	ArrayList<Packet> terminatingPackets;
+	SmartDevice srcOfTermination;
+	SmartDevice other;
+	
 	public SimpleConnection(SmartDevice src, SmartDevice dest, Link link, Protocol p) {
 		source = src;
 		destination = dest;
@@ -32,5 +39,21 @@ public class SimpleConnection implements Connection {
 	public SmartDevice getSource() {
 		return source;
 	}
+	@Override
+	public Collection<SmartDevice> getParticipants() {
+		return new ArrayList<SmartDevice>(Arrays.asList(source, destination));
+	}
+	@Override
+	public void removeSmartDevice(SmartDevice sd) {
+		srcOfTermination = sd;
+		other = sd == source? destination : source;
+		source = null;
+		destination = null;	
+	}
+	
+	@Override
+	public Collection<Packet> getTerminationPackages(long startTime) {
+		return new ArrayList<Packet>(Arrays.asList((Packet)new SimplePacket(startTime, srcOfTermination, other)));
+	}
 
 }

+ 11 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SmartDevice.java

@@ -82,6 +82,16 @@ public class SmartDevice {
 	public void addLink(Link link) {
 		this.links.add(link);
 	}
+	
+	/**
+	 * Removes the link from this SmartDevice
+	 * @param link
+	 */
+	public void removeLink(Link link){
+		if(link == null)return;
+		links.remove(link);
+		link.removeDevice(this);
+	}
 
 	/**
 	 * @return the x
@@ -145,6 +155,7 @@ public class SmartDevice {
 	 * @param duration
 	 */
 	public void simulateTimeStep(long startTime, long duration) {
+		
 	}
 
 }