From a90d78213a5d6a3d4aa0e1fb86d27286c1863904 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Thu, 27 Oct 2016 22:41:25 +0200 Subject: [PATCH] Basic serverside slaveawareness --- client.js | 36 ++++++++++++++++++++++++++++++++++++ master.js | 20 ++++++++++++++++++++ package.json | 1 + 3 files changed, 57 insertions(+) diff --git a/client.js b/client.js index 3ce1eb67..a7c8ee97 100644 --- a/client.js +++ b/client.js @@ -119,6 +119,7 @@ client.on('authenticated', function() { console.log('Authenticated!'); }).on('connected', function() { console.log('Connected!'); + getID(); }).on('disconnected', function() { console.log('Disconnected!'); // now reconnect @@ -128,6 +129,29 @@ client.on('authenticated', function() { // set some globals confirmedOrders = []; lastSignalCheck = Date.now(); + +// world IDs ------------------------------------------------------------------ +function getID() { + var payload = { + time : Date.now(), + rconPort : instanceconfig.clientPort, + rconPassword : instanceconfig.clientPassword, + serverPort : instanceconfig.factorioPort, + unique : instanceconfig.clientPassword.hashCode() + } + require('getmac').getMac(function(err,mac){ + if (err) throw err + payload.mac = mac + console.log(payload) + needle.post(config.masterIP + ":" + config.masterPort + '/getID', payload, function(err, response, body){ + if(response && response.body) { + // In the future we might be interested in whether or not we actually manage to send it, but honestly I don't care. + console.log(response.body) + } + } ); + }) + +} // provide items -------------------------------------------------------------- // trigger when something happens to output.txt fs.watch(instancedirectory + "/script-output/output.txt", function(eventType, filename) { @@ -258,3 +282,15 @@ setInterval(function() { } } }, 1000) + +// simple string hasher +String.prototype.hashCode = function(){ + var hash = 0; + if (this.length == 0) return hash; + for (i = 0; i < this.length; i++) { + char = this.charCodeAt(i); + hash = ((hash<<5)-hash)+char; + hash = hash & hash; // Convert to 32bit integer + } + return hash; +} diff --git a/master.js b/master.js index 47fa55f7..55a86494 100644 --- a/master.js +++ b/master.js @@ -26,6 +26,7 @@ db = {}; db.items = new Datastore({ filename: 'database/items.db', autoload: true }); db.signals = new Datastore({ filename: 'database/signals.db', autoload: true, inMemoryOnly: true}); db.signals.ensureIndex({ fieldName: 'time', expireAfterSeconds: 3600 }, function (err) {}); +// db.slaves = new Datastore({ filename: 'database/slaves.db', autoload: true, inMemoryOnly: false}); db.items.additem = function(object) { db.items.findOne({name:object.name}, function (err, doc) { @@ -42,6 +43,25 @@ db.items.additem = function(object) { } }) } +var slaves = {} +// world ID management +app.post("/getID", function(req,res) { + // request.body should be an object + // {rconPort, rconPassword, serverPort, mac, time} + // time us a unix timestamp we can use to check for how long the server has been unresponsive + // we should save that somewhere and give appropriate response + if(req.body){ + slaves[req.body.unique] = req.body + console.log("Slave: " + req.body.mac + " : " + req.body.serverPort) + } +}); + +// endpoint for getting information about all our slaves +app.get("/slaves", function(req, res) { + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); + res.send(slaves) +}); // endpoint to send items to app.post("/place", function(req, res) { diff --git a/package.json b/package.json index 01b32f75..cb9a0d13 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "child_process": "^1.0.2", "express": "^4.14.0", "fs": "0.0.2", + "getmac": "^1.2.1", "mkdirp": "^0.5.1", "nedb": "^1.8.0", "needle": "^1.1.0",