-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.js
156 lines (134 loc) · 5.4 KB
/
repl.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
152
153
154
155
156
var fs = require('fs');
var readLine = require('readline');
var statusMap = {}; // Keeps status and start/end times
// Help message
var help = function () {
console.log('commands:');
console.log('\tlist - List computations and status.');
console.log('\tshare <computation id> <filename> - shares the content of data/<filename>.csv with all parties in the computation.');
console.log('\tstart <computation id> - begins the computation and benchmarking of the computation.');
console.log('\tstatus <computation id> - displays the status of the given computation');
};
// reading from command line for interactive benchmarking
var scanf = readLine.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
module.exports = function (server_instance) {
// print status
var printStatus = function (computation_id) {
var status = statusMap[computation_id];
if (status == null) {
var total = server_instance.totalparty_map[computation_id];
var current = server_instance.client_map[computation_id];
current = current != null ? current.length : 0;
if (total === current && total != null) {
console.log(computation_id, ':', 'initialized');
} else {
console.log(computation_id, ':', 'waiting for', total - current);
}
} else {
var msg = '';
if (status.start_ts != null) {
var end_ts = status.end_ts != null ? status.end_ts : Date.now();
var elapsed = end_ts - status.start_ts;
elapsed = Math.floor(elapsed / 1000); // seconds
var minutes = Math.floor(elapsed / 60);
var seconds = elapsed % 60;
msg = minutes + '::' + seconds + ' (' + elapsed + ')';
}
console.log(computation_id, ':', status.status, msg);
}
};
// list all computations with status
var list = function () {
for (var computation_id in server_instance.totalparty_map) {
if (server_instance.totalparty_map.hasOwnProperty(computation_id)) {
printStatus(computation_id);
}
}
};
// read and share csv data
var share = function (computation_id, file) {
// Figure setting out
var tmp = computation_id.split('-');
var parties = parseInt(tmp[1]), replicas = parseInt(tmp[2]);
var cliques = [];
for (var i = 0; i < replicas; i++) {
var tmp = [];
for (var j = 0; j < parties; j++) {
tmp.push(1 + j * replicas + i);
}
cliques.push(tmp);
}
// Start sharing
var computation_instance = server_instance.compute(computation_id, { sodium: false, Zp: 251 });
computation_instance.connect();
var lineReader = readLine.createInterface({
input: fs.createReadStream('./data/'+file+'.csv')
});
var colCount = 0, rowCount = 0;
var processLine = function (line) {
if (line == null) return;
line = line.trim();
if (line === '') return;
line = line.split(',');
colCount = line.length;
rowCount++;
for (var i = 0; i < line.length; i++) {
for (var c = 0; c < cliques.length; c++) {
computation_instance.share(parseInt(line[i].trim()), null, cliques[c], ['s1']);
}
}
};
lineReader.on('line', processLine);
lineReader.on('close', function (line) {
processLine(line);
computation_instance.emit('share', null, JSON.stringify({ rows: rowCount, cols: colCount }), false);
console.log('done parsing');
statusMap[computation_id] = {
status: 'Parsed: ' + file
};
});
statusMap[computation_id] = {
status: 'Parsing: ' + file
};
};
// start the computation and monitor progress
var start = function (computation_id) {
var computation_instance = server_instance.compute(computation_id, { sodium: false, Zp: 251 });
computation_instance.emit('start', null, 'start', false);
statusMap[computation_id] = {
start_ts: Date.now(),
count: computation_instance.party_count,
status: 'Started'
};
computation_instance.listen('end', function () {
var status = statusMap[computation_id];
status.count--;
if (status.count === 0) {
status.end_ts = Date.now();
status.status = 'Finished';
printStatus(computation_id);
}
});
};
scanf.on('line', function (line) {
try {
line = line.trim();
if (line === '') return;
if (line === 'list') list();
else if (line.startsWith('status')) printStatus(line.substring('status'.length + 1).trim());
else if (line.startsWith('start')) start(line.substring('start'.length + 1).trim());
else if (line.startsWith('share')) {
line = line.substring('share'.length + 1).trim();
line = line.split(' ');
share(line[0].trim(), line[1].trim());
}
} catch(exception) {
console.log('Error!', exception);
}
});
help();
};