-
Notifications
You must be signed in to change notification settings - Fork 15
/
DaemonMode.jl
292 lines (227 loc) · 6.82 KB
/
DaemonMode.jl
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
module DaemonMode
export serve, runfile, runargs, runexpr, sendExitCode
using Sockets
const PORT = 3000
function add_packages(fname::AbstractString)
end
const first_time = Ref(true)
const token_runfile = "DaemonMode::runfile"
const token_runexpr = "DaemonMode::runexpr"
const token_exit = "DaemonMode::exit"
const token_end = "DaemonMode::end"
"""
serve(port=3000, shared=false)
Run the daemon, running all files and expressions sended by the client function.
# Optionals
- port: port to listen (default=3000).
- shared: Share the environment between calls. If it is false (default) each run
has its own environment, so the variables/functions are not shared.
- print_stack: Print the complete stack when there is an error. By default it is true.
"""
function serve(port=PORT, shared=missing; print_stack=true)
server = Sockets.listen(Sockets.localhost, port)
quit = false
current = pwd()
while !quit
sock = accept(server)
mode = readline(sock)
if mode == token_runfile
serverRunFile(sock, coalesce(shared, false), print_stack)
elseif mode == token_runexpr
serverRunExpr(sock, coalesce(shared, true), print_stack)
elseif mode == token_exit
println(sock, token_end)
sleep(1)
quit = true
else
println(sock, "Error, unrecognised mode, expected (\"runFile()\", \"runExpr()\" or \"exit()\", but received \"$mode\"")
quit = true
end
end
close(server)
end
function serverReplyError(sock, e)
try
showerror(sock, e)
println(sock)
println(sock, token_end)
catch e
if (e isa Base.IOError) && abs(e.code) == abs(Libc.EPIPE)
# client disconnected early, ignore
else
rethrow()
end
end
end
function serverReplyError(sock, e, bt)
try
showerror(sock, e, bt)
println(sock)
println(sock, token_end)
catch e
if (e isa Base.IOError) && abs(e.code) == abs(Libc.EPIPE)
# client disconnected early, ignore
else
rethrow()
end
end
end
function serverRun(run, sock, shared, print_stack)
redirect_stdout(sock) do
redirect_stderr(sock) do
try
if shared
run(Main)
else
m = Module()
add_include = Meta.parse("include(arg)=Base.include(@__MODULE__,arg)")
Base.eval(m, add_include)
run(m)
end
println(sock, token_end)
catch e
if print_stack
serverReplyError(sock, e, catch_backtrace())
else
serverReplyError(sock, e)
end
end
end
end
end
"""
serverRunFile(sock, shared)
Run the source code of the filename push through the socket.
# Parameters
- sock: socket in which is going to receive the dir, the filename, and args to run.
- shared: Share the environment between calls. If it is false (default) each run
has its own environment, so the variables/functions are not shared.
"""
function serverRunFile(sock, shared, print_stack)
try
dir = readline(sock)
fname = readline(sock)
args_str = readline(sock)
args = split(args_str, " ")
append!(ARGS, args)
first_time[] = true
cd(dir) do
content = read(fname, String)
serverRun(sock, shared, print_stack) do mod
include_string(mod, content)
end
end
catch e
serverReplyError(sock, e)
end
empty!(ARGS)
end
"""
serverRunExpr(sock, shared)
Run the source code of the filename push through the socket.
# Parameters
- sock: socket in which is going to receive the code to run.
- shared: Share the environment between calls. If it is false (default) each run
has its own environment, so the variables/functions are not shared.
"""
function serverRunExpr(sock, shared, print_stack)
try
dir = readline(sock)
expr = readuntil(sock, token_end) # Read until token_end to handle multi-line expressions
parsedExpr = Meta.parse(expr)
cd(dir) do
serverRun(sock, shared, print_stack) do mod
Base.eval(mod, parsedExpr)
end
end
catch e
serverReplyError(e)
end
empty!(ARGS)
end
"""
runexpr(expr::AbstractString)
Ask the server to run julia code in a string pass as parameters.
# Parameters
- expr: Julia code to run in the server.
# Optionals
- port: Port (default=3000).
- output: stream in which it is shown the output of the run.
"""
function runexpr(expr::AbstractString ; output = stdout, port = PORT)
try
sock = Sockets.connect(port)
println(sock, token_runexpr)
println(sock, pwd())
println(sock, expr)
println(sock, token_end)
line = readline(sock)
while (line != token_end)
println(output, line)
line = readline(sock)
end
catch e
println(stderr, "Error, cannot connect with server. Is it running?")
end
end
"""
runfile(fname::AbstractString)
Ask the server to run a specific filename.
# Parameters
- fname: Filename to run.
# Optionals
- args: List of arguments (array of String, default=[]).
- port: Port (default=3000)
- output: stream in which it is shown the output of the run.
"""
function runfile(fname::AbstractString; args=String[], port = PORT, output=stdout)
dir = dirname(fname)
if isempty(dir)
fcompletename = joinpath(dir, fname)
else
fcompletename = fname
end
try
sock = Sockets.connect(port)
println(sock, token_runfile)
println(sock, pwd())
println(sock, fcompletename)
println(sock, join(args, " "))
line = readline(sock)
token_size = length(token_end)
while (length(line) < token_size || !occursin(token_end, line))
println(output, line)
line = readline(sock)
end
if length(line) > token_size
end_line = replace(line, token_end => "")
print(output, end_line)
end
catch e
println(stderr, "Error, cannot connect with server. Is it running?")
end
return
end
"""
sendExitCode(port)
send the exit code, it closes the server.
# Optionals
- port: port to connect (default=3000).
"""
function sendExitCode(port=PORT)
sock = Sockets.connect(port)
println(sock, token_exit)
end
"""
runargs(port=PORT)
Ask the server to run all files in ARGS.
# Optionals
- port: Port of the server to ask (default=3000).
"""
function runargs(port=PORT)
if isempty(ARGS)
println(file=stderr, "Error: missing filename")
end
runfile(ARGS[1], args=ARGS[2:end], port=port)
end
end # module