forked from petabridge/akka-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConsoleReaderActor.cs
39 lines (33 loc) · 1.14 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
using System;
using Akka.Actor;
namespace WinTail
{
/// <summary>
/// Actor responsible for reading FROM the console.
/// Also responsible for calling <see cref="ActorSystem.Shutdown"/>.
/// </summary>
class ConsoleReaderActor : UntypedActor
{
public const string ExitCommand = "exit";
private IActorRef _consoleWriterActor;
public ConsoleReaderActor(IActorRef consoleWriterActor)
{
_consoleWriterActor = consoleWriterActor;
}
protected override void OnReceive(object message)
{
var read = Console.ReadLine();
if (!string.IsNullOrEmpty(read) && String.Equals(read, ExitCommand, StringComparison.OrdinalIgnoreCase))
{
// shut down the system (acquire handle to system via
// this actors context)
Context.System.Shutdown();
return;
}
// send input to the console writer to process and print
_consoleWriterActor.Tell(read);
// continue reading messages from the console
Self.Tell("continue");
}
}
}