-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
258 lines (228 loc) · 6.24 KB
/
Copy pathindex.js
File metadata and controls
258 lines (228 loc) · 6.24 KB
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
'use strict';
const express = require('express');
const app = express();
const request = require('request');
const WebSocket = require('ws');
const CHICKENRAND_URL = process.env.CHICKENRAND_URL || 'http://localhost:7000';
const CONTROL_PASSWORD = process.env.CONTROL_PASSWORD || 'toto';
const RNG_URL = process.env.RNG_URL || 'localhost:8080';
const NB_TRIALS_PER_SECOND = 10; // We recieve the message each 100ms
const QUEUE_UPDATE_INTERVAL = 3000; // in ms
const TIME_BEFORE_STARTING_XP = 1000; // in ms
const RNG_CONTROL_PORT = process.env.PORT || 1337;
const RNG_ID = process.env.RNG_ID || 2; // TODO : use rng rest API
const XP_DURATION = 10; // in seconds
const XP_TRIALS = NB_TRIALS_PER_SECOND * XP_DURATION;
const COOKIE_JAR = request.jar();
let results;
let ws = null;
let queueId = null;
let xpId = 2;
let userId = null;
let totalBits = 0;
let trialsCount = 0;
let pendingControls = [];
let xpStarted = false;
function resetResults() {
results = {
date: Date.now(),
trials: [],
rng_control: true
};
}
function bitAt(byte, pos) {
return ((byte & (1 << pos)) !== 0);
}
function connectingRng() {
console.log('Connecting to RNG');
ws = new WebSocket(`ws://${RNG_URL}`);
ws.on('open', () => {
console.log('Connection with the RNG established');
resetResults();
trialsCount = 0;
xpStarted = true;
setTimeout(() => ws.send('start'), TIME_BEFORE_STARTING_XP);
});
ws.on('close', () => {
if (queueId !== null) {
console.log('Connection closed. Remove from queue.')
removeFromQueue()
.catch(err => console.error('Error when leaving the queue.', err))
.then(function() {
let pending;
// If there is some pending control then launch another control right away
if (pendingControls.length > 0) {
pending = pendingControls.pop();
xpId = pending.xpId;
userId = pending.userId;
addToQueue();
}
});
}
});
ws.on('message', data => {
if(!xpStarted) {
return;
}
const NB_BITS_PER_BYTE = 8;
const numbers = Array.from(new Uint8Array(data))
const trialRes = {
nbOnes: 0,
nbZeros: 0,
ms: Date.now() - results.date,
rawDataBase64: Buffer.from(data).toString("base64")
};
numbers.forEach(num => {
for (let pos = 0; pos < NB_BITS_PER_BYTE; pos++) {
if (bitAt(num, pos)) {
trialRes.nbOnes++;
} else {
trialRes.nbZeros++;
}
}
});
totalBits += trialRes.nbOnes + trialRes.nbZeros;
results.trials.push(trialRes);
trialsCount++;
if (trialsCount === XP_TRIALS) {
stopExperiment();
}
});
ws.on('error', (e) => {
console.log('RNG connection error. Message :', e.message);
stopExperiment(true);
})
}
function startExperiment() {
if (queueId === null) {
console.log('No queueId');
return;
}
request.post(`${CHICKENRAND_URL}/queue/start/${queueId}.json`, {jar: COOKIE_JAR}, (err, httpResponse, body) => {
if (err) {
console.error('Error when starting the experiment.');
console.error(err);
}
const resp = JSON.parse(body);
if (resp.message) {
console.error('problem', resp.message);
return;
}
connectingRng();
});
}
function removeFromQueue() {
return new Promise((resolve, reject) => {
request.post(`${CHICKENRAND_URL}/queue/remove/${queueId}.json`, {jar: COOKIE_JAR}, (err, httpResponse) => {
queueId = null;
if (err) {
reject(err);
}
resolve();
});
});
}
function sendResults() {
return new Promise((resolve, reject) => {
request.post(`${CHICKENRAND_URL}/xp/send_results/${xpId}`, {jar: COOKIE_JAR, form: {results: JSON.stringify(results), rng_id: RNG_ID, rng_control_user_id: userId}}, (err, httpResponse, body) => {
if (err || httpResponse.statusCode === 500) {
reject(err);
}
resolve(body)
});
});
}
function stopExperiment(err) {
if (ws) {
ws.close();
ws = null;
xpStarted = false;
}
// We may stop the expermiment with no result (ie rng connection error)
if(err) {
removeFromQueue()
.catch(err => console.error('Error when leaving the queue.', err));
} else {
sendResults()
.then(() => {
console.log('Results sended total bits recieved : ', totalBits);
totalBits = 0;
userId = null;
})
.catch(err => console.error('Error when sending experiment results.', err));
}
}
function update() {
request.post(`${CHICKENRAND_URL}/queue/update/${queueId}.json`, function (err, httpResponse, body) {
if (err) {
console.error('Error when updating the queue');
console.error(err);
}
const data = JSON.parse(body);
if (data.item_on_top === queueId) {
startExperiment();
} else {
setTimeout(update, QUEUE_UPDATE_INTERVAL);
}
});
}
function addToQueue() {
request.post(`${CHICKENRAND_URL}/queue/add/${xpId}.json`, {jar: COOKIE_JAR}, (err, httpResponse, body) => {
if (err) {
console.error('Error when entering into to the queue.');
console.error(err);
logIn(addToQueue);
}
const queue = JSON.parse(body);
if (queue.item) {
console.log('Added to queue #', queue.item.id);
queueId = queue.item.id;
if (queue.state.length === 1) {
console.log('Start experiment');
startExperiment();
} else {
console.log('Queue is not empty waiting in the queue', queue.state.length);
update();
}
} else {
console.error('Error when entering into to the queue.');
console.error(queue.message);
//TEMP need a better error handling
if(queue.message === 'Erreur : Vous devez être connecté.') {
logIn(addToQueue);
}
}
});
}
function createControlXp(req, res) {
xpId = req.body['xp_id'];
userId = req.body['user_id'];
if(xpId) {
// We may already be in the queue
if (queueId === null) {
addToQueue();
} else {
pendingControls.push({xpId, userId});
}
}
res.send('OK');
}
function logIn(callback) {
console.log('Log in to ChichenRand server as [email protected]');
request.post(`${CHICKENRAND_URL}/user/login`, {jar: COOKIE_JAR, form: {email: '[email protected]', password: CONTROL_PASSWORD}}, err => {
if (err) {
console.error('Cannot log into chickenrand, exiting.');
throw new Error(err);
}
console.log('Logged into chickenrand, waiting for control results to generate...');
if (callback) {
callback();
}
});
}
logIn();
app.use(express.json());
app.post('/rng-control', createControlXp);
app.listen(RNG_CONTROL_PORT, () => {
console.log(`Start server on port ${RNG_CONTROL_PORT}`);
});