Browse Source

Add Labels to Connections & Packets

* Short number represents the label
-> forced per package, or defined in the connection
-> e.g. 0 normal, 1-7 anomalies
-> Maybe add mapping/user configuration later
Andreas T. Meyer-Berg 4 years ago
parent
commit
12b87b9928

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

@@ -202,4 +202,16 @@ public interface Connection {
 			return "unknown";
 		}
 	}
+	
+	/**
+	 * Returns the default label for packets of this connection represented as a short
+	 * @return default label value
+	 */
+	public short getDefaultLabel();
+	
+	/**
+	 * Set the default label value for packets of this connection
+	 * @param label new default label value
+	 */
+	public void setLabel(short label);
 }

+ 13 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/ConnectionPerformance.java

@@ -26,7 +26,8 @@ public class ConnectionPerformance implements Connection {
 	protected boolean changed = false;
 	/** Link on which this connection runs */
 	protected String name;
-	
+	/** default label */
+	protected short label;
 	/**
 	 * Initializes the connection, adds participants of the protocol
 	 * 
@@ -105,6 +106,8 @@ public class ConnectionPerformance implements Connection {
 				returnPackets.addAll(protocol.generateNextPackets(p, (long) Math.max((p.getLastTrigger()+p.getTriggerInterval()+p.getJitter()*(Math.random())),p.getLastTrigger()+p.getTriggerInterval()),Math.random()<packetLossRate));
 		}
 		returnPackets.sort((a,b)->(Long.compare(a.getTimestamp(),b.getTimestamp())));
+		if(label!=0)
+			returnPackets.forEach(p->p.setLabel(label));
 		return returnPackets;
 	}
 	
@@ -168,4 +171,13 @@ public class ConnectionPerformance implements Connection {
 		this.name = name;
 	}
 
+	@Override
+	public short getDefaultLabel() {
+		return label;
+	}
+
+	@Override
+	public void setLabel(short label) {
+		this.label = label;
+	}
 }

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

@@ -120,6 +120,9 @@ public class ConnectionPrecision extends ConnectionPerformance {
 				break;
 			last = returnPackets.getLast();
 		}
+		if(label!=0)
+			returnPackets.forEach(p->p.setLabel(label));
+		
 		return returnPackets;
 	}
 	

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

@@ -21,6 +21,12 @@ public abstract class Packet {
 	 * DestinationPort of the packet
 	 */
 	protected Port destination;
+	
+	/**
+	 * Number representing the label of the package
+	 */
+	protected short label = 0;
+	
 	/**
 	 * Creates a new packet with the given timestamp
 	 * @param timestamp time the packet was sent
@@ -94,4 +100,20 @@ public abstract class Packet {
 	 * @return Protocol name
 	 */
 	public abstract String getProtocolName();
+	
+	/**
+	 * Returns the label represented as a short
+	 * @return label value
+	 */
+	public short getLabel() {
+		return label;
+	}
+	
+	/**
+	 * Set the label value
+	 * @param label new label value
+	 */
+	public void setLabel(short label) {
+		this.label = label;
+	}
 }

+ 15 - 1
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/simpleImplementation/SimpleConnection.java

@@ -60,6 +60,10 @@ public class SimpleConnection implements Connection {
 	 */
 	private double packetLossProbability = 0.0;
 
+	/**
+	 * Default label assigned to packets
+	 */
+	protected short label = 0;
 	
 	/**
 	 * Creates a new connection between two SmartDevice Ports on a given Link, which
@@ -150,7 +154,7 @@ public class SimpleConnection implements Connection {
 		status = DONE;
 		return new LinkedList<Packet>(Arrays.asList((Packet) new SimplePacket(
 				startTime, srcOfTermination,
-				srcOfTermination == source ? destination : source, "Terminated")));
+				srcOfTermination == source ? destination : source, "Terminated", label)));
 	}
 
 	@Override
@@ -205,4 +209,14 @@ public class SimpleConnection implements Connection {
 		this.name = name;
 	}
 
+	@Override
+	public short getDefaultLabel() {
+		return label;
+	}
+
+	@Override
+	public void setLabel(short label) {
+		this.label = label;
+	}
+
 }

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

@@ -41,6 +41,8 @@ public class SimplePacket extends Packet {
 	 * @param payload
 	 *            String which represents the payload which is encapsulated by
 	 *            this packet
+	 * @param label
+	 * 			  Label of this packet, represented as a short
 	 */
 	public SimplePacket(long time, Port source, Port destination, String payload) {
 		super(time, source, destination);
@@ -48,6 +50,29 @@ public class SimplePacket extends Packet {
 		this.destination = destination;
 		this.payload = payload;
 	}
+	
+	/**
+	 * Creates a new dummy packet with a String as payload
+	 * 
+	 * @param time
+	 *            time the package was created in System.currentTimeMillis
+	 * @param source
+	 *            SmartDevice which sent this packet
+	 * @param destination
+	 *            SmartDevice which should receive this packet
+	 * @param payload
+	 *            String which represents the payload which is encapsulated by
+	 *            this packet
+	 * @param label
+	 * 			  Label of this packet, represented as a short
+	 */
+	public SimplePacket(long time, Port source, Port destination, String payload, short label) {
+		super(time, source, destination);
+		this.source = source;
+		this.destination = destination;
+		this.payload = payload;
+		this.label = label;
+	}
 
 	@Override
 	public byte[] dumpBytes() {