Created
February 20, 2015 08:55
-
-
Save madhon/162655397a6459e9c942 to your computer and use it in GitHub Desktop.
Phone State Machine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using Stateless; | |
| using System.Diagnostics; | |
| namespace WFA2 | |
| { | |
| public enum Trigger | |
| { | |
| CallDialed, | |
| HungUp, | |
| CallConnected, | |
| LeftMessage, | |
| PlacedOnHold, | |
| TakenOffHold, | |
| PhoneHurledAgainstWall, | |
| } | |
| public enum State | |
| { | |
| OffHook, | |
| Ringing, | |
| Connected, | |
| OnHold, | |
| PhoneDestroyed | |
| } | |
| public class PhoneStateless | |
| { | |
| private readonly StateMachine<State, Trigger> _phone = new StateMachine<State, Trigger>(State.OffHook); | |
| private readonly Stopwatch _watch = new Stopwatch(); | |
| public PhoneStateless() | |
| { | |
| _phone.Configure(State.OffHook) | |
| .Permit(Trigger.CallDialed, State.Ringing); | |
| _phone.Configure(State.Ringing) | |
| .Permit(Trigger.HungUp, State.OffHook) | |
| .Permit(Trigger.CallConnected, State.Connected); | |
| _phone.Configure(State.Connected) | |
| .OnEntry(StartCallTimer) | |
| .OnExit(StopCallTimer) | |
| .Permit(Trigger.LeftMessage, State.OffHook) | |
| .Permit(Trigger.HungUp, State.OffHook) | |
| .Permit(Trigger.PlacedOnHold, State.OnHold); | |
| _phone.Configure(State.OnHold) | |
| .SubstateOf(State.Connected) | |
| .Permit(Trigger.TakenOffHold, State.Connected) | |
| .Permit(Trigger.HungUp, State.OffHook) | |
| .Permit(Trigger.PhoneHurledAgainstWall, State.PhoneDestroyed); | |
| } | |
| public void StartCallTimer() | |
| { | |
| _watch.Start(); | |
| } | |
| public void StopCallTimer() | |
| { | |
| _watch.Stop(); | |
| } | |
| public void CallDialed() | |
| { | |
| _phone.Fire(Trigger.CallDialed); | |
| } | |
| public void CallConnected() | |
| { | |
| _phone.Fire(Trigger.CallConnected); | |
| } | |
| public void PlacedOnHold() | |
| { | |
| _phone.Fire(Trigger.PlacedOnHold); | |
| } | |
| public void TakenOffHold() | |
| { | |
| _phone.Fire(Trigger.TakenOffHold); | |
| } | |
| public void HungUp() | |
| { | |
| _phone.Fire(Trigger.HungUp); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment