Basic serverside slaveawareness

This commit is contained in:
Danielv123 committed 2016-10-27 22:41:25 +02:00
1 parent f6de6e026b
commit a90d78213a
3 files changed
+57

No files matched your search

+36
View File
@@ -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;
}
+20
View File
@@ -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) {
+1
View File
@@ -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",