forked from petabridge/akka-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleReaderActor.cs
52 lines (44 loc) · 1.56 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
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";
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 send the message off for validation
Context.ActorSelection("akka://MyActorSystem/user/validationActor").Tell(message);
}
#endregion
}
}