Created
September 28, 2017 10:49
-
-
Save NMZivkovic/aade390ce4b27fa0b1ae9470e0e29a2b to your computer and use it in GitHub Desktop.
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
public class UserActor : ReceiveActor | |
{ | |
private Stopwatch _stopwatch; | |
private bool _isAlreadyReading; | |
public UserActor() | |
{ | |
_stopwatch = new Stopwatch(); | |
Receive<StartedReadingMessage>(message => ReceivedStartMessage(message)); | |
Receive<StopedReadingMessage>(message => ReceivedStopMessage(message)); | |
} | |
private void ReceivedStartMessage(StartedReadingMessage message) | |
{ | |
if (_isAlreadyReading) | |
throw new InvalidOperationException("User is already reading another article!"); | |
_stopwatch.Start(); | |
_isAlreadyReading = true; | |
} | |
private void ReceivedStopMessage(StopedReadingMessage message) | |
{ | |
if (!_isAlreadyReading) | |
throw new InvalidOperationException("User was not reading any article!"); | |
_stopwatch.Stop(); | |
_isAlreadyReading = false; | |
Context.ActorSelection("../../reporting").Tell(new ReportMessage(message.User, message.Article, _stopwatch.ElapsedMilliseconds)); | |
_stopwatch.Reset(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment