Browse Source

Adds Transmission delay to Link and protocols

Andreas T. Meyer-Berg 5 years ago
parent
commit
a8a708d7c7

+ 2 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/control/ImportController.java

@@ -20,7 +20,7 @@ import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.SmartDevice;
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.configuration.ImportConfiguration;
 
 /**
- * Controller which manages Imported Classes of the
+ * Controller which manages Imported Classes of the framework
  *
  * @author Andreas T. Meyer-Berg
  */
@@ -294,7 +294,7 @@ public class ImportController {
 	}
 	
 	/**
-	 * Imports the given .java File, compiles it and returns the 
+	 * Imports the given .java File, compiles it and returns the compiled class
 	 * 
 	 * @param javaFile File(path)
 	 * @return Class which was compiled

+ 8 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Link.java

@@ -77,4 +77,12 @@ public interface Link {
 	 * @return true if changed
 	 */
 	public boolean getStatusChanged();
+	
+	/**
+	 * Returns the Transmission delay between the two given devices
+	 * @param from SmartDevice which starts transmission
+	 * @param to SmartDevices which receives transmission
+	 * @return delay of the transmission, infinite if to is unreachable or the Packets is lost.
+	 */
+	public long getTransmissionDelayFrom(SmartDevice from, SmartDevice to);
 }

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

@@ -248,4 +248,16 @@ public class Port {
 			protocol=connection.getProtocol().getName();
 		return "Port: " + portNumber + protocol + " Status:" + statusToString(status) + " " + "TriggerInterval:"+triggerInterval+" "+(owner == null ? "" : " on " + owner.getName());
 	}
+	
+	/**
+	 * Returns the transmission delay in milliseconds to the other port, which is specified in the Link of the Connection.
+	 * @param to Port, which should receive the transmission
+	 * @return delay between this port and the to Port.
+	 */
+	public long getTransmissionDelayTo(Port to){
+		if(connection != null && connection.getLink() != null)
+				if(owner != null && to != null && to.getOwner()!=null)
+					return connection.getLink().getTransmissionDelayFrom(owner, to.getOwner());
+		return Long.MAX_VALUE;
+	}
 }

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

@@ -178,4 +178,9 @@ public class PrecisionLink implements Link {
 	public boolean getStatusChanged() {
 		return statusChanged;
 	}
+
+	@Override
+	public long getTransmissionDelayFrom(SmartDevice from, SmartDevice to) {
+		return 0;
+	}
 }

+ 47 - 14
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/MQTT_protocol.java

@@ -136,8 +136,12 @@ public class MQTT_protocol implements Protocol {
 			}
 			if(dest != null){
 				returnPackets.add(new MQTT_packet(MQTT_packet.PINGREQ, timestep, port, dest));
-				if(dest.getStatus()!=Port.CLOSED)
-					returnPackets.add(new MQTT_packet(MQTT_packet.PINGRESP, timestep, dest, port));
+				/**
+				 * Delay to the destination
+				 */
+				long delayBrokerDest = broker.getTransmissionDelayTo(dest);
+				if(dest.getStatus()!=Port.CLOSED && delayBrokerDest!=Long.MAX_VALUE)
+					returnPackets.add(new MQTT_packet(MQTT_packet.PINGRESP, timestep+delayBrokerDest+dest.getJitter()+dest.getResponseTime(), dest, port));
 			}
 		}
 		if(subs.contains(port)||pubSubs.contains(port)){
@@ -162,9 +166,14 @@ public class MQTT_protocol implements Protocol {
 				newTopic = available.get((int) Math.floor(Math.random()*available.size()));
 				returnPackets.add(new MQTT_packet(MQTT_packet.SUBSCRIBE, timestep, port, broker, "topic:"+newTopic));
 				
-				if(broker.getStatus()!=Port.CLOSED){
+				/**
+				 * Delay to the broker
+				 */
+				long delayPortToBroker = port.getTransmissionDelayTo(broker);
+				
+				if(broker.getStatus()!=Port.CLOSED && delayPortToBroker!=Long.MAX_VALUE){
 					timestep+=broker.getResponseTime();
-					returnPackets.add(new MQTT_packet(MQTT_packet.SUBACK, timestep, broker, port));
+					returnPackets.add(new MQTT_packet(MQTT_packet.SUBACK, timestep+delayPortToBroker, broker, port));
 					tops.add(newTopic);
 				}
 			}
@@ -172,9 +181,14 @@ public class MQTT_protocol implements Protocol {
 				//Forced Unsubscribe
 				newTopic = tops.get((int) Math.floor(Math.random()*tops.size()));
 				returnPackets.add(new MQTT_packet(MQTT_packet.UNSUBSCRIBE, timestep, port, broker, "topic:"+newTopic));
-				if(broker.getStatus()!=Port.CLOSED){
+				/**
+				 * Delay to the broker
+				 */
+				long delayPortToBroker = port.getTransmissionDelayTo(broker);
+				
+				if(broker.getStatus()!=Port.CLOSED && delayPortToBroker!=Long.MAX_VALUE){
 					timestep+=broker.getResponseTime();
-					returnPackets.add(new MQTT_packet(MQTT_packet.UNSUBACK, timestep, broker, port));
+					returnPackets.add(new MQTT_packet(MQTT_packet.UNSUBACK, timestep+delayPortToBroker, broker, port));
 					tops.remove(newTopic);
 				}
 			}
@@ -186,31 +200,50 @@ public class MQTT_protocol implements Protocol {
 			 */
 			String msg = "Topic:"+newTopic+":"+(Math.random()<0.5?"true":"false");
 			returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, port, broker, msg));
-			//Response
-			timestep+=broker.getResponseTime();
-			returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep, broker, port));
 			
