From 6f75fcba4301ad56ffdbd5dfc40c3f1b1e3e7d0a Mon Sep 17 00:00:00 2001 From: Gregg Tavares Date: Mon, 24 Nov 2014 22:53:14 -0800 Subject: [PATCH] make it try to change ipaddresses when NIC changes --- lib/iputils.js | 63 ++++++++++++++++++++++++++++++++++++-------- server/hft-server.js | 8 +++++- server/hftsite.js | 48 +++++++++++++++++++++++---------- todo.md | 10 +++++++ 4 files changed, 103 insertions(+), 26 deletions(-) diff --git a/lib/iputils.js b/lib/iputils.js index 48a496f2..25f33a6d 100644 --- a/lib/iputils.js +++ b/lib/iputils.js @@ -30,6 +30,10 @@ */ "use strict"; +var events = require('events'); + +var g_emitter = new events.EventEmitter(); + /** * @module */ @@ -47,7 +51,6 @@ var getIpAddress = (function() { var os = require('os'); return function() { - var now = Date.now(); if (!addresses || now - lastRead > cacheTime) { lastRead = now; @@ -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); + + diff --git a/server/hft-server.js b/server/hft-server.js index d72c4d16..ec1445cd 100644 --- a/server/hft-server.js +++ b/server/hft-server.js @@ -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}); } @@ -124,6 +125,8 @@ var HFTServer = function(options, startedCallback) { }; }()); + var ipIntervalId = setInterval(getAddress, 15 * 1000); + var getBaseUrl = function() { return "http://" + getAddress() + ":" + g.port; }; @@ -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() { diff --git a/server/hftsite.js b/server/hftsite.js index 4ed2ea02..0bde5b24 100644 --- a/server/hftsite.js +++ b/server/hftsite.js @@ -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 diff --git a/todo.md b/todo.md index 92218929..901f4181 100644 --- a/todo.md +++ b/todo.md @@ -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(fn) + * RegisterCmdHandler(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?