forked from petabridge/akka-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleReaderActor.cs
58 lines (49 loc) · 1.72 KB
/
ConsoleReaderActor.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
58
using System;
using Akka.Actor;
namespace WinTail
{
/// <summary>
/// Actor responsible for reading FROM the console.
/// Also responsible for calling <see cref="ActorSystem.Terminate"/>.
/// </summary>
class ConsoleReaderActor : UntypedActor
{
public const string StartCommand = "start";
public const string ExitCommand = "exit";
private readonly IActorRef _validationActor;
public ConsoleReaderActor(IActorRef validationActor)
{
_validationActor = validationActor;
}
protected override void OnReceive(object message)
{
if (message.Equals(StartCommand))
{
DoPrintInstructions();
}
GetAndValidateInput();
}
#region Internal methods
private void DoPrintInstructions()
{
Console.WriteLine("Please provide the URI of a log file on disk.\n");
}
/// <summary>
/// Reads input from console, validates it, then signals appropriate response
/// (continue processing, error, success, etc.).
/// </summary>
private void GetAndValidateInput()
{
var message = Console.ReadLine();
if (!string.IsNullOrEmpty(message) && String.Equals(message, ExitCommand, StringComparison.OrdinalIgnoreCase))
{
// if user typed ExitCommand, shut down the entire actor system (allows the process to exit)
Context.System.Terminate();
return;
}
// otherwise, just hand message off to validation actor (by telling its actor ref)
_validationActor.Tell(message);
}
#endregion
}
}