-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayerSMPC.js
151 lines (123 loc) · 4.2 KB
/
PlayerSMPC.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
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
const { spawn } = require('child_process')
const EventEmitter = require('events')
const fs = require('fs')
const util = require('util')
const _ = require('lodash')
const kill = require('tree-kill')
const algorithms = require('./smpc-global/algorithms.json')
const { includeError } = require('./helpers')
const readFile = util.promisify(fs.readFile)
const mkdir = util.promisify(fs.mkdir)
const writeFile = util.promisify(fs.writeFile)
const SCALE = process.env.SMPC_ENGINE
const PLAYER_CMD = process.env.NODE_ENV === 'development' ? './fake_scale.sh' : `${SCALE}/Player.x`
const COMPILE_CMD = `${SCALE}/compile.py`
const PROGRAMS_PATH = `${SCALE}/Programs/dynamic`
const FHE_FACTORIES = process.env.FHE_FACTORIES || 2
class Player extends EventEmitter {
constructor (id) {
super()
this.player = null
this.compile = null
this.id = id
this.errors = []
this.job = null
this.output = ''
}
setJob (job) {
this.job = job
}
async _compile (dataInfo) {
const algorithm = algorithms.find(a => a.name === this.job.algorithm)
if (!algorithm || !algorithm.template) {
throw new Error('Unsupported algorithm')
}
await mkdir(`${PROGRAMS_PATH}/${this.job.id}`, { recursive: true })
let program = await readFile(`./templates/${algorithm.template}.mpc`, 'utf8')
let compiled = _.template(program)
program = compiled({ ...dataInfo })
await writeFile(`${PROGRAMS_PATH}/${this.job.id}/${this.job.id}.mpc`, program)
}
async compileProgram (dataInfo) {
try {
await this._compile(dataInfo)
} catch (e) {
if (e.code !== 'EEXIST') {
console.log(e)
this.emit('error', { id: this.id, errors: [e.message] })
return
}
}
this.compile = spawn(COMPILE_CMD, [`${PROGRAMS_PATH}/${this.job.id}`], { cwd: SCALE, shell: true, detached: true })
this.compile.stdout.on('data', (data) => {})
this.compile.stderr.on('data', (data) => {
data = data.toString().toLowerCase()
console.log(data)
this.errors.push(data)
this.emit('error', { id: this.id, errors: this.errors })
})
this.compile.on('exit', (code) => {
console.log(`Compilation exited with code ${code}`)
this.emit('compilation-ended', { id: this.id, code, errors: this.errors })
this.terminateCompilation()
})
}
run () {
const totalClients = this.job.totalClients || 0
const args = process.env.NODE_ENV === 'development' ? [`-a ${this.job.algorithm}`] : [this.id, `${PROGRAMS_PATH}/${this.job.id}`, `-f ${FHE_FACTORIES}`, `-clients ${totalClients}`]
const cwd = process.env.NODE_ENV === 'development' ? __dirname : SCALE
this.player = spawn(PLAYER_CMD, [...args], { cwd, shell: true, detached: true })
this.player.stdout.on('data', (data) => {
data = data.toString()
console.log(data)
if (data.includes('@')) { // @ mean the server is ready and listening for client connections
this.emit('listen', { id: this.id })
}
if (data.includes('#')) { // # means output start
this.bufferOutput = true
}
if (this.bufferOutput) {
this.output += data
}
if (data.includes('$')) { // $ means output end
this.bufferOutput = false
this.output += data
}
})
this.player.stderr.on('data', (data) => {
console.log(data.toString())
data = data.toString().toLowerCase()
if (includeError(data, ['what()', 'aborted'])) {
this.errors.push(data)
this.emit('error', { id: this.id, errors: this.errors })
}
})
this.player.on('exit', (code) => {
console.log(`Player exited with code ${code}`)
this.emit('exit', { id: this.id, code, errors: this.errors, output: this.output })
this.terminate()
})
}
terminatePlayer () {
this._terminate(this.player)
}
terminateCompilation () {
this._terminate(this.compile)
}
_terminate (process) {
if (process) {
process.removeAllListeners()
process.stdin.pause()
kill(process.pid, 'SIGKILL')
// process.kill('SIGKILL')
process = null
}
}
terminate () {
this.terminateCompilation()
this.terminatePlayer()
this.job = null
this.output = ''
}
}
module.exports = Player