net-helper/server.js
2025-01-23 16:22:26 +03:00

48 lines
1.1 KiB
JavaScript

'use strict';
global.moduleApi = require("./module-api");
const EventEmitter = require("events");
const rtmap = require("./routemap-parser");
class ServersOrchestry extends EventEmitter {
constructor () {
super();
this.on("update-routemap", () => {
console.log("[Update routemap]: routemap.txt updated, reloading servers...");
this.stop();
this.run();
});
this.instances = new Array();
}
run () {
this.hash = rtmap.hash();
const syntaxTree = rtmap.parse();
for (let faddress in syntaxTree) {
const [address, port] = faddress.split(/:/gmi);
const localSyntaxTree = syntaxTree[faddress];
const instance = new localSyntaxTree.Type({ address, port }, localSyntaxTree);
localSyntaxTree.modules.forEach(module => module.bind(instance));
instance.run();
this.instances.push(instance);
localSyntaxTree.modules.forEach(module => module.emit("runned"));
}
}
stop () {
for (let instance of this.instances) {
instance.stop();
}
this.instances = [];
}
}
const orchestry = new ServersOrchestry();
orchestry.run();
setInterval(() => {
if (rtmap.hash() !== orchestry.hash)
orchestry.emit("update-routemap");
}, 100);