Browse Source

Adds Connection for complex protocols and improves Link, Port, Protocol

Port.triggerIntervall has to be bigger than 0 to prevent loops.
ConnectionImplementation allows connections with more than two
participants.
Andreas T. Meyer-Berg 5 years ago
parent
commit
bc8530346a

+ 98 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/ConnectionImplementation.java

@@ -0,0 +1,98 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+public class ConnectionImplementation implements Connection {
+
+	private Link link;
+	private LinkedList<Port> participants;
+	private Protocol protocol;
+	private double packetLossRate;
+	private byte status;
+	
+	public ConnectionImplementation(Link l, Protocol p) {
+		link = l;
+		participants=new LinkedList<Port>();
+		this.protocol = p;
+		status =Connection.ACTIVE;
+	}
+	
+	@Override
+	public Link getLink() {
+		return link;
+	}
+
+	@Override
+	public Port getSource() {
+		if(participants.size()==0)return null;
+		return participants.getFirst();
+	}
+
+	@Override
+	public Collection<Port> getParticipants() {
+		return participants;
+	}
+
+	@Override
+	public boolean removeSmartDevice(Port sd) {
+		return participants.remove(sd);
+	}
+
+	@Override
+	public boolean addSmartDevice(Port sd) {
+		return participants.add(sd);
+	}
+
+	@Override
+	public Collection<Packet> simulateTimeInterval(long startTime, long duration) {
+		LinkedList<Packet> returnPackets=new LinkedList<Packet>();
+		returnPackets.addAll(getTerminationPackages(startTime));
+		for(Port p:participants){
+			if(p.getLastTrigger()+p.getTriggerInterval()<startTime && p.getStatus()==Port.SENDING){
+				returnPackets.addAll(protocol.generateNextPakets(p, startTime,Math.random()<packetLossRate));
+			}
+			while(p.getLastTrigger()+p.getTriggerInterval()<startTime+duration &&p.getStatus()==Port.SENDING)
+				returnPackets.addAll(protocol.generateNextPakets(p, p.getLastTrigger()+p.getTriggerInterval(),Math.random()<packetLossRate));
+		}
+		returnPackets.sort((a,b)->(Long.compare(a.getTimestamp(),b.getTimestamp())));
+		return returnPackets;
+	}
+
+	@Override
+	public Collection<Packet> getTerminationPackages(long startTime) {
+		return protocol.generateNextPakets(null, startTime, false);
+	}
+
+	@Override
+	public Protocol getProtocol() {
+		return protocol;
+	}
+
+	@Override
+	public boolean setProtocol(Protocol protocol) {
+		this.protocol = protocol;
+		return true;
+	}
+
+	@Override
+	public byte getStatus() {
+		return status;
+	}
+
+	@Override
+	public void setStatus(byte status) {
+		this.status = status;
+	}
+
+	@Override
+	public void setPacketLossProbability(double lossPercentage) {
+		packetLossRate=lossPercentage;
+	}
+
+	@Override
+	public double getPacketLossProbability() {
+		return packetLossRate;
+	}
+
+}

+ 8 - 2
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/Port.java

@@ -118,7 +118,10 @@ public class Port {
 	 *            the triggerInterval to set
 	 */
 	public void setTriggerInterval(long triggerInterval) {
-		this.triggerInterval = triggerInterval;
+		if(triggerInterval<=0)
+			this.triggerInterval = (long)0;
+		else
+		    this.triggerInterval = triggerInterval;
 	}
 
 	/**
@@ -133,7 +136,10 @@ public class Port {
 	 *            the responseTime to set
 	 */
 	public void setResponseTime(short responseTime) {
-		this.responseTime = responseTime;
+		if(responseTime<=0)
+			this.responseTime = 0;
+		else
+			this.responseTime = responseTime;
 	}
 
 	/**

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

@@ -11,9 +11,11 @@ import java.util.Collection;
 public interface Protocol {
 	
 	/**
-	 * Generates the next packet
+	 * Generates the next packets<br>
+	 * If {@code port==null} the terminating packets shall be sent.<br>
+	 * This method should update port.setLastTrigger.<br>
 	 * 
-	 * @param port SmartDevice(Port) which sends the package
+	 * @param port SmartDevice(Port) which sends the package, null if terminating packets shall be sent.
 	 * @param timestep
 	 *            Time the package should be sent, in System.currentTimeMillis
 	 * @param packetLost True if the packet was lost

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

@@ -28,6 +28,9 @@ public class SimpleLink implements Link {
 	 */
 	private ArrayList<SmartDevice> devices;
 
+	/**
+	 * Connections running via this Link
+	 */
 	private ArrayList<Connection> connections;
 	/**
 	 * List of packages to store packages sent during simulation intervals, or

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

@@ -33,6 +33,7 @@ public class SimpleProtocol implements Protocol {
 	@Override
 	public Collection<Packet> generateNextPakets(Port p, long timestep, boolean packetLost) {
 		LinkedList<Packet> ret = new LinkedList<Packet>();
+		if(p==null)return ret;
 		p.setLastTrigger(timestep);
 		if(packetLost)ret.add(new SimplePacket(timestep, p, p==source?destination:source, "Lost"));
 		if(p == destination)