-
Notifications
You must be signed in to change notification settings - Fork 1
/
Actors.fs
153 lines (119 loc) · 4.6 KB
/
Actors.fs
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
module WinTail.Actors
open System
open System.IO
open Akkling
open Akka.Actor
module Helpers =
let (|IsNull|IsExit|IsContent|) (input: String) =
match input with
| null -> IsNull
| s when s.ToLower() = "exit" -> IsExit
| _ -> IsContent
let (|IsValidPath|IsInvalidPath|) (content: String) =
if File.Exists content then IsValidPath else IsInvalidPath
let readLine () =
Console.ReadLine()
let writeLine (text: String) =
Console.WriteLine(text)
let writeLineInColor (text: String) (color: ConsoleColor) =
Console.BackgroundColor <- color
Console.WriteLine(text)
Console.ResetColor()
module Messages =
type ConsoleWriterMessage =
| WriteInfo of String
| WriteError of String
type TailOperatorMessage =
| TailInit
| TailError of ex: exn
| TailChange
type TailCoordinatorMessage =
| StartTail of String * IActorRef<ConsoleWriterMessage>
type InputValidatorMessage =
| ValidateInput of String
type ConsoleReaderMessage =
| ReadInput
module Actors =
open Helpers
open Messages
let consoleWriter (context: Actor<_>) (message) =
match message with
| WriteInfo text ->
writeLineInColor text ConsoleColor.Green
| WriteError text ->
writeLineInColor text ConsoleColor.Red
writeLine String.Empty
ignored ()
let tailOperator (path: String) (consoleWriter: IActorRef<ConsoleWriterMessage>) (context: Actor<_>) =
let stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
let reader = new StreamReader(stream, Text.Encoding.UTF8)
let watcher = new FileSystemWatcher(
Path = Path.GetDirectoryName path,
Filter = Path.GetFileName path,
NotifyFilter = (NotifyFilters.FileName ||| NotifyFilters.LastWrite))
watcher.Error
|> Event.map (fun ev -> ev.GetException())
|> Event.add (fun ex -> context.Self <! TailError ex)
watcher.Changed
|> Event.add (fun ev -> context.Self <! TailChange)
context.Self <! TailInit
watcher.EnableRaisingEvents <- true
let rec loop () = actor {
let! message = context.Receive()
match message with
| TailInit ->
let initContent = reader.ReadToEnd()
consoleWriter <! WriteInfo(sprintf "INIT CONTENT:\n%s" initContent)
| TailError ex ->
let errorMessage = ex.Message
consoleWriter <! WriteError(sprintf "ERROR MESSAGE:\n%s" errorMessage)
| TailChange ->
let newContent = reader.ReadToEnd()
consoleWriter <! WriteInfo(sprintf "NEW CONTENT:\n%s" newContent)
| LifecycleEvent life ->
match life with
| PostStop ->
watcher.Dispose()
reader.Dispose()
stream.Dispose()
| _ ->
()
return! loop()
}
loop()
let tailCoordinator (context: Actor<_>) (message) =
match message with
| StartTail (file, writer) ->
let actor = tailOperator file writer
actor
|> props
|> spawn context "TailOperator"
|> ignore
ignored()
let inputValidator (consoleWriter: IActorRef<ConsoleWriterMessage>) (context: Actor<_>) (message) =
match message with
| ValidateInput input ->
match input with
| IsValidPath ->
consoleWriter <! WriteInfo(sprintf "'%s' is a valid file path" input)
let tailCoordinator = select context "/user/TailCoordinator"
tailCoordinator <! StartTail(input, consoleWriter)
| IsInvalidPath ->
consoleWriter <! WriteError(sprintf "'%s' is not a valid file path" input)
let consoleReader = context.Sender()
consoleReader <! ReadInput
ignored()
let consoleReader (context: Actor<_>) (message) =
match message with
| ReadInput ->
let input = readLine()
writeLine String.Empty
match input with
| IsNull ->
()
| IsExit ->
context.System.Terminate() |> ignore
| IsContent ->
let inputValidator = select context "/user/InputValidator"
inputValidator <! ValidateInput input
ignored ()