-
Notifications
You must be signed in to change notification settings - Fork 13
/
slave.js
182 lines (162 loc) · 5.11 KB
/
slave.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
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
(function() {
var net = require('net'),
fs = require('fs'),
rimraf = require('rimraf'),
db = require(config.dbHandler),
reportError = static.reportError;
var counter = 0,
name;
var createServer = function(serverName) {
name = serverName;
// remove old stage directory
rimraf('stage', function(err) {
if (!err) {
fs.mkdirSync('stage');
// create the slave compilation server
var slaveServer = net.createServer(slaveHandler);
slaveServer.listen(config.slavePort, 'localhost');
console.log('Slave server is up.');
}
});
};
var slaveHandler = function(socket) {
var data = '';
socket.on('data', function(buf) {
data += buf;
});
socket.on('end', function() {
var obj = db.parse(data);
createWorkspace(obj, counter);
counter++;
});
};
var createScratch = function(counter) {
return static.mkdir('stage/' + counter);
};
var writeProgram = function(filename, code, counter) {
return function() {
return static.writeFile('stage/' + counter + '/' + filename, code);
};
};
var writeInput = function(input, counter) {
return function() {
return static.writeFile('stage/' + counter + '/in.txt', input);
};
};
var writeOutput = function(output, counter) {
return function() {
return static.writeFile('stage/' + counter + '/out.txt', output);
};
};
var writeCompileScript = function(language, filename, counter) {
var langHandler = require(config.languages[language]);
var compileScript = langHandler.getCompileScript(filename, counter);
return function() {
return static.writeFile('stage/' + counter + '/compile.sh', compileScript);
};
};
var execCompileScript = function(counter) {
return function() {
return static.exec('sh stage/' + counter + '/compile.sh');
};
};
var readCompile = function(counter) {
return function() {
return static.readFile('stage/' + counter + '/err.txt');
};
};
var checkCompile = function(id) {
return function(data) {
if ((data + "").trim() == "")
return true;
db.reportCompileFail(id, data + "");
throw new Error("Compile Error");
};
};
var writeRunScript = function(language, filename, time, counter) {
var langHandler = require(config.languages[language]);
var runScript = langHandler.getRunScript(filename, time, counter);
return function() {
return static.writeFile('stage/' + counter + '/run.sh', runScript);
};
};
var execRunScript = function(counter) {
return function() {
return static.exec('sh stage/' + counter + '/run.sh');
};
};
var readRunErrors = function(counter) {
return function() {
return static.readFile('stage/' + counter + '/err.txt');
};
};
var checkRunErrors = function(id) {
return function(data) {
data = (data + "").trim();
if (data === "")
return true;
var tle = "CPU time limit exceeded";
if (data.slice(0, tle.length) === tle)
db.reportTLE(id);
else
db.reportRunFail(id, data + "");
throw new Error("Runtime Error");
};
};
var readRunOutput = function(counter) {
return function() {
return static.readFile('stage/' + counter + '/tmp.txt');
};
};
var checkRunOutput = function(id, output, matchLines, partial) {
return function(data) {
var result;
if (!matchLines) {
if ((data + "").trim() === output.trim())
result = 1;
else
result = 0;
} else {
var outputLines = output.trim().split("\n");
var dataLines = (data + "").trim().split("\n");
var count = 0;
for (var i = 0; i < outputLines.length; i++) {
if (outputLines[i].trim() !== dataLines[i].trim()) {
if (!partial)
result = 0;
} else
count++;
}
result = count / outputLines.length;
}
return db.reportResult(id, result + "");
};
};
var reportDone = function() {
var client = net.connect({ host: config.masterIP, port: config.masterPort },
function() {
client.end(name);
});
};
var createWorkspace = function(obj, count) {
// This is a perfect use case for promise.
// Made the code sane enough to be read by humans.
(function(i) {
createScratch(i)
.then(writeProgram(obj.filename, obj.code, i), reportError)
.then(writeInput(obj.input, i), reportError)
.then(writeCompileScript(obj.language, obj.filename, i), reportError)
.then(execCompileScript(i), reportError)
.then(readCompile(i), reportError)
.then(checkCompile(obj.id), reportError)
.then(writeRunScript(obj.language, obj.filename, obj.time, i), reportError)
.then(execRunScript(i), reportError)
.then(readRunErrors(i), reportError)
.then(checkRunErrors(obj.id), reportError)
.then(readRunOutput(i), reportError)
.then(checkRunOutput(obj.id, obj.output, obj.matchLines, obj.partial), reportError)
.then(reportDone);
})(count);
};
module.exports.createServer = createServer;
})();