-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
124 lines (100 loc) · 3.5 KB
/
index.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
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const spawn = require("child_process").spawn;
const exec = require("child_process").exec;
var fs = require("fs");
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('frontend'));
app.use(function (req, res, next){
console.log("HTTP request", req.method, req.url, req.body);
next();
});
app.get('/api/program/:programName', function(req, res, next){
var programName = req.params.programName;
var fs = require('fs');
fs.readFile("./PresetPrograms/"+programName+".java", 'utf8', function(err, program){
return res.json({program:program});
});
});
app.post('/api/runJavaTest/', function (req, res, next) {
var programName = req.body["programName"];
var type = req.body["type"];
var output = "";
var error = "";
var sum = 0;
var java = spawn('bash', ["runProgram.sh", parseInt(type), programName]);
java.stdout.on('data', function(data){
output += data;
});
java.stderr.on('data', function(data){
sum+=parseFloat(""+data);
error += data + "\n"
});
java.on('close', function(code){
var time;
if (type == 2) {
time = sum*1000;
} else {
time = (sum/5)*1000;
}
time = Math.round(time*100)/100;
console.log("JAVA: Process exited with code "+ code +" and executed in " + time + " milliseconds");
return res.json({code:code, time:time, output:output});
});
});
app.post('/api/compileJavaProgram', function (req, res, next){
var filename = req.body["filename"];
fs.writeFile(filename+".java", req.body['program'], function (err) {
if (err) throw err;
console.log('Program has been saved');
var javac = spawn("javac", [filename + ".java"]);
javac.stdout.on('data', function(data){
console.log("stdout: " + data);
});
javac.stderr.on('data', function(data){
console.log("stderr: " + data);
});
javac.on('close', function(code){
console.log("JAVAC: Process exited with code "+ code);
return res.json({code:code});
});
});
});
app.post('/api/clearCache', function (req, res, next){
var cache = exec("rm -f testCache");
cache.on('close', function(code) {
var cache2 = exec("javaj9 -Xshareclasses:destroyAll");
cache2.on('close', function(code) {
return res.json({});
});
});
});
app.post('/api/coldRun', function (req, res, next){
var programName = req.body["programName"];
var type = req.body["type"];
var output = "";
var error = "";
var sum = 0;
var java = spawn('bash', ["coldRun.sh", parseInt(type), programName]);
java.stdout.on('data', function(data){
output += data;
});
java.stderr.on('data', function(data){
sum+=parseFloat(""+data);
error += data + "\n"
});
java.on('close', function(code){
var time = sum*1000;
time = Math.round(time*100)/100;
console.log("JAVA: Process exited with code "+ code +" and executed in " + time + " milliseconds");
return res.json({code:code, time:time, output:output});
});
});
const http = require('http');
const PORT = 3000;
http.createServer(app).listen(PORT, function (err) {
if (err) console.log(err);
else console.log("HTTP server on http://localhost:%s", PORT);
});