2017-04-13 19:33:34 +02:00
|
|
|
/* Worker code, contains Netscript scripts that are actually running */
|
2016-11-28 23:02:06 +01:00
|
|
|
|
2016-11-30 23:08:21 +01:00
|
|
|
//TODO Tested For and while and generic call statements. Have not tested if statements
|
|
|
|
|
2016-11-28 23:02:06 +01:00
|
|
|
/* Actual Worker Code */
|
2016-12-19 19:20:19 +01:00
|
|
|
function WorkerScript(script) {
|
2016-11-30 00:38:50 +01:00
|
|
|
this.name = "";
|
|
|
|
this.running = false;
|
2016-12-02 22:57:20 +01:00
|
|
|
this.serverIp = null;
|
2016-11-30 00:38:50 +01:00
|
|
|
this.code = "";
|
|
|
|
this.env = new Environment();
|
2016-12-02 22:57:20 +01:00
|
|
|
this.output = "";
|
2016-12-19 19:20:19 +01:00
|
|
|
this.ramUsage = 0;
|
|
|
|
this.scriptRef = script;
|
2017-05-01 07:39:48 +02:00
|
|
|
this.errorMessage = "";
|
2016-12-19 19:20:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//Returns the server on which the workerScript is running
|
|
|
|
WorkerScript.prototype.getServer = function() {
|
|
|
|
return AllServers[this.serverIp];
|
2016-11-28 23:02:06 +01:00
|
|
|
}
|
|
|
|
|
2016-11-30 00:07:24 +01:00
|
|
|
//Array containing all scripts that are running across all servers, to easily run them all
|
2016-11-28 23:02:06 +01:00
|
|
|
var workerScripts = [];
|
|
|
|
|
|
|
|
//Loop through workerScripts and run every script that is not currently running
|
|
|
|
function runScriptsLoop() {
|
2016-12-05 23:31:46 +01:00
|
|
|
//Run any scripts that haven't been started
|
2016-11-28 23:02:06 +01:00
|
|
|
for (var i = 0; i < workerScripts.length; i++) {
|
2016-12-05 23:31:46 +01:00
|
|
|
//If it isn't running, start the script
|
2016-12-06 17:59:20 +01:00
|
|
|
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == false) {
|
2016-12-15 23:22:42 +01:00
|
|
|
try {
|
|
|
|
var ast = Parser(Tokenizer(InputStream(workerScripts[i].code)));
|
|
|
|
} catch (e) {
|
2017-04-13 21:36:03 +02:00
|
|
|
dialogBoxCreate("Syntax ERROR in " + workerScripts[i].name + ":", e, "", "");
|
|
|
|
workerScripts[i].env.stopFlag = true;
|
2016-12-19 19:20:19 +01:00
|
|
|
continue;
|
2016-12-15 23:22:42 +01:00
|
|
|
}
|
2017-04-23 04:32:51 +02:00
|
|
|
|
2016-11-28 23:02:06 +01:00
|
|
|
workerScripts[i].running = true;
|
2016-12-05 23:31:46 +01:00
|
|
|
var p = evaluate(ast, workerScripts[i]);
|
|
|
|
//Once the code finishes (either resolved or rejected, doesnt matter), set its
|
|
|
|
//running status to false
|
2016-12-06 17:59:20 +01:00
|
|
|
p.then(function(w) {
|
2016-12-14 21:29:40 +01:00
|
|
|
console.log("Stopping script " + w.name + " because it finished running naturally");
|
2016-12-06 17:59:20 +01:00
|
|
|
w.running = false;
|
|
|
|
w.env.stopFlag = true;
|
|
|
|
}, function(w) {
|
2016-12-15 23:22:42 +01:00
|
|
|
if (w instanceof Error) {
|
2016-12-19 19:20:19 +01:00
|
|
|
//Error text format: |serverip|scriptname|error message
|
|
|
|
var errorText = w.toString();
|
2017-02-16 19:52:11 +01:00
|
|
|
if (Engine.Debug) {
|
|
|
|
console.log("Error in script: " + errorText);
|
|
|
|
}
|
2016-12-19 19:20:19 +01:00
|
|
|
var errorTextArray = errorText.split("|");
|
|
|
|
if (errorTextArray.length != 4) {
|
|
|
|
console.log("ERROR: Something wrong with Error text in evaluator...");
|
|
|
|
console.log("Error text: " + errorText);
|
|
|
|
}
|
|
|
|
var serverIp = errorTextArray[1];
|
|
|
|
var scriptName = errorTextArray[2];
|
|
|
|
var errorMsg = errorTextArray[3];
|
|
|
|
|
2017-02-24 16:11:45 +01:00
|
|
|
dialogBoxCreate("Script runtime error: ", "Server Ip: " + serverIp, "Script name: " + scriptName, errorMsg);
|
2016-12-19 19:20:19 +01:00
|
|
|
|
|
|
|
//Find the corresponding workerscript and set its flags to kill it
|
|
|
|
for (var i = 0; i < workerScripts.length; ++i) {
|
|
|
|
if (workerScripts[i].serverIp == serverIp && workerScripts[i].name == scriptName) {
|
|
|
|
workerScripts[i].running = false;
|
|
|
|
workerScripts[i].env.stopFlag = true;
|
|
|
|
return;
|
|
|
|
}
|
2017-05-01 07:39:48 +02:00
|
|
|
}
|
|
|
|
} else if (w instanceof WorkerScript) {
|
|
|
|
if (isScriptErrorMessage(w.errorMessage)) {
|
|
|
|
var errorTextArray = w.errorMessage.split("|");
|
|
|
|
if (errorTextArray.length != 4) {
|
|
|
|
console.log("ERROR: Something wrong with Error text in evaluator...");
|
|
|
|
console.log("Error text: " + errorText);
|
|
|
|
}
|
|
|
|
var serverIp = errorTextArray[1];
|
|
|
|
var scriptName = errorTextArray[2];
|
|
|
|
var errorMsg = errorTextArray[3];
|
2016-12-19 19:20:19 +01:00
|
|
|
|
2017-05-01 07:39:48 +02:00
|
|
|
dialogBoxCreate("Script runtime error: ", "Server Ip: " + serverIp, "Script name: " + scriptName, errorMsg);
|
|
|
|
}
|
2016-12-15 23:22:42 +01:00
|
|
|
w.running = false;
|
|
|
|
w.env.stopFlag = true;
|
|
|
|
}
|
2016-12-05 23:31:46 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Delete any scripts that finished or have been killed. Loop backwards bc removing
|
|
|
|
//items fucks up the indexing
|
|
|
|
for (var i = workerScripts.length - 1; i >= 0; i--) {
|
|
|
|
if (workerScripts[i].running == false && workerScripts[i].env.stopFlag == true) {
|
2016-12-14 00:52:32 +01:00
|
|
|
console.log("Deleting script: " + workerScripts[i].name);
|
2016-12-19 19:20:19 +01:00
|
|
|
//Delete script from the runningScripts array on its host serverIp
|
2016-12-05 23:31:46 +01:00
|
|
|
var ip = workerScripts[i].serverIp;
|
|
|
|
var name = workerScripts[i].name;
|
|
|
|
for (var j = 0; j < AllServers[ip].runningScripts.length; j++) {
|
|
|
|
if (AllServers[ip].runningScripts[j] == name) {
|
2016-12-06 17:59:20 +01:00
|
|
|
AllServers[ip].runningScripts.splice(j, 1);
|
2016-12-05 23:31:46 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2016-12-19 19:20:19 +01:00
|
|
|
|
|
|
|
//Free RAM
|
|
|
|
AllServers[ip].ramUsed -= workerScripts[i].ramUsage;
|
2016-12-05 23:31:46 +01:00
|
|
|
|
|
|
|
//Delete script from workerScripts
|
|
|
|
workerScripts.splice(i, 1);
|
2016-12-20 21:18:34 +01:00
|
|
|
|
|
|
|
//Delete script from Active Scripts
|
|
|
|
Engine.deleteActiveScriptsItem(i);
|
2016-11-28 23:02:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(runScriptsLoop, 10000);
|
|
|
|
}
|
|
|
|
|
2016-12-06 19:09:23 +01:00
|
|
|
//Queues a script to be killed by settings its stop flag to true. Then, the code will reject
|
|
|
|
//all of its promises recursively, and when it does so it will no longer be running.
|
|
|
|
//The runScriptsLoop() will then delete the script from worker scripts
|
|
|
|
function killWorkerScript(scriptName, serverIp) {
|
|
|
|
for (var i = 0; i < workerScripts.length; i++) {
|
|
|
|
if (workerScripts[i].name == scriptName && workerScripts[i].serverIp == serverIp) {
|
|
|
|
workerScripts[i].env.stopFlag = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-14 21:29:40 +01:00
|
|
|
//Queues a script to be run
|
|
|
|
function addWorkerScript(script, server) {
|
|
|
|
var filename = script.filename;
|
|
|
|
|
|
|
|
//Update server's ram usage
|
|
|
|
server.ramUsed += script.ramUsage;
|
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Create the WorkerScript
|
|
|
|
var s = new WorkerScript(script);
|
2016-12-14 21:29:40 +01:00
|
|
|
s.name = filename;
|
|
|
|
s.code = script.code;
|
|
|
|
s.serverIp = server.ip;
|
2016-12-19 19:20:19 +01:00
|
|
|
s.ramUsage = script.ramUsage;
|
|
|
|
|
|
|
|
//Add the WorkerScript to the Active Scripts list
|
|
|
|
Engine.addActiveScriptsItem(s);
|
|
|
|
|
|
|
|
//Add the WorkerScript
|
2016-12-14 21:29:40 +01:00
|
|
|
workerScripts.push(s);
|
2016-12-15 18:51:23 +01:00
|
|
|
return;
|
2016-12-14 21:29:40 +01:00
|
|
|
}
|
|
|
|
|
2016-12-19 19:20:19 +01:00
|
|
|
//Updates the online running time stat of all running scripts
|
|
|
|
function updateOnlineScriptTimes(numCycles = 1) {
|
|
|
|
var time = (numCycles * Engine._idleSpeed) / 1000; //seconds
|
|
|
|
for (var i = 0; i < workerScripts.length; ++i) {
|
|
|
|
workerScripts[i].scriptRef.onlineRunningTime += time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-28 23:02:06 +01:00
|
|
|
runScriptsLoop();
|