forked from petabridge/akka-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidationActor.cs
57 lines (51 loc) · 1.81 KB
/
ValidationActor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using Akka.Actor;
namespace WinTail
{
/// <summary>
/// Actor that validates user input and signals result to others.
/// </summary>
public class ValidationActor : UntypedActor
{
private readonly IActorRef _consoleWriterActor;
public ValidationActor(IActorRef consoleWriterActor)
{
_consoleWriterActor = consoleWriterActor;
}
protected override void OnReceive(object message)
{
var msg = message as string;
if (string.IsNullOrEmpty(msg))
{
// signal that the user needs to supply an input
_consoleWriterActor.Tell(new Messages.NullInputError("No input received."));
}
else
{
var valid = IsValid(msg);
if (valid)
{
// send success to console writer
_consoleWriterActor.Tell(new Messages.InputSuccess("Thank you! Message was valid."));
}
else
{
// signal that input was bad
_consoleWriterActor.Tell(new Messages.ValidationError("Invalid: input had odd number of characters."));
}
}
// tell sender to continue doing its thing (whatever that may be, this actor doesn't care)
Sender.Tell(new Messages.ContinueProcessing());
}
/// <summary>
/// Determines if the message received is valid.
/// Currently, arbitrarily checks if number of chars in message received is even.
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
private static bool IsValid(string msg)
{
var valid = msg.Length % 2 == 0;
return valid;
}
}
}