SynchronizedTrafficLightCtrlStatemachine.java 5.2 KB

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