123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- package traffic.light.trafficlightctrl;
- import java.util.List;
- import traffic.light.ITimer;
- import traffic.light.ITimerCallback;
- import traffic.light.trafficlightctrl.TrafficLightCtrlStatemachine.State;
- /**
- * Runnable wrapper of TrafficLightCtrlStatemachine. This wrapper provides a thread-safe
- * instance of the state machine.
- *
- * Please report bugs and issues...
- */
- public class SynchronizedTrafficLightCtrlStatemachine implements ITrafficLightCtrlStatemachine {
-
- /**
- * The core state machine is simply wrapped and the event processing will be
- * delegated to that state machine instance. This instance will be created
- * implicitly.
- */
- protected TrafficLightCtrlStatemachine statemachine = new TrafficLightCtrlStatemachine();
-
- /**
- * Interface object for SCInterface
- */
- protected class SynchronizedSCInterface implements SCInterface {
-
- public void raisePolice_interrupt() {
-
- synchronized (statemachine) {
- statemachine.getSCInterface().raisePolice_interrupt();
- statemachine.runCycle();
- }
- }
-
- public void raiseToggle() {
-
- synchronized (statemachine) {
- statemachine.getSCInterface().raiseToggle();
- statemachine.runCycle();
- }
- }
-
- };
-
- protected SCInterface sCInterface;
-
- /**
- * Interface object for SCITrafficLight
- */
- protected class SynchronizedSCITrafficLight implements SCITrafficLight {
-
- public List<SCITrafficLightListener> getListeners() {
- synchronized(statemachine) {
- return statemachine.getSCITrafficLight().getListeners();
- }
- }
-
- public boolean isRaisedDisplayRed() {
- synchronized(statemachine) {
- return statemachine.getSCITrafficLight().isRaisedDisplayRed();
- }
- }
-
- public boolean isRaisedDisplayGreen() {
- synchronized(statemachine) {
- return statemachine.getSCITrafficLight().isRaisedDisplayGreen();
- }
- }
-
- public boolean isRaisedDisplayYellow() {
- synchronized(statemachine) {
- return statemachine.getSCITrafficLight().isRaisedDisplayYellow();
- }
- }
-
- public boolean isRaisedDisplayNone() {
- synchronized(statemachine) {
- return statemachine.getSCITrafficLight().isRaisedDisplayNone();
- }
- }
-
- };
-
- protected SCITrafficLight sCITrafficLight;
-
- /**
- * Interface object for SCITimer
- */
- protected class SynchronizedSCITimer implements SCITimer {
-
- public List<SCITimerListener> getListeners() {
- synchronized(statemachine) {
- return statemachine.getSCITimer().getListeners();
- }
- }
-
- public boolean isRaisedUpdateTimerColour() {
- synchronized(statemachine) {
- return statemachine.getSCITimer().isRaisedUpdateTimerColour();
- }
- }
-
- public String getUpdateTimerColourValue() {
- synchronized(statemachine) {
- return statemachine.getSCITimer().getUpdateTimerColourValue();
- }
- }
- public boolean isRaisedUpdateTimerValue() {
- synchronized(statemachine) {
- return statemachine.getSCITimer().isRaisedUpdateTimerValue();
- }
- }
-
- public long getUpdateTimerValueValue() {
- synchronized(statemachine) {
- return statemachine.getSCITimer().getUpdateTimerValueValue();
- }
- }
- };
-
- protected SCITimer sCITimer;
-
- public SynchronizedTrafficLightCtrlStatemachine() {
- sCInterface = new SynchronizedSCInterface();
- sCITrafficLight = new SynchronizedSCITrafficLight();
- sCITimer = new SynchronizedSCITimer();
- }
-
- public synchronized SCInterface getSCInterface() {
- return sCInterface;
- }
- public synchronized SCITrafficLight getSCITrafficLight() {
- return sCITrafficLight;
- }
- public synchronized SCITimer getSCITimer() {
- return sCITimer;
- }
- /*================ TIME EVENT HANDLING ================
-
- /** An external timer instance is required. */
- protected ITimer externalTimer;
-
- /** Internally we use a timer proxy that queues time events together with other input events. */
- protected ITimer timerProxy = new ITimer() {
- /** Simply delegate to external timer with a modified callback. */
- @Override
- public void setTimer(ITimerCallback callback, int eventID, long time,
- boolean isPeriodic) {
- externalTimer.setTimer(SynchronizedTrafficLightCtrlStatemachine.this, eventID, time, isPeriodic);
- }
-
- @Override
- public void unsetTimer(ITimerCallback callback, int eventID) {
- externalTimer.unsetTimer(SynchronizedTrafficLightCtrlStatemachine.this, eventID);
- }
- };
-
- /**
- * Set the {@link ITimer} for the state machine. It must be set externally
- * on a timed state machine before a run cycle can be correct executed.
- *
- * @param timer
- */
- public void setTimer(ITimer timer) {
- synchronized(statemachine) {
- this.externalTimer = timer;
- /* the wrapped state machine uses timer proxy as timer */
- statemachine.setTimer(timerProxy);
- }
- }
-
- /**
- * Returns the currently used timer.
- *
- * @return {@link ITimer}
- */
- public ITimer getTimer() {
- return externalTimer;
- }
-
- public void timeElapsed(int eventID) {
- synchronized (statemachine) {
- statemachine.timeElapsed(eventID);
- }
- }
-
- /**
- * init() will be delegated thread-safely to the wrapped state machine.
- */
- public void init() {
- synchronized(statemachine) {
- statemachine.init();
- }
- }
-
- /**
- * enter() will be delegated thread-safely to the wrapped state machine.
- */
- public void enter() {
- synchronized(statemachine) {
- statemachine.enter();
- }
- }
-
- /**
- * exit() will be delegated thread-safely to the wrapped state machine.
- */
- public void exit() {
- synchronized(statemachine) {
- statemachine.exit();
- }
- }
-
- /**
- * isActive() will be delegated thread-safely to the wrapped state machine.
- */
- public boolean isActive() {
- synchronized(statemachine) {
- return statemachine.isActive();
- }
- }
-
- /**
- * isFinal() will be delegated thread-safely to the wrapped state machine.
- */
- public boolean isFinal() {
- synchronized(statemachine) {
- return statemachine.isFinal();
- }
- }
-
- /**
- * isStateActive() will be delegated thread-safely to the wrapped state machine.
- */
- public boolean isStateActive(State state) {
- synchronized(statemachine) {
- return statemachine.isStateActive(state);
- }
- }
-
- /**
- * runCycle() will be delegated thread-safely to the wrapped state machine.
- */
- @Override
- public void runCycle() {
- synchronized (statemachine) {
- statemachine.runCycle();
- }
- }
- }
|