Skip to content

Commit

Permalink
make it try to change ipaddresses when NIC changes
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Nov 25, 2014
1 parent 3f78dd5 commit 6f75fcb
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 26 deletions.
63 changes: 52 additions & 11 deletions lib/iputils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
*/
"use strict";

var events = require('events');

var g_emitter = new events.EventEmitter();

/**
* @module
*/
Expand All @@ -47,7 +51,6 @@ var getIpAddress = (function() {
var os = require('os');

return function() {

var now = Date.now();
if (!addresses || now - lastRead > cacheTime) {
lastRead = now;
Expand All @@ -66,21 +69,59 @@ var getIpAddress = (function() {
};
}());

var getOneIpAddress = function() {
var address;
var addresses = getIpAddress();
var arraysEqual = function(a, b) {
if ((!a && b) || (!b && a)) {
return false;
}

if (a.length != b.length) {
return false;
}

if (addresses.length < 1) {
console.error("No IP address found!!!");
} else {
address = addresses[0];
if (addresses.length > 1) {
console.log("more than 1 IP address found: " + addresses);
var len = a.length;
for (var ii = 0; ii < len; ++ii) {
if (a[ii] !== b[ii]) {
return false;
}
}
return address;
return true;
};

var getOneIpAddress = (function() {
var lastAddress;
var oldAddresses;

return function() {
var address;
var addresses = getIpAddress();

if (addresses.length < 1) {
console.error("No IP address found!!!");
} else {
address = addresses[0];
if (addresses.length > 1) {
var newAddresses = addresses.slice().sort();
if (!arraysEqual(newAddresses, oldAddresses)) {
console.log("more than 1 IP address found: " + addresses);
oldAddresses = newAddresses;
}
}
}

if (address !== lastAddress) {
lastAddress = address;
g_emitter.emit('changed', lastAddress);
}

return address;
};
}());

exports.getIpAddress = getIpAddress;
exports.getOneIpAddress = getOneIpAddress;
exports.on = g_emitter.on.bind(g_emitter);
exports.addListener = exports.on;
exports.removeListener = g_emitter.removeListener.bind(g_emitter);



8 changes: 7 additions & 1 deletion server/hft-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ var HFTServer = function(options, startedCallback) {
relayServer.setOptions({baseUrl: getBaseUrl()});
}
hftSite.setup({address: address});
hftSite.inform();
if (appleCaptivePortalHandler) {
appleCaptivePortalHandler.setOptions({address: address});
}
Expand All @@ -124,6 +125,8 @@ var HFTServer = function(options, startedCallback) {
};
}());

var ipIntervalId = setInterval(getAddress, 15 * 1000);

var getBaseUrl = function() {
return "http://" + getAddress() + ":" + g.port;
};
Expand Down Expand Up @@ -612,7 +615,10 @@ var HFTServer = function(options, startedCallback) {
servers.forEach(function(server) {
server.close();
});

if (ipIntervalId) {
clearInterval(ipIntervalId);
ipIntervalId = undefined;
}
}.bind(this);

this.getSettings = function() {
Expand Down
48 changes: 34 additions & 14 deletions server/hftsite.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,44 @@ var io = require('../lib/io');
var restUrl = require('rest-url');

var g = {
throttleTime: 1000
};

var getTime = function() {
return Date.now();
};

// Sends the local ip address and port
var inform = function() {
if (!g.privateServer) {
var url = restUrl.make(process.env.HFT_RENDEZVOUS_URL || config.getSettings().settings.rendezvousUrl, {
hftip: g.address,
hftport: g.port,
})
debug("ping: " + url);
io.sendJSON(url, {}, {}, function(err, result) {
// do I care?
if (err) {
console.error(err);
var inform = (function() {
var lastAddress;
var lastPort;
var lastTime = 0;

return function() {
if (!g.privateServer && g.port && g.address) {
var now = getTime();
var elapsedTime = now - lastTime;
if (lastAddress != g.address ||
lastPort != g.port ||
elapsedTime > g.throttleTime) {
lastTime = now;
lastAddress = g.address;
lastPort = g.port;
var url = restUrl.make(process.env.HFT_RENDEZVOUS_URL || config.getSettings().settings.rendezvousUrl, {
hftip: g.address,
hftport: g.port,
})
debug("ping: " + url);
io.sendJSON(url, {}, {}, function(err, result) {
// do I care?
if (err) {
console.error(err);
}
});
}
});
}
};
}
};
}());

/**
* @typedef {Object} HFTSite~Options
Expand Down
10 changes: 10 additions & 0 deletions todo.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
To Do
=====

* unity plugin
* make package.json editor
* make "new project" button?
* make "start HFT?" button (or should running it be enough)
* make "location of HFT" button?
* make stages of commands
* cmd with cmd string RegisterCmdHandler<type>(fn)
* RegisterCmdHandler<type>(name, fn)
* RegisterCmdHandler(fn);
* Use RegisterCmdHandler(fn) for gamestart
* move tiled support to hft-tiled?
* add tiled support to jumpjump
* add glow processing to powpow?
Expand Down

0 comments on commit 6f75fcb

Please sign in to comment.