+			/**
+			 * Delay to the broker
+			 */
+			long delayPortToBroker = port.getTransmissionDelayTo(broker);
 			//Publish to Subscribers
 			//Should be be improved to just notify Subs that are subscribed to the topic
-			if(broker.getStatus()!=Port.CLOSED){
+			if(broker.getStatus()!=Port.CLOSED && delayPortToBroker!=Long.MAX_VALUE){
+				//Response
+				timestep+=broker.getResponseTime()+delayPortToBroker;
+				returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep, broker, port));
+				
 				for(Port p:subs){
 					if(!subbedTopics.get(p).contains(newTopic))continue;//Skip unsubbed ports
+					/**
+					 * Delay broker to subscriber
+					 */
+					long delayBrokerToSub = broker.getTransmissionDelayTo(p);
 					timestep+=broker.getResponseTime();
 					returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, broker, p, msg));
-					returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep+p.getResponseTime(), p, broker));
+					if(p.getStatus()!=Port.CLOSED && delayBrokerToSub != Long.MAX_VALUE)
+						returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep+p.getResponseTime(), p, broker));
 				}
 				for(Port p:pubSubs){
 					if(!subbedTopics.get(p).contains(newTopic))continue;//skip unsubbed ports
 					timestep+=broker.getResponseTime();
+					/**
+					 * Delay broker to subscriber
+					 */
+					long delayBrokerToSub = broker.getTransmissionDelayTo(p);
 					returnPackets.add(new MQTT_packet(MQTT_packet.PUBLISH, timestep, broker, p, msg));
-					returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep+p.getResponseTime(), p, broker));
+					if(p.getStatus()!=Port.CLOSED && delayBrokerToSub != Long.MAX_VALUE)
+						returnPackets.add(new MQTT_packet(MQTT_packet.PUBACK, timestep+p.getResponseTime(), p, broker));
 				}
 			}
 		}
 		if(Math.random() < 0.05 && port != broker){
 			returnPackets.add(new MQTT_packet(MQTT_packet.PINGREQ, timestep, port, broker));
-			if(port.getStatus()!=Port.CLOSED)
-				returnPackets.add(new MQTT_packet(MQTT_packet.PINGRESP, timestep, broker, port));
+			/**
+			 * Delay broker to subscriber
+			 */
+			long delayPortToBroker = port.getTransmissionDelayTo(broker);
+			if(broker.getStatus()!=Port.CLOSED && delayPortToBroker != Long.MAX_VALUE)
+				returnPackets.add(new MQTT_packet(MQTT_packet.PINGRESP, timestep+delayPortToBroker+broker.getResponseTime(), broker, port));
 		}
 		return returnPackets;
 	}

+ 3 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/protocols/Ping_protocol.java

@@ -50,8 +50,9 @@ public class Ping_protocol implements Protocol {
 		
 		if(port == sender && target != null){
 			packets.add(new Ping_packet(timestep, sender, target, Ping_packet.EchoRequest, sequence));
-			if(target.getStatus() >= Port.OPEN && !packetLost){
-				packets.add(new Ping_packet(timestep+target.getResponseTime()+(short)(target.getJitter()*Math.random()), target, sender, Ping_packet.EchoReply, sequence++));
+			long delay = sender.getTransmissionDelayTo(target); 
+			if(target.getStatus() >= Port.OPEN && !packetLost && delay != Long.MAX_VALUE){
+				packets.add(new Ping_packet(timestep+target.getResponseTime()+delay+(short)(target.getJitter()*Math.random()), target, sender, Ping_packet.EchoReply, sequence++));
 			}
 		}
 		else if(port == target){

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

@@ -159,4 +159,9 @@ public class SimpleLink implements Link {
 	public boolean getStatusChanged() {
 		return statusChanged;
 	}
+
+	@Override
+	public long getTransmissionDelayFrom(SmartDevice from, SmartDevice to) {
+		return 0;
+	}
 }