Last active
May 27, 2016 18:31
-
-
Save tomliversidge/19c40e7903fb7efddaa4b402aec304cc to your computer and use it in GitHub Desktop.
example akka implementation of a tracker manager
This file contains 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 System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using Akka.Actor; | |
namespace AkkaTrackerManager | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
} | |
} | |
public class TrackerManager : ReceiveActor | |
{ | |
private IActorRef _gpsTracker; | |
public TrackerManager() | |
{ | |
Become(Unknown); | |
} | |
private void Unknown() | |
{ | |
Receive<GPSSignal>(msg => | |
{ | |
_gpsTracker.Tell(msg); | |
}); | |
Receive<EntryCandidate>(msg => | |
{ | |
if (AcceptCandidate(msg)) | |
{ | |
Sender.Tell(new CandidateAccepted()); | |
Become(Somewhere); // or some other state | |
} | |
else | |
{ | |
Sender.Tell(new CandidateRejected()); | |
} | |
}); | |
} | |
private void Somewhere() | |
{ | |
Receive<GPSSignal>(msg => | |
{ | |
_gpsTracker.Tell(msg); | |
}); | |
Receive<ExitCandidate>(msg => | |
{ | |
if (AcceptCandidate(msg)) | |
{ | |
Sender.Tell(new CandidateAccepted()); | |
Become(Unknown); // or some other state | |
} | |
else | |
{ | |
Sender.Tell(new CandidateRejected()); | |
} | |
}); | |
} | |
private bool AcceptCandidate(ExitCandidate msg) | |
{ | |
// some logic to determine whether or not to accept the candidate | |
return true; | |
} | |
private bool AcceptCandidate(EntryCandidate msg) | |
{ | |
// some logic to determine whether or not to accept the candidate | |
return true; | |
} | |
} | |
public class GPSTracker : ReceiveActor, IWithUnboundedStash | |
{ | |
public IStash Stash { get; set; } | |
public GPSTracker() | |
{ | |
Become(Unknown); | |
} | |
private void Unknown() | |
{ | |
Receive<GPSSignal>(msg => | |
{ | |
// pretend GPS enters somewhere | |
// Sender would be TrackerManager | |
Sender.Tell(new EntryCandidate()); | |
Become(AwaitingEntryDecision); | |
}); | |
} | |
private void Somewhere() | |
{ | |
Receive<GPSSignal>(msg => | |
{ | |
// pretend GPS exits somewhere | |
// Sender would be TrackerManager | |
Sender.Tell(new ExitCandidate()); | |
Become(AwaitingExitDecision); | |
}); | |
} | |
private void AwaitingEntryDecision() | |
{ | |
Receive<GPSSignal>(msg => | |
{ | |
Stash.Stash(); // store until we receive confirmation | |
}); | |
Receive<CandidateAccepted>(msg => | |
{ | |
Stash.UnstashAll(); | |
Become(Somewhere); | |
}); | |
Receive<CandidateRejected>(msg => | |
{ | |
Stash.UnstashAll(); | |
Become(Unknown); // rollback | |
}); | |
} | |
private void AwaitingExitDecision() | |
{ | |
Receive<GPSSignal>(msg => | |
{ | |
Stash.Stash(); // stash until we receive confirmation | |
}); | |
Receive<CandidateAccepted>(msg => | |
{ | |
Stash.UnstashAll(); | |
Become(Unknown); // or some other state | |
}); | |
Receive<CandidateRejected>(msg => | |
{ | |
Stash.UnstashAll(); | |
Become(Somewhere); // rollback | |
}); | |
} | |
} | |
internal class GPSSignal{} | |
internal class CandidateRejected{} | |
internal class CandidateAccepted{} | |
internal class EntryCandidate{} | |
internal class ExitCandidate { } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment