|
|
var http = require('http');
|
|
const fs = require('fs');
|
|
var url = require('url');
|
|
var path = require('path');
|
|
var StringDecoder = require('string_decoder').StringDecoder;
|
|
var exec = require("child_process").exec;
|
|
var Promise= require('promise');
|
|
|
|
var mimeTypes = {
|
|
"html": "text/html",
|
|
"jpeg": "image/jpeg",
|
|
"jpg": "image/jpeg",
|
|
"png": "image/png",
|
|
"js": "text/javascript",
|
|
"css": "text/css",
|
|
"json": "text/plain"};
|
|
|
|
|
|
|
|
function writenodes(nodes) {
|
|
|
|
fs.writeFile('public/Verbesserungen.txt', nodes, { flag: 'w' }, (err) => {
|
|
if (err)
|
|
console.log(err);
|
|
else {
|
|
console.log("File written successfully\n");
|
|
console.log("The written has the following contents:");
|
|
console.log(fs.readFileSync("public/nodes.txt", "utf8"));
|
|
}
|
|
})};
|
|
// function writeedges(edges) {
|
|
//
|
|
// fs.writeFile('public/edges.txt', edges, { flag: 'w' }, (err) => {
|
|
// if (err)
|
|
// console.log(err);
|
|
// else {
|
|
// console.log("File written successfully\n");
|
|
// console.log("The written has the following contents:");
|
|
// console.log(fs.readFileSync("public/edges.txt", "utf8"));
|
|
// }
|
|
// })};
|
|
// the following read funcitons are not asyncronous, that means they will block the process..
|
|
// for scalability this has to be changed in future
|
|
// function readnodes() {
|
|
// var input = fs.readFileSync('public/nodes.txt', 'utf8')
|
|
// // console.log('input ooohyeah',input)
|
|
// // // var danodes = JSON.parse(input);
|
|
// //
|
|
// // const lines = input.split(/\r?\n/);
|
|
// // console.log('line numbaaa 1', lines, lines[1], lines[1].length)
|
|
// // var string = '';
|
|
// // var i;
|
|
// //
|
|
// // for (i = 0; i < (lines.length - 2); i++) {
|
|
// // string += '"data' + i.toString() + '":' + lines[i] + ' ,' ;
|
|
// // };
|
|
// // string += '"data' + lines.length.toString() + '":' + lines[lines.length - 2] ;
|
|
// // string = '{' + string + '}' ;
|
|
// // const jsonlines = JSON.parse(string)
|
|
// //
|
|
// //
|
|
// // let sortedlines = [];
|
|
// // sortedlines = [].slice.call(jsonlines).sort(function(a,b) {
|
|
// // var x = a.length;
|
|
// // var y = b.length;
|
|
// // if (x < y) {
|
|
// // return -1;
|
|
// // }
|
|
// // if (x > y) {
|
|
// // return 1;
|
|
// // }
|
|
// // return 0;
|
|
// //
|
|
// // });
|
|
// // console.log('daline1 in nodes', sortedlines)
|
|
// return input
|
|
// };
|
|
// function readedges() {
|
|
//
|
|
// var daedges = fs.readFileSync('public/edges.txt', 'utf8');
|
|
// // const lines = daedges.split(/\r?\n/);
|
|
// // // console.log(lines)
|
|
// // sortedlines = lines.sort(function(a,b) {
|
|
// // var x = a.length;
|
|
// // var y = b.length;
|
|
// // if (x < y) {
|
|
// // return -1;
|
|
// // }
|
|
// // if (x > y) {
|
|
// // return 1;
|
|
// // }
|
|
// // return 0;
|
|
// //
|
|
// // });
|
|
// // console.log('daline1 in edges', sortedlines[0])
|
|
// return daedges
|
|
// };
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
http.createServer((request, response)=>{
|
|
var pathname = url.parse(request.url).pathname;
|
|
var filename;
|
|
if(pathname === "/"){
|
|
filename = "Prototyp.html";
|
|
}
|
|
else{
|
|
filename = path.join(process.cwd(), pathname);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try{
|
|
fs.accessSync(filename, fs.F_OK);
|
|
var fileStream = fs.createReadStream(filename);
|
|
var mimeType = mimeTypes[path.extname(filename).split(".")[1]];
|
|
response.writeHead(200, {'Content-Type':mimeType});
|
|
fileStream.pipe(response);
|
|
|
|
|
|
if (mimeType === "text/plain"){
|
|
// Get the payload,if any
|
|
var decoder = new StringDecoder('utf-8');
|
|
var buffer = '';
|
|
var theanswer = '';
|
|
request.on('data', function(data) {
|
|
buffer += decoder.write(data);
|
|
});
|
|
var theanswer = '';
|
|
console.log('i am at least come til here')
|
|
request.on('end', function() {
|
|
buffer += decoder.end();
|
|
console.log(buffer);
|
|
if (buffer[3] === 'i'){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(buffer);
|
|
|
|
|
|
}
|
|
if (buffer[3] === 'f'){
|
|
|
|
|
|
console.log(buffer);
|
|
theanswer = 'your changes were taken into the\n\nM A T R I X'
|
|
|
|
|
|
|
|
}
|
|
|
|
console.log(buffer[3])
|
|
if (buffer === 'DONE'){
|
|
|
|
console.log('recieved a DONE')
|
|
|
|
exec("cp /dev/null > public/data.json")
|
|
|
|
|
|
|
|
|
|
}
|
|
if (buffer.length > 1) {
|
|
console.log('starting hte python code, argument is ' + buffer)
|
|
const execpython = new Promise((resolve, reject) => {
|
|
exec("python prototype_all_chained.py " + buffer + " > public/data.json", (error, stdout, stderr) => {
|
|
if (error) {
|
|
console.log(`error: ${error.message}`);
|
|
reject(error)
|
|
} else {
|
|
console.log(`stdout: ${stdout}`);
|
|
resolve(stdout)
|
|
}
|
|
});
|
|
|
|
})
|
|
|
|
|
|
|
|
const checkIfItsDone = () => {
|
|
execpython
|
|
.then(ok => {
|
|
console.log(ok)
|
|
})
|
|
.catch(err => {
|
|
console.log(err)
|
|
})
|
|
}
|
|
|
|
|
|
|
|
checkIfItsDone()
|
|
}
|
|
|
|
// var string = 'oi';
|
|
// // var string = execpython('oi');
|
|
// // console.log('output of execpython', string);
|
|
// Promise.resolve(string)
|
|
// .then(execpython(string))
|
|
// .then(s => response.end(s))
|
|
|
|
|
|
// Log the request/response
|
|
console.log('Payload received: ',buffer);
|
|
});
|
|
|
|
|
|
}
|
|
|
|
}
|
|
catch(e) {
|
|
console.log('File not exists: ' + filename);
|
|
|
|
response.writeHead(404, {'Content-Type': 'text/plain'});
|
|
response.write('404 Not Found\n');
|
|
response.end();
|
|
return;
|
|
}
|
|
|
|
return;
|
|
}
|
|
|
|
).listen(8888);
|