-
Notifications
You must be signed in to change notification settings - Fork 14
/
eval_server.js
37 lines (30 loc) · 928 Bytes
/
eval_server.js
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
var syscalls = require('syscalls')
var loop = require('./loop')
var fd = syscalls.socket(syscalls.AF_INET, syscalls.SOCK_STREAM, 0)
syscalls.fcntl(fd, syscalls.F_SETFL, syscalls.O_NONBLOCK)
syscalls.bind(fd, 3000, "0.0.0.0")
syscalls.listen(fd, 100)
loop.on(fd, 'read', function() {
console.log("New connection")
var connFd = syscalls.accept(fd)
loop.on(connFd, 'read', function() {
var code = syscalls.read(connFd, 10240)
loop.remove(connFd, 'read')
if (code.length == 0) {
syscalls.close(connFd)
return
}
if (syscalls.fork() == 0) {
// In the child process
console.log("Running in PID: " + syscalls.getpid())
var result = eval(code)
syscalls.write(connFd, JSON.stringify(result) + "\n")
console.log("Done PID: " + syscalls.getpid())
process.exit()
} else {
// In the master process
syscalls.close(connFd)
}
})
})
loop.run()