Scheduler.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. package de.tu_darmstadt.tk.SmartHomeNetworkSim.core.scheduler;
  2. import java.util.TreeSet;
  3. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Connection;
  4. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Model;
  5. import de.tu_darmstadt.tk.SmartHomeNetworkSim.core.Port;
  6. /**
  7. * The Scheduler manages the EventQueue of the framework and allows manipulating the queue.
  8. *
  9. * @author Andreas T. Meyer-Berg
  10. */
  11. public class Scheduler {
  12. /**
  13. * Event Queue, which stores the different events
  14. */
  15. private TreeSet<Schedulable> eventQueue;
  16. /**
  17. * Minimum time step. No earlier events can be scheduled
  18. */
  19. private long minimumTimeStep = Long.MIN_VALUE;
  20. /**
  21. * Initializes a new Scheduler with an empty event queue.
  22. */
  23. public Scheduler() {
  24. eventQueue = new TreeSet<Schedulable>(new ScheduleComparator());
  25. }
  26. /**
  27. * Schedules the given {@link Schedulable}, by adding it to the event queue.
  28. * @param event Event which should be scheduled
  29. * @return true if it was scheduled, false if otherwise
  30. */
  31. public boolean scheduleEvent(Schedulable event){
  32. if(event.getEventTime()<minimumTimeStep)
  33. return false;
  34. return eventQueue.add(event);
  35. }
  36. /**
  37. * Schedules all network events of the given model
  38. * @param m Model which should be scheduled
  39. */
  40. public void scheduleAll(Model m){
  41. for(Connection c: m.getConnections())
  42. for(Port p:c.getParticipants()){
  43. if(p != null && p.getStatus() == Port.SENDING)
  44. scheduleEvent(p);
  45. }
  46. }
  47. /**
  48. * Return true, if the EventQueue contains another event before the given maximum time
  49. * @param maxTime exclusive maximum time events should have in this simulation interval
  50. * @return true, if
  51. */
  52. public boolean hasNext(long maxTime){
  53. if(eventQueue.isEmpty())
  54. return false;
  55. return eventQueue.first().getEventTime()<maxTime;
  56. }
  57. /**
  58. * Returns the next Event, which will be simulated
  59. * @return next event
  60. */
  61. public Schedulable getNextEvent(){
  62. return eventQueue.first();
  63. }
  64. /**
  65. * Returns the first Element of the queue and removes it.
  66. * @return
  67. */
  68. public Schedulable getAndRemoveFirst(){
  69. return eventQueue.pollFirst();
  70. }
  71. /**
  72. * Removes the event from the Event Queue, it will not be simulated
  73. * @param event Event to be removed
  74. * @return true if it was removed
  75. */
  76. public boolean removeEvent(Schedulable event){
  77. return eventQueue.remove(event);
  78. }
  79. /**
  80. * Resets the EventQueue by removing all events and resetting the minimum timestep
  81. */
  82. public void reset(long newMinTime){
  83. minimumTimeStep = newMinTime;
  84. eventQueue.clear();
  85. }
  86. /**
  87. * Returns the minimum Time, events could be scheduled.
  88. * @return minimum time step
  89. */
  90. public long getMinTime(){
  91. return minimumTimeStep;
  92. }
  93. /**
  94. * Sets the new minimum time. No events will be scheduled earlier.
  95. * @param newMinTime new minimum time
  96. */
  97. public void setMinTime(long newMinTime){
  98. this.minimumTimeStep = newMinTime;
  99. }
  100. }