SynchronizedTrafficLightCtrlStatemachine.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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 void raisePolice_interrupt() {
  24. synchronized (statemachine) {
  25. statemachine.getSCInterface().raisePolice_interrupt();
  26. statemachine.runCycle();
  27. }
  28. }
  29. public void raisePedestrian_request() {
  30. synchronized (statemachine) {
  31. statemachine.getSCInterface().raisePedestrian_request();
  32. statemachine.runCycle();
  33. }
  34. }
  35. };
  36. protected SCInterface sCInterface;
  37. /**
  38. * Interface object for SCICars
  39. */
  40. protected class SynchronizedSCICars implements SCICars {
  41. public List<SCICarsListener> getListeners() {
  42. synchronized(statemachine) {
  43. return statemachine.getSCICars().getListeners();
  44. }
  45. }
  46. public boolean isRaisedDisplayRed() {
  47. synchronized(statemachine) {
  48. return statemachine.getSCICars().isRaisedDisplayRed();
  49. }
  50. }
  51. public boolean isRaisedDisplayGreen() {
  52. synchronized(statemachine) {
  53. return statemachine.getSCICars().isRaisedDisplayGreen();
  54. }
  55. }
  56. public boolean isRaisedDisplayYellow() {
  57. synchronized(statemachine) {
  58. return statemachine.getSCICars().isRaisedDisplayYellow();
  59. }
  60. }
  61. public boolean isRaisedDisplayNone() {
  62. synchronized(statemachine) {
  63. return statemachine.getSCICars().isRaisedDisplayNone();
  64. }
  65. }
  66. };
  67. protected SCICars sCICars;
  68. /**
  69. * Interface object for SCIPedestrians
  70. */
  71. protected class SynchronizedSCIPedestrians implements SCIPedestrians {
  72. public List<SCIPedestriansListener> getListeners() {
  73. synchronized(statemachine) {
  74. return statemachine.getSCIPedestrians().getListeners();
  75. }
  76. }
  77. public boolean isRaisedDisplayRed() {
  78. synchronized(statemachine) {
  79. return statemachine.getSCIPedestrians().isRaisedDisplayRed();
  80. }
  81. }
  82. public boolean isRaisedDisplayGreen() {
  83. synchronized(statemachine) {
  84. return statemachine.getSCIPedestrians().isRaisedDisplayGreen();
  85. }
  86. }
  87. public boolean isRaisedDisplayNone() {
  88. synchronized(statemachine) {
  89. return statemachine.getSCIPedestrians().isRaisedDisplayNone();
  90. }
  91. }
  92. };
  93. protected SCIPedestrians sCIPedestrians;
  94. public SynchronizedTrafficLightCtrlStatemachine() {
  95. sCInterface = new SynchronizedSCInterface();
  96. sCICars = new SynchronizedSCICars();
  97. sCIPedestrians = new SynchronizedSCIPedestrians();
  98. }
  99. public synchronized SCInterface getSCInterface() {
  100. return sCInterface;
  101. }
  102. public synchronized SCICars getSCICars() {
  103. return sCICars;
  104. }
  105. public synchronized SCIPedestrians getSCIPedestrians() {
  106. return sCIPedestrians;
  107. }
  108. /*================ TIME EVENT HANDLING ================
  109. /** An external timer instance is required. */
  110. protected ITimer externalTimer;
  111. /** Internally we use a timer proxy that queues time events together with other input events. */
  112. protected ITimer timerProxy = new ITimer() {
  113. /** Simply delegate to external timer with a modified callback. */
  114. @Override
  115. public void setTimer(ITimerCallback callback, int eventID, long time,
  116. boolean isPeriodic) {
  117. externalTimer.setTimer(SynchronizedTrafficLightCtrlStatemachine.this, eventID, time, isPeriodic);
  118. }
  119. @Override
  120. public void unsetTimer(ITimerCallback callback, int eventID) {
  121. externalTimer.unsetTimer(SynchronizedTrafficLightCtrlStatemachine.this, eventID);
  122. }
  123. };
  124. /**
  125. * Set the {@link ITimer} for the state machine. It must be set externally
  126. * on a timed state machine before a run cycle can be correct executed.
  127. *
  128. * @param timer
  129. */
  130. public void setTimer(ITimer timer) {
  131. synchronized(statemachine) {
  132. this.externalTimer = timer;
  133. /* the wrapped state machine uses timer proxy as timer */
  134. statemachine.setTimer(timerProxy);
  135. }
  136. }
  137. /**
  138. * Returns the currently used timer.
  139. *
  140. * @return {@link ITimer}
  141. */
  142. public ITimer getTimer() {
  143. return externalTimer;
  144. }
  145. public void timeElapsed(int eventID) {
  146. synchronized (statemachine) {
  147. statemachine.timeElapsed(eventID);
  148. }
  149. }
  150. /**
  151. * init() will be delegated thread-safely to the wrapped state machine.
  152. */
  153. public void init() {
  154. synchronized(statemachine) {
  155. statemachine.init();
  156. }
  157. }
  158. /**
  159. * enter() will be delegated thread-safely to the wrapped state machine.
  160. */
  161. public void enter() {
  162. synchronized(statemachine) {
  163. statemachine.enter();
  164. }
  165. }
  166. /**
  167. * exit() will be delegated thread-safely to the wrapped state machine.
  168. */
  169. public void exit() {
  170. synchronized(statemachine) {
  171. statemachine.exit();
  172. }
  173. }
  174. /**
  175. * isActive() will be delegated thread-safely to the wrapped state machine.
  176. */
  177. public boolean isActive() {
  178. synchronized(statemachine) {
  179. return statemachine.isActive();
  180. }
  181. }
  182. /**
  183. * isFinal() will be delegated thread-safely to the wrapped state machine.
  184. */
  185. public boolean isFinal() {
  186. synchronized(statemachine) {
  187. return statemachine.isFinal();
  188. }
  189. }
  190. /**
  191. * isStateActive() will be delegated thread-safely to the wrapped state machine.
  192. */
  193. public boolean isStateActive(State state) {
  194. synchronized(statemachine) {
  195. return statemachine.isStateActive(state);
  196. }
  197. }
  198. /**
  199. * runCycle() will be delegated thread-safely to the wrapped state machine.
  200. */
  201. @Override
  202. public void runCycle() {
  203. synchronized (statemachine) {
  204. statemachine.runCycle();
  205. }
  206. }
  207. }