forked from petabridge/akka-bootcamp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileValidatorActor.cs
66 lines (56 loc) · 2.26 KB
/
FileValidatorActor.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
59
60
61
62
63
64
65
66
using System.IO;
using Akka.Actor;
namespace WinTail
{
/// <summary>
/// Actor that validates user input and signals result to others.
/// </summary>
public class FileValidatorActor : UntypedActor
{
private readonly IActorRef _consoleWriterActor;
private readonly IActorRef _tailCoordinatorActor;
public FileValidatorActor(IActorRef consoleWriterActor, IActorRef tailCoordinatorActor)
{
_consoleWriterActor = consoleWriterActor;
_tailCoordinatorActor = tailCoordinatorActor;
}
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("Input was blank. Please try again.\n"));
// tell sender to continue doing its thing (whatever that may be, this actor doesn't care)
Sender.Tell(new Messages.ContinueProcessing());
}
else
{
var valid = IsFileUri(msg);
if (valid)
{
// signal successful input
_consoleWriterActor.Tell(new Messages.InputSuccess(string.Format("Starting processing for {0}", msg)));
// start coordinator
_tailCoordinatorActor.Tell(new TailCoordinatorActor.StartTail(msg, _consoleWriterActor));
}
else
{
// signal that input was bad
_consoleWriterActor.Tell(new Messages.ValidationError(string.Format("{0} is not an existing URI on disk.", msg)));
// tell sender to continue doing its thing (whatever that may be, this actor doesn't care)
Sender.Tell(new Messages.ContinueProcessing());
}
}
}
/// <summary>
/// Checks if file exists at path provided by user.
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
private static bool IsFileUri(string path)
{
return File.Exists(path);
}
}
}