-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathweb.js
342 lines (295 loc) · 10 KB
/
web.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*************************/
/** **/
/** Added for solver.PD **/
/** **/
/*************************/
var express = require("express")
, bodyParser = require("body-parser")
, cookieParser = require("cookie-parser")
, morgan = require("morgan")
, cp = require('child_process')
// , memwatch = require('memwatch-next')
, http = require('http')
, fs = require('fs')
, request = require('request')
, app = express()
, port = Number(process.env.PORT || 5000);
/*************************/
/** **/
/** Added for solver.PD **/
/** **/
/*************************/
var dbConfig = require('./config/database.js');
var knex = require('knex')(dbConfig);
var bookshelf = require('bookshelf')(knex);
app.set('bookshelf', bookshelf);
app.set('lastdomain', 'None');
app.set('lastproblem', 'None');
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.status(200).end();
}
else {
next();
}
});
app.use(express.static(__dirname + '/client'));
app.use(bodyParser.json({limit: '5mb'}));
app.use(bodyParser.urlencoded({ extended: true, limit: '5mb' }));
app.use(cookieParser('I am a banana!'));
// Data structures and functions for throttling
app.last_requests = {};
app.current_caller = "";
app.get_ip = function(req) {
return req.headers['x-forwarded-for'] ||
req.connection.remoteAddress ||
req.socket.remoteAddress ||
req.connection.socket.remoteAddress;
};
app.server_use = function(req) {
// Extract the IP as a marker of who is using the service
var ip = app.get_ip(req);
// Mark the current caller
app.current_caller = ip;
};
app.check_for_throttle = function(req) {
// In rare cases (e.g., with a /solve GET request that's malformed), the server
// can go into a weird state where the lock isn't released. This fixes that.
// It may cause the server to become overloaded if the process is truely still
// going, but that's better than a permanent app lock.
if ((app.current_caller in app.last_requests) &&
((Date.now() - app.last_requests[app.current_caller]) >= 60000))
app.release_lock();
// Extract the IP as a marker of who is using the service
var ip = app.get_ip(req);
// Only throttle if it has been less than 20 seconds since the last contentious
// server busy call.
return (ip in app.last_requests) && ((Date.now() - app.last_requests[ip]) < 20000);
};
app.server_in_contention = function() {
// Mark the currently running source as having a last request for throttling
app.last_requests[app.current_caller] = Date.now();
};
// Keep around memwatch and cp for debugging purposes
// app.memwatch = memwatch;
app.cp = cp;
app.lock = false;
app.get_lock = function() {
if (app.lock)
return false;
app.lock = true;
return true;
};
app.release_lock = function() { app.lock = false; };
var download = function(url, dest, cb) {
var file = fs.createWriteStream(dest);
http.get(url,
function _handleResponse(response) {
response.pipe(file);
file.on('finish', function() {
file.close(cb);
});
});
};
app.storeFile = function(content, path, whendone) {
fs.writeFile(path, content.split('\\n').join('\n').split('\\t').join('\t'),
function _fileWritten() {
whendone(null, {'path': path});
});
};
// The magic sauce
app.fetchDomains = function(domainLoc, problemLoc, path, whendone) {
var domainPath = path + '/domain.pddl';
var problemPath = path + '/problem.pddl';
download(domainLoc, domainPath,
function _domDownloaded() {
download(problemLoc, problemPath,
function _probDownloaded() {
whendone(null, {'domainPath': domainPath, 'problemPath': problemPath});
});
});
};
app.readDomains = function(domdata, probdata, path, whendone) {
var domainPath = path + '/domain.pddl';
var problemPath = path + '/problem.pddl';
app.storeFile(domdata, domainPath,
function _domStored(domErr, domRes) {
if (domErr) {
whendone(domErr, null);
} else {
app.storeFile(probdata, problemPath,
function _probStored(probErr, probRes) {
if (probErr) {
whendone(probErr, null);
} else {
whendone(null, {'domainPath': domainPath, 'problemPath': problemPath});
}
});
}
});
};
app.fetchDomainsByID = function(problemID, path, whendone) {
request({
url: 'http://api.planning.domains/json/classical/problem/' + problemID,
json: true
},
function _handleResponse(error, response, body) {
if (error) {
whendone(error, null);
} else if (response.statusCode != 200) {
whendone(body, null);
} else {
app.fetchDomains(body.result.domain_url, body.result.problem_url, path, whendone);
}
});
};
app.getDomains = function(problemID, problem, domain, is_url, path, whendone) {
if (typeof problemID != 'undefined') {
app.fetchDomainsByID(problemID, path, whendone);
} else if ((typeof problem != 'undefined') && (typeof domain != 'undefined')) {
if(typeof is_url === 'undefined' || is_url === false) {
app.readDomains(domain, problem, path, whendone);
} else {
app.fetchDomains(domain, problem, path, whendone);
}
} else {
whendone("Must define either domain and problem or probID.", null);
}
};
app.solve = function(domainPath, problemPath, cwd, whendone) {
var planPath = cwd + '/plan';
var logPath = cwd + '/log';
var addPathsAndRespond = function(error, result) {
if (result) {
result['planPath'] = planPath;
result['logPath'] = logPath;
}
whendone(error, result);
};
var t = new Date().getTime();
fs.readFile(domainPath, 'utf8', function (err,data) {
if (err)
console.log(err);
else
app.lastdomain = data;
});
fs.readFile(problemPath, 'utf8', function (err,data) {
if (err)
console.log(err);
else
app.lastproblem = data;
});
cp.exec(__dirname + '/plan ' + domainPath + ' ' + problemPath + ' ' + planPath
+ ' > ' + logPath + ' 2>&1; '
+ 'if [ -f ' + planPath + ' ]; then echo; echo Plan:; cat ' + planPath + '; fi',
{ cwd: cwd },
function _processStopped(error, stdout, stderr) {
t = ((new Date().getTime()) - t) / 1000;
var bookshelf = app.get('bookshelf');
var Entry = bookshelf.Model.extend({tableName: 'Calls'});
Entry.forge({type:'solve', time: t, extra: '', date: new Date()}).save().then(console.log('Solving saved...'));
if (error)
whendone(error, null);
else
app.parsePlan(domainPath, problemPath, planPath, logPath, cwd, addPathsAndRespond);
});
};
app.parsePlan = function(domainPath, problemPath, planPath, logPath, cwd, whendone) {
cp.exec('timeout 5 python ' + __dirname + '/process_solution.py '
+ domainPath + ' ' + problemPath + ' ' + planPath + ' ' + logPath,
{ cwd: cwd },
function _processStopped(error, stdout, stderr) {
if (error)
whendone(error, null);
try {
var result = JSON.parse(stdout);
} catch (error) {
console.log("Error parsing output:");
console.log("<error>");
console.log(error);
console.log("</error>");
console.log("<stdout>");
console.log(stdout);
console.log("</stdout>");
console.log("<stderr>");
console.log(stderr);
console.log("</stderr>");
whendone("Error parsing the json output (if problem persists, please email solver admin).", null);
return;
}
if (result.parse_status === 'err')
whendone(result, null);
else
whendone(null, result);
});
};
app.validate = function(domainPath, problemPath, planPath, cwd, whendone) {
cp.exec(__dirname + '/validate ' + domainPath + ' ' + problemPath + ' ' + planPath,
{ cwd: cwd },
function _processStopped(error, stdout, stderr) {
if (error) {
app.failValidate(domainPath, problemPath, planPath, cwd, whendone);
} else if (stderr) {
whendone({
'val_status': 'err',
'error':'Validator wrote to stderr but did not report an error.',
'val_stdout': stdout,
'val_stderr': stderr
}, null);
} else if (isNaN(stdout.trim())) {
whendone({
'val_status': 'err',
'error':'Validator output does not look as expected.',
'val_stdout': stdout,
'val_stderr': stderr
}, null);
} else {
var cost = parseInt(stdout.trim(), 10);
whendone(null, {'val_status': 'valid',
'error': false,
'val_stdout': stdout,
'val_stderr': stderr,
'cost': cost});
}
});
};
app.failValidate = function(domainPath, problemPath, planPath, cwd, whendone) {
cp.exec('timeout 5 ' + __dirname + '/validate -e ' + domainPath + ' ' + problemPath + ' ' + planPath,
{ cwd: cwd },
function _processStopped(error, stdout, stderr) {
whendone({
'val_status': 'err',
'error': 'Plan is invalid.',
'val_stdout': stdout,
'val_stderr': stderr
}, null);
});
};
app.errorToText = function(err) {
return "Error :" + JSON.stringify(err, null, 3);
};
app.resultToText = function(result) {
var toRet = '';
if (result['parse_status'] === 'err') {
toRet += "No plan found. Error:\n" + result['error'];
} else {
toRet += "Plan Found:\n ";
for (var i = 0; i < result['plan'].length; i++)
toRet += "\n " + result['plan'][i]['name'];
}
toRet += "\n\n\nOutput:\n";
toRet += result['output'];
return toRet;
};
app.set('view engine', 'ejs'); // set up ejs for templating
// set up our express application
app.use(morgan('dev')); // log every request to the console
// routes ======================================================================
require('./routes.js')(app); // load our routes and pass in our app
// launch ======================================================================
app.listen(port);
console.log('The magic happens on port ' + port);