-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
252 lines (215 loc) · 7.58 KB
/
app.js
File metadata and controls
252 lines (215 loc) · 7.58 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
var webSocket = require("websocket").w3cwebsocket;
var fs = require("fs");
var jpointer = require("json-pointer");
var https = require("https");
var http = require("http");
var pckg = require("./package.json");
// System config loading
var properties = require("./config/app-config.json");
var httpPort = properties.port.http;
var httpsPortP = properties.port.https;
var key = properties.path.key;
var cert = properties.path.cert;
// Services configuration file
var servicesConfig = require("./config/services.json");
// This function validates the JSON schemas
var Ajv = require("ajv");
const addFormats = require("ajv-formats");
validateJsonSchemas();
// Prepare the languages array to give to the clients
var languagesData = getLanguages();
var server = http.createServer();
var enableHTTPS = false;
if (key.length !== 0 && cert.length !== 0) {
enableHTTPS = true;
var options = {
key: fs.readFileSync(key),
cert: fs.readFileSync(cert),
};
var secureServer = https.createServer(options);
}
var io = require("socket.io")({
cors: {
origin: true,
},
});
io.attach(server);
if (enableHTTPS) {
io.attach(secureServer);
}
io.sockets.on("connection", (client) => {
// Wait for the incoming connection from the browser, the Socket.io client from index.html
print_log("Opened connection");
client.on("getLanguages", () => {
print_log('Executed "getLanguages"');
client.emit("languages", JSON.stringify(languagesData));
});
client.on("run", (data) => {
// Wait for the incoming data with the 'run' event and send data
print_log('Executed "run"');
// The function return the host path of one of the executors for a particular language and solver, if know
var host = getExcecutorURL(data);
// Check if the choosen host is configured
if (host === undefined) {
client.emit("problem", {
reason: "No Executor available for this solver!",
});
return;
}
// Connect to the executor
var executor = new webSocket(host);
print_log('Connecting to "' + host + '"');
executor.onopen = () => {
// Opens the connection and send data
print_log(
"Sending to EmbASPServerExecutor:\n" +
JSON.stringify(JSON.parse(data), null, "\t")
);
executor.send(data);
};
executor.onerror = (error) => {
print_log(
"WebSocket problem:\n" + JSON.stringify(error, null, "\t")
);
client.emit("problem", {
reason: "Execution error, please try again later!",
});
};
executor.onmessage = (output) => {
// Wait for the incoming data from the EmbASPServerExecutor
var model = JSON.parse(output.data);
print_log(
'From EmbASPServerExecutor:\nModel "' +
model.model +
'"\nError "' +
model.error +
'"'
); // debug string
client.emit("output", model); // Socket.io calls emit() to send data to the browser.
};
});
});
if (enableHTTPS) {
secureServer.listen(httpsPortP, function () {
print_log("Loide API Server listening on secure port " + httpsPortP);
print_log("Version: " + pckg.version);
});
}
server.listen(httpPort, function () {
print_log("Loide API Server listening on port " + httpPort);
print_log("Version: " + pckg.version);
});
function print_log(statement) {
console.log("%s: %s", new Date().toLocaleString(), statement); // debug string
}
function getExcecutorURL(data) {
console.log("data getExcecutorURL", data);
try {
data = JSON.parse(data);
} catch (error) {
return undefined;
}
for (var i in servicesConfig.languages) {
if (servicesConfig.languages[i].value === data.language) {
var solvers = servicesConfig.languages[i].solvers;
for (var j in solvers) {
// FIXME: The client should pass 'solver' parameter and not 'engine'
if (solvers[j].value === data.engine) {
var executors = solvers[j].executors;
for (var k in executors) {
if (executors[k].value === data.executor) {
return (
executors[k].protocol +
"://" +
executors[k].url +
":" +
executors[k].port +
executors[k].path
);
}
}
}
}
}
}
return undefined;
}
function validateJsonSchemas() {
// Validate JSON file with the relative scheme
var servicesValidation = validateSchema(
"./config/services.json",
"./config/services-schema.json"
);
var appConfigValidation = validateSchema(
"./config/app-config.json",
"./config/app-config-schema.json"
);
if (servicesValidation.criticalError || appConfigValidation.criticalError) {
console.log(
"Fatal error: configuration files are not setted up properly!"
);
process.exit(1);
}
}
function validateSchema(jsonPath, schemaPath) {
// Loading files
var json = require(jsonPath);
var schema = require(schemaPath);
// Config
var ajv = new Ajv({
allErrors: true,
});
addFormats(ajv);
// Compiling the schema
var compiledSchema = ajv.compile(schema);
var validated = false;
var printError = true;
var response = {};
while (!validated) {
// Validating
var validatedJson = compiledSchema(json);
// If some there is some error, the nearest parent object in the file, containing this error, is deleted
if (!validatedJson) {
// Prints the errors only the first time
if (printError) {
console.log(compiledSchema.errors);
printError = false;
}
for (var index in compiledSchema.errors) {
var path = compiledSchema.errors[index].dataPath;
if (path === "") {
// 'This' case happen when there is a problem in to the root of the json file (eg. when the file is empty)
console.log(
"Fatal error: " +
jsonPath +
" is not setted up properly!"
);
response.criticalError = true;
validated = true;
} else {
jpointer.remove(json, path);
}
}
} else {
console.log("Validated: " + jsonPath);
validated = true;
}
}
return response;
}
function getLanguages() {
// get a copy of languages from the services JSON
let languages = JSON.parse(JSON.stringify(servicesConfig.languages));
for (let i = 0; i < languages.length; i++) {
for (let j = 0; j < languages[i].solvers.length; j++) {
for (let x = 0; x < languages[i].solvers[j].executors.length; x++) {
let executor = languages[i].solvers[j].executors[x];
languages[i].solvers[j].executors[x] = {
name: executor.name,
value: executor.value,
};
}
}
}
return languages;
}