Browse Source

Adds first classes for the global EventQueue

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

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

@@ -85,4 +85,10 @@ public interface Link {
 	 * @return delay of the transmission, infinite if to is unreachable or the Packets is lost.
 	 */
 	public long getTransmissionDelayFrom(SmartDevice from, SmartDevice to);
+
+	/**
+	 * Adds Packets to the internal data structure
+	 * @param packets Packets, which should be added
+	 */
+	public void addPackets(Collection<Packet> packets);
 }

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

@@ -1,14 +1,17 @@
 package de.tu_darmstadt.tk.SmartHomeNetworkSim.core;
 
+import java.util.Collection;
 import java.util.Random;
 
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.Schedulable;
+
 /**
  * Representation of connection EndPoints, which allows configuration of timings
  * and if it reacts to incoming traffic or even triggers new connections.
  * 
  * @author Andreas T. Meyer-Berg
  */
-public class Port {
+public class Port implements Schedulable {
 
 	/**
 	 * A closed Port which does not react to incoming traffic
@@ -62,6 +65,11 @@ public class Port {
 	 */
 	private short jitter;
 	
+	/**
+	 * Current Jitter, updated after every simulation
+	 */
+	private short currentJitter;
+	
 	/**
 	 * Port number of this Port.
 	 */
@@ -78,7 +86,8 @@ public class Port {
 		connection = null;
 		setTriggerInterval(new Random().nextInt(1000)+1);
 		lastTrigger = 0;
-		setTriggerInterval(new Random().nextInt(5)+1);
+		jitter = (short)(new Random().nextInt(5)+1);
+		currentJitter = (short)Math.round(Math.random()*jitter);
 		responseTime = 0;
 		this.portNumber = portNumber;
 	}
@@ -96,6 +105,7 @@ public class Port {
 		this.triggerInterval = triggerInterval;
 		lastTrigger = 0;
 		jitter = 0;
+		currentJitter = (short)Math.round(Math.random()*jitter);
 		responseTime = 0;
 		this.portNumber = portNumber;
 	}
@@ -116,6 +126,7 @@ public class Port {
 		this.triggerInterval = triggerInterval;
 		this.lastTrigger = lastTrigger;
 		this.jitter = jitter;
+		currentJitter = (short)Math.round(Math.random()*jitter);
 		this.responseTime = responseTime;
 		this.portNumber = portNumber;
 	}
@@ -286,4 +297,22 @@ public class Port {
 					return connection.getLink().getTransmissionDelayFrom(owner, to.getOwner());
 		return Long.MAX_VALUE;
 	}
+
+	@Override
+	public long getEventTime() {
+		return lastTrigger+triggerInterval+currentJitter;
+	}
+
+	@Override
+	public void simulateEvent() {
+		if(connection==null || connection.getProtocol()==null || connection.getLink()==null)
+			return;
+		
+		Collection<Packet> packets = connection.getProtocol().generateNextPackets(this, getEventTime(), false);
+		//Packets encapsulate in Connection
+		
+		//Encapsulate in Links
+		connection.getLink().addPackets(packets);
+		
+	}
 }

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

@@ -201,4 +201,9 @@ public class PrecisionLink implements Link {
 	public long  getFixedDelay(){
 		return this.fixedDelay;
 	}
+
+	@Override
+	public void addPackets(Collection<Packet> packets) {
+		this.packets.addAll(packets);
+	}
 }

+ 13 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/SimulationManager.java

@@ -6,6 +6,7 @@ import java.util.Observable;
 import javax.swing.Timer;
 
 import de.tu_darmstadt.tk.SmartHomeNetworkSim.control.Controller;
+import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler.Scheduler;
 
 /**
  * Manages the Simulation by calling the relevant methods.
@@ -74,6 +75,11 @@ public class SimulationManager extends Observable {
 	 */
 	//private long perfStart = 0;
 	
+	/**
+	 * Scheduler which stores the event queue and enables scheduling and running of further events
+	 */
+	private Scheduler scheduler;
+	
 	/**
 	 * Creates a new Simulationmanager
 	 * 
@@ -86,6 +92,7 @@ public class SimulationManager extends Observable {
 		this.collectionMan = new PacketCollectionManager(model);
 		this.exportMan = new PacketExportManager(model);
 		timer = new Timer(0, t -> simulateTimeStep());
+		scheduler = new Scheduler();
 	}
 
 	/**
@@ -118,6 +125,11 @@ public class SimulationManager extends Observable {
 	 *            Duration of the simulation interval in milliseconds
 	 */
 	public void simulateTimeIntervall(long startTime, long duration) {
+		scheduler.setMinTime(startTime);
+		//TODO: Fill Queue
+		//TODO: Simulate Schedule
+		//TODO: Export
+		
 		//Simulates the network
 		simulateNetwork(startTime, duration);
 		runAlgorithms(startTime+duration);
@@ -138,6 +150,7 @@ public class SimulationManager extends Observable {
 	 *            Duration of the simulation interval in milliseconds
 	 */
 	private void simulateNetwork(long startTime, long duration){
+		//TODO: Implement Event Schedule
 		// Nothing changed so far
 		statusChanged = false;
 		// Simulate all Links, and their connections

+ 20 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/scheduler/Schedulable.java

@@ -0,0 +1,20 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler;
+
+/**
+ * Interface for events or objects, which can be scheduled during simulation of the network or world model.
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public interface Schedulable {
+	
+	/**
+	 * Returns the timestep, in which the event begins
+	 * @return timestep, in which the event begins
+	 */
+	public long getEventTime();
+	
+	/**
+	 * Simulates the given Event, might update states of model parts and schedule further events.
+	 */
+	public void simulateEvent();
+}

+ 21 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/scheduler/ScheduleComparator.java

@@ -0,0 +1,21 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler;
+
+import java.util.Comparator;
+
+/**
+ * Comparator used for scheduling different events in the global event queue
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class ScheduleComparator implements Comparator<Schedulable> {
+
+	
+	@Override
+	public int compare(Schedulable o1, Schedulable o2) {
+		int res = Long.compare(o1.getEventTime(), o2.getEventTime());
+		if(res==0)//Advanced handling ? TODO: Maybe microtime ? Some random alternation or event priority
+			return 0;
+		return res;
+	}
+
+}

+ 62 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/scheduler/Scheduler.java

@@ -0,0 +1,62 @@
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler;
+
+import java.util.TreeSet;
+
+/**
+ * The Scheduler manages the EventQueue of the framework and allows manipulating the queue.
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class Scheduler {
+
+	/**
+	 * Event Queue, which stores the different events
+	 */
+	private TreeSet<Schedulable> eventQueue;
+	
+	/**
+	 * Minimum time step. No earlier events can be scheduled
+	 */
+	private long minimumTimeStep = Long.MIN_VALUE;
+	
+	/**
+	 * Initializes a new Scheduler with an empty event queue.
+	 */
+	public Scheduler() {
+		eventQueue  = new TreeSet<Schedulable>(new ScheduleComparator());
+	}
+	
+	
+	/**
+	 * Schedules the given {@link Schedulable}, by adding it to the event queue. 
+	 * @param event Event which should be scheduled
+	 * @return true if it was scheduled, false if otherwise
+	 */
+	public boolean scheduleEvent(Schedulable event){
+		return eventQueue.add(event);
+	}
+	
+	/**
+	 * Resets the EventQueue by removing all events and resetting the minimum timestep
+	 */
+	public void reset(long newMinTime){
+		minimumTimeStep = newMinTime;
+		eventQueue.clear();
+	}
+	
+	/**
+	 * Returns the minimum Time, events could be scheduled.
+	 * @return minimum time step
+	 */
+	public long getMinTime(){
+		return minimumTimeStep;
+	}
+	
+	/**
+	 * Sets the new minimum time. No events will be scheduled earlier.
+	 * @param newMinTime new minimum time
+	 */
+	public void setMinTime(long newMinTime){
+		this.minimumTimeStep = newMinTime;
+	}
+}

+ 6 - 0
src/main/java/de/tu_darmstadt/tk/SmartHomeNetworkSim/core/scheduler/package-info.java

@@ -0,0 +1,6 @@
+/**
+ * Packet for the different classes and interfaces required for event scheduling 
+ * during the simulation of the network and world model. 
+ * @author Andreas T. Meyer-Berg
+ */
+package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler;

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

@@ -184,4 +184,9 @@ public class SimpleLink implements Link {
 	public long  getFixedDelay(){
 		return this.fixedDelay;
 	}
+
+	@Override
+	public void addPackets(Collection<Packet> packets) {
+		this.packets.addAll(packets);
+	}
 }