Scheduler.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. System.out.println("Min: "+minimumTimeStep+" Event: "+event.getEventTime());
  34. System.out.println("Could not schedule: "+event.toString());
  35. System.out.println("Simulate now ");
  36. event.simulateEvent(minimumTimeStep);//TODO: Maybe other solution
  37. //throw new Error("Fail");
  38. return false;
  39. }else {
  40. return eventQueue.add(event);
  41. }
  42. }
  43. /**
  44. * Schedules all network events of the given model
  45. * @param m Model which should be scheduled
  46. */
  47. public void scheduleAll(Model m){
  48. for(Connection c: m.getConnections())
  49. for(Port p:c.getParticipants()){
  50. if(p != null && p.getStatus() == Port.SENDING)
  51. scheduleEvent(p);
  52. }
  53. }
  54. /**
  55. * Return true, if the EventQueue contains another event before the given maximum time
  56. * @param maxTime exclusive maximum time events should have in this simulation interval
  57. * @return true, if
  58. */
  59. public boolean hasNext(long maxTime){
  60. if(eventQueue.isEmpty())
  61. return false;
  62. return eventQueue.first().getEventTime()<maxTime;
  63. }
  64. /**
  65. * Returns the next Event, which will be simulated
  66. * @return next event
  67. */
  68. public Schedulable getNextEvent(){
  69. return eventQueue.first();
  70. }
  71. /**
  72. * Returns the first Element of the queue and removes it.
  73. * @return
  74. */
  75. public Schedulable getAndRemoveFirst(){
  76. return eventQueue.pollFirst();
  77. }
  78. /**
  79. * Removes the event from the Event Queue, it will not be simulated
  80. * @param event Event to be removed
  81. * @return true if it was removed
  82. */
  83. public boolean removeEvent(Schedulable event){
  84. return eventQueue.remove(event);
  85. }
  86. /**
  87. * Resets the EventQueue by removing all events and resetting the minimum timestep
  88. */
  89. public void reset(long newMinTime){
  90. minimumTimeStep = newMinTime;
  91. eventQueue.clear();
  92. }
  93. /**
  94. * Returns the minimum Time, events could be scheduled.
  95. * @return minimum time step
  96. */
  97. public long getMinTime(){
  98. return minimumTimeStep;
  99. }
  100. /**
  101. * Sets the new minimum time. No events will be scheduled earlier.
  102. * @param newMinTime new minimum time
  103. */
  104. public void setMinTime(long newMinTime){
  105. this.minimumTimeStep = newMinTime;
  106. }
  107. }