SynchronizedTrafficLightCtrlStatemachine.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package traffic.light.trafficlightctrl;
  2. import java.util.List;
  3. import traffic.light.ITimer;
  4. import traffic.light.ITimerCallback;
  5. import traffic.light.trafficlightctrl.TrafficLightCtrlStatemachine.State;
  6. /**
  7. * Runnable wrapper of TrafficLightCtrlStatemachine. This wrapper provides a thread-safe
  8. * instance of the state machine.
  9. *
  10. * Please report bugs and issues...
  11. */
  12. public class SynchronizedTrafficLightCtrlStatemachine implements ITrafficLightCtrlStatemachine {
  13. /**
  14. * The core state machine is simply wrapped and the event processing will be
  15. * delegated to that state machine instance. This instance will be created
  16. * implicitly.
  17. */
  18. protected TrafficLightCtrlStatemachine statemachine = new TrafficLightCtrlStatemachine();
  19. /**
  20. * Interface object for SCInterface
  21. */
  22. protected class SynchronizedSCInterface implements SCInterface {
  23. public List<SCInterfaceListener> getListeners() {
  24. synchronized(statemachine) {
  25. return statemachine.getSCInterface().getListeners();
  26. }
  27. }
  28. public void raisePolice_interrupt() {
  29. synchronized (statemachine) {
  30. statemachine.getSCInterface().raisePolice_interrupt();
  31. statemachine.runCycle();
  32. }
  33. }
  34. public boolean isRaisedDisplayRed() {
  35. synchronized(statemachine) {
  36. return statemachine.getSCInterface().isRaisedDisplayRed();
  37. }
  38. }
  39. public boolean isRaisedDisplayGreen() {
  40. synchronized(statemachine) {
  41. return statemachine.getSCInterface().isRaisedDisplayGreen();
  42. }
  43. }
  44. public boolean isRaisedDisplayYellow() {
  45. synchronized(statemachine) {
  46. return statemachine.getSCInterface().isRaisedDisplayYellow();
  47. }
  48. }
  49. public boolean isRaisedDisplayNone() {
  50. synchronized(statemachine) {
  51. return statemachine.getSCInterface().isRaisedDisplayNone();
  52. }
  53. }
  54. };
  55. protected SCInterface sCInterface;
  56. public SynchronizedTrafficLightCtrlStatemachine() {
  57. sCInterface = new SynchronizedSCInterface();
  58. }
  59. public synchronized SCInterface getSCInterface() {
  60. return sCInterface;
  61. }
  62. /*================ TIME EVENT HANDLING ================
  63. /** An external timer instance is required. */
  64. protected ITimer externalTimer;
  65. /** Internally we use a timer proxy that queues time events together with other input events. */
  66. protected ITimer timerProxy = new ITimer() {
  67. /** Simply delegate to external timer with a modified callback. */
  68. @Override
  69. public void setTimer(ITimerCallback callback, int eventID, long time,
  70. boolean isPeriodic) {
  71. externalTimer.setTimer(SynchronizedTrafficLightCtrlStatemachine.this, eventID, time, isPeriodic);
  72. }
  73. @Override
  74. public void unsetTimer(ITimerCallback callback, int eventID) {
  75. externalTimer.unsetTimer(SynchronizedTrafficLightCtrlStatemachine.this, eventID);
  76. }
  77. };
  78. /**
  79. * Set the {@link ITimer} for the state machine. It must be set externally
  80. * on a timed state machine before a run cycle can be correct executed.
  81. *
  82. * @param timer
  83. */
  84. public void setTimer(ITimer timer) {
  85. synchronized(statemachine) {
  86. this.externalTimer = timer;
  87. /* the wrapped state machine uses timer proxy as timer */
  88. statemachine.setTimer(timerProxy);
  89. }
  90. }
  91. /**
  92. * Returns the currently used timer.
  93. *
  94. * @return {@link ITimer}
  95. */
  96. public ITimer getTimer() {
  97. return externalTimer;
  98. }
  99. public void timeElapsed(int eventID) {
  100. synchronized (statemachine) {
  101. statemachine.timeElapsed(eventID);
  102. }
  103. }
  104. /**
  105. * init() will be delegated thread-safely to the wrapped state machine.
  106. */
  107. public void init() {
  108. synchronized(statemachine) {
  109. statemachine.init();
  110. }
  111. }
  112. /**
  113. * enter() will be delegated thread-safely to the wrapped state machine.
  114. */
  115. public void enter() {
  116. synchronized(statemachine) {
  117. statemachine.enter();
  118. }
  119. }
  120. /**
  121. * exit() will be delegated thread-safely to the wrapped state machine.
  122. */
  123. public void exit() {
  124. synchronized(statemachine) {
  125. statemachine.exit();
  126. }
  127. }
  128. /**
  129. * isActive() will be delegated thread-safely to the wrapped state machine.
  130. */
  131. public boolean isActive() {
  132. synchronized(statemachine) {
  133. return statemachine.isActive();
  134. }
  135. }
  136. /**
  137. * isFinal() will be delegated thread-safely to the wrapped state machine.
  138. */
  139. public boolean isFinal() {
  140. synchronized(statemachine) {
  141. return statemachine.isFinal();
  142. }
  143. }
  144. /**
  145. * isStateActive() will be delegated thread-safely to the wrapped state machine.
  146. */
  147. public boolean isStateActive(State state) {
  148. synchronized(statemachine) {
  149. return statemachine.isStateActive(state);
  150. }
  151. }
  152. /**
  153. * runCycle() will be delegated thread-safely to the wrapped state machine.
  154. */
  155. @Override
  156. public void runCycle() {
  157. synchronized (statemachine) {
  158. statemachine.runCycle();
  159. }
  160. }
  161. }