-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
104 lines (80 loc) · 2.36 KB
/
player.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
require('dotenv').config()
const _ = require('lodash')
const https = require('https')
const fs = require('fs')
const WebSocket = require('ws')
const Player = require('./PlayerSMPC')
const { print, pack, unpack } = require('./helpers')
const PORT = process.env.PORT || 3003
if (_.isEmpty(process.env.ROOT_CA)) {
throw new Error('HTTPS root CA path must be defined!')
}
if (_.isEmpty(process.env.PEM_KEY)) {
throw new Error('HTTPS key path must be defined!')
}
if (_.isEmpty(process.env.PEM_CERT)) {
throw new Error('HTTPS cert path must be defined!')
}
if (_.isEmpty(process.env.SMPC_ENGINE)) {
throw new Error('SMPC Engine absolute path not defined!')
}
if (!process.env.ID) {
throw new Error('Player ID not defined!')
}
const server = https.createServer({
ca: fs.readFileSync(process.env.ROOT_CA, { encoding: 'utf-8' }),
cert: fs.readFileSync(process.env.PEM_CERT),
key: fs.readFileSync(process.env.PEM_KEY),
requestCert: true,
rejectUnauthorized: true,
port: PORT,
clientTracking: true
})
const wss = new WebSocket.Server({ server })
console.log(`Player ${process.env.ID} started on port ${PORT}.`)
const handleConnection = (ws) => {
const player = new Player(process.env.ID)
player.on('compilation-ended', (msg) => {
ws.send(pack({ message: 'compilation-ended', ...msg }))
})
player.on('listen', () => {
ws.send(pack({ message: 'listen', player: { id: player.id } }))
})
player.on('error', (msg) => {
ws.send(pack({ message: 'error', ...msg }))
})
player.on('exit', (msg) => {
ws.send(pack({ message: 'exit', entity: 'player', ...msg }))
})
ws.on('close', (data) => {
console.log('Connection Closed: ', data)
player.terminate()
player.removeAllListeners()
})
ws.on('error', (err) => {
console.log(err)
player.terminate()
player.removeAllListeners()
})
ws.on('message', (data) => {
print(`Message: ${data}`)
data = unpack(data)
if (data.message === 'job-info') {
player.setJob({ ...data.job })
}
if (data.message === 'compile') {
player.compileProgram({ ...data.dataInfo, clients: data.job.totalClients })
}
if (data.message === 'start') {
player.run()
}
if (data.message === 'restart') {
player.terminate()
}
})
}
wss.on('connection', (ws) => {
print('Connection Accepted!')
handleConnection(ws)
})
server.listen(process.env.PORT)