Jelajahi Sumber

Adds PacketLoss Option to Connections and Protocols

Connections now have a probability of packet loss. If a packet would be
lost, value true is sent to the packet generating Protocol, which should
generate the packets following the loss.
Andreas T. Meyer-Berg 5 tahun lalu
induk
melakukan
e6bc5a494c

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

@@ -47,7 +47,7 @@ public class Main {
 	    v = new MainFrame(m, c);
 	    sim = new SimulationManager(m);
 	    initializeTest();
-	    sim.simulateTimeIntervall(0, 100000);
+	    sim.simulateTimeIntervall(0, 1000);
 	}
 	
 	/**
@@ -90,7 +90,7 @@ public class Main {
 		B.addPort(b);
 		
 		SimpleConnection s = new SimpleConnection(a, b, link, new SimpleProtocol(a, b));
-
+		s.setPacketLossProbability(0.01);//1% Packet loss probability
 		a.setConnection(s);
 		b.setConnection(s);
 		m.addConnectionNetwork(link);

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

@@ -134,4 +134,18 @@ public interface Connection {
 	 *      {@link Connection#TERMINATED} and {@link Connection#HALTED}
 	 */
 	public void setStatus(byte status);
+	
+	/**
+	 * Set probability of packet loss. Should be between 0.0 (No packets are lost) and 1.0 (All packets are dropped)
+	 * 
+	 * @param lossPercentage Probability that packets are lost
+	 */
+	public void setPacketLossProbability(double lossPercentage);
+	
+	/**
+	 * Returns the probability that packets are lost 
+	 * 
+	 * @return probability of packet loss (between 0.0 and 1.0
+	 */
+	public double getPacketLossProbability();
 }

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

@@ -16,9 +16,10 @@ public interface Protocol {
 	 * @param port SmartDevice(Port) which sends the package
 	 * @param timestep
 	 *            Time the package should be sent, in System.currentTimeMillis
+	 * @param packetLost True if the packet was lost
 	 * @return next Packet, which was sent
 	 */
-	public Packet generateNextPaket(Port port, long timestep);
+	public Packet generateNextPaket(Port port, long timestep, boolean packetLost);
 
 	/**
 	 * Returns the number of different roles the participating SmartDevice(Port)s

+ 22 - 7
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimpleConnection.java

@@ -43,15 +43,20 @@ public class SimpleConnection implements Connection {
 	 * Transmission status of the connection
 	 */
 	private byte status = Connection.ACTIVE;
+	
+	/**
+	 * Probability of packet loss. (default: 0.0 -> no packets are lost. 1.0 -> all packets are lost)
+	 */
+	private double packetLossProbability = 0.0;
 
 	/**
-	 * Creates a new connection between to SmartDevices on a given Link, which
+	 * Creates a new connection between two SmartDevice Ports on a given Link, which
 	 * communicate with a protocol p
 	 * 
 	 * @param src
-	 *            SmartDevice which is the source of this connection
+	 *            Port which is the source of this connection
 	 * @param dest
-	 *            SmartDevice which is the destination of this connection
+	 *            Port which is the destination of this connection
 	 * @param link
 	 *            Link which connects {@code src} and {@code dest}
 	 * @param p
@@ -75,16 +80,16 @@ public class SimpleConnection implements Connection {
 		if(status != ACTIVE)return list;
 		//Generate packets by source
 		if(source.getLastTrigger()+source.getTriggerInterval()<startTime && source.getStatus()==Port.SENDING){
-			list.add(p.generateNextPaket(source, startTime));
+			list.add(p.generateNextPaket(source, startTime,Math.random()<packetLossProbability));
 		}
 		while(source.getLastTrigger()+source.getTriggerInterval()<startTime+duration &&source.getStatus()==Port.SENDING)
-			list.add(p.generateNextPaket(source, source.getLastTrigger()+source.getTriggerInterval()));
+			list.add(p.generateNextPaket(source, source.getLastTrigger()+source.getTriggerInterval(),Math.random()<packetLossProbability));
 		
 		if(destination.getLastTrigger()+destination.getTriggerInterval()<startTime && destination.getStatus()==Port.SENDING){
-			list.add(p.generateNextPaket(destination, startTime));
+			list.add(p.generateNextPaket(destination, startTime,Math.random()<packetLossProbability));
 		}
 		while(destination.getLastTrigger()+destination.getTriggerInterval()<startTime+duration &&destination.getStatus()==Port.SENDING)
-			list.add(p.generateNextPaket(destination, destination.getLastTrigger()+destination.getTriggerInterval()));
+			list.add(p.generateNextPaket(destination, destination.getLastTrigger()+destination.getTriggerInterval(),Math.random()<packetLossProbability));
 
 		list.sort((x,y) -> Long.compare(x.getTimestamp(),y.getTimestamp()));
 		return list;
@@ -147,4 +152,14 @@ public class SimpleConnection implements Connection {
 		this.status = status;
 	}
 
+	@Override
+	public void setPacketLossProbability(double lossPercentage) {
+		packetLossProbability = lossPercentage;
+	}
+
+	@Override
+	public double getPacketLossProbability() {
+		return packetLossProbability;
+	}
+
 }

+ 2 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimpleProtocol.java

@@ -30,8 +30,9 @@ public class SimpleProtocol implements Protocol {
 		srcSends = true;
 	}
 	@Override
-	public Packet generateNextPaket(Port p, long timestep) {
+	public Packet generateNextPaket(Port p, long timestep, boolean packetLost) {
 		p.setLastTrigger(timestep);
+		if(packetLost)return new SimplePacket(timestep, p.getOwner(), p==source?destination.getOwner():source.getOwner(), "Lost");
 		if(p == destination)
 			return new SimplePacket(timestep, destination.getOwner(), source.getOwner());
 		else