public void Publish <T>(T eventData) { Type handleType = typeof(T); Latches.RunWithLock(handleType, delegate { SetPublication(eventData); EventHandlers.Handle(eventData); }); }
public void FSMActor_must_unlock_the_lock() { var latches = new Latches(Sys); var timeout = 2.Seconds(); var lockFsm = Sys.ActorOf(Props.Create(() => new Lock("33221", 1.Seconds(), latches))); var transitionTester = Sys.ActorOf(Props.Create(() => new TransitionTester(latches))); lockFsm.Tell(new SubscribeTransitionCallBack(transitionTester)); latches.InitialStateLatch.Ready(timeout); lockFsm.Tell('3'); lockFsm.Tell('3'); lockFsm.Tell('2'); lockFsm.Tell('2'); lockFsm.Tell('1'); latches.UnlockedLatch.Ready(timeout); latches.TransitionLatch.Ready(timeout); latches.TransitionCallBackLatch.Ready(timeout); latches.LockedLatch.Ready(timeout); EventFilter.Warning("unhandled event").ExpectOne(() => { lockFsm.Tell("not_handled"); latches.UnhandledLatch.Ready(timeout); }); var answerLatch = new TestLatch(); var tester = Sys.ActorOf(Props.Create(() => new AnswerTester(answerLatch, lockFsm))); tester.Tell(Hello.Instance); answerLatch.Ready(timeout); tester.Tell(Bye.Instance); latches.TerminatedLatch.Ready(timeout); }
public TransitionTester(Latches latches) { _latches = latches; }
public Lock(string code, TimeSpan timeout, Latches latches) { var code1 = code; _latches = latches; StartWith(LockState.Locked, new CodeState("", code1)); When(LockState.Locked, evt => { if (evt.FsmEvent is char) { var codeState = evt.StateData; if (codeState.Code == code1) { DoUnlock(); return(GoTo(LockState.Open).Using(new CodeState("", codeState.Code)).ForMax(timeout)); } } else if (evt.FsmEvent.Equals("hello")) { return(Stay().Replying("world")); } else if (evt.FsmEvent.Equals("bey")) { return(Stop(Shutdown.Instance)); } return(null); }); When(LockState.Open, evt => { if (evt.FsmEvent is StateTimeout) { DoLock(); return(GoTo(LockState.Locked)); } return(null); }); WhenUnhandled(evt => { var msg = evt.FsmEvent; Log.Warning($"unhandled event {msg} in state {StateName} with data {StateData}"); latches.UnhandledLatch.Open(); return(Stay()); }); OnTransition((state, nextState) => { if (state == LockState.Locked && nextState == LockState.Open) { _latches.TransitionLatch.Open(); } }); OnTermination(evt => { if (evt.Reason == Shutdown.Instance && evt.TerminatedState == LockState.Locked) { // stop is called from lockstate with shutdown as reason... latches.TerminatedLatch.Open(); } }); Initialize(); }