Skip to content

Commit

Permalink
update ip address
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed Nov 24, 2014
1 parent fb670db commit 3f78dd5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 16 deletions.
4 changes: 2 additions & 2 deletions server/game-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ GameGroup.prototype.getNumPlayers = function() {
}, 0);
};

GameGroup.prototype.controllerUrl = function() {
return this.games.length > 0 ? this.games[0].controllerUrl : undefined;
GameGroup.prototype.getControllerUrl = function(baseUrl) {
return this.games.length > 0 ? this.games[0].getControllerUrl(baseUrl) : undefined;
};

GameGroup.prototype.sendQuit = function() {
Expand Down
8 changes: 4 additions & 4 deletions server/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var hftSite = require('./hftsite');

/**
* @typedef {object} Game~Options
* @property {string} baseUrl the base url used to make urls
*/

/**
Expand All @@ -57,13 +56,14 @@ var Game = function(id, gameGroup, options) {
this.numPlayers = 0;
this.sendQueue = [];
this.options = options;
if (this.runtimeInfo) {
this.controllerUrl = options.baseUrl + "/games/" + this.runtimeInfo.info.happyFunTimes.gameId + "/index.html";
}
this.setGameId();
debug("create game " + this. gameId);
};

Game.prototype.getControllerUrl = function(baseUrl) {
return this.runtimeInfo ? baseUrl + "/games/" + this.runtimeInfo.info.happyFunTimes.gameId + "/index.html" : undefined;
};

Game.prototype.setGameId = function() {
this.gameId = (this.runtimeInfo ? this.runtimeInfo.info.happyFunTimes.gameId : "") + " id=" + this.id;
};
Expand Down
42 changes: 37 additions & 5 deletions server/hft-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var HFTGame = require('./hftgame');
var hftSite = require('./hftsite');
var highResClock = require('../lib/highresclock');
var http = require('http');
var iputils = require('../lib/iputils');
var log = require('../lib/log');
var mime = require('mime');
var NonRequire = require('./non-require');
Expand Down Expand Up @@ -99,6 +100,34 @@ var HFTServer = function(options, startedCallback) {
g[prop] = options[prop];
});

var updateIpAddress = function(address) {
if (relayServer) {
relayServer.setOptions({baseUrl: getBaseUrl()});
}
hftSite.setup({address: address});
if (appleCaptivePortalHandler) {
appleCaptivePortalHandler.setOptions({address: address});
}
};

var getAddress = (function() {
var oldAddress;

return function() {
var address = g.address || iputils.getOneIpAddress();
if (address !== oldAddress) {
oldAddress = address;
console.log("using ip address: " + address);
updateIpAddress(address);
}
return address;
};
}());

var getBaseUrl = function() {
return "http://" + getAddress() + ":" + g.port;
};

g.gameDB = g.gameDB || new AvailableGames();

var eventEmitter = new events.EventEmitter();
Expand All @@ -111,7 +140,11 @@ var HFTServer = function(options, startedCallback) {
var app = express();
var relayServer;

hftSite.setup(g);
hftSite.setup({
address: getAddress(),
port: g.port,
privateServer: g.privateServer,
});

g.cwd = path.normalize(path.join(__dirname, ".."));

Expand Down Expand Up @@ -308,7 +341,7 @@ var HFTServer = function(options, startedCallback) {

var appleCaptivePortalHandler = new AppleCaptivePortalHandler({
baseDir: path.join(g.cwd, g.baseDir),
address: g.address,
address: getAddress(),
port: g.port,
sendFileFn: sendFileResponse,
});
Expand Down Expand Up @@ -377,7 +410,7 @@ var HFTServer = function(options, startedCallback) {
}
var prepFn = isTemplate ? (function() {
var params = [{
localhost: g.address,
localhost: getAddress(),
}];
if (runtimeInfo) {
params.push(runtimeInfo);
Expand Down Expand Up @@ -521,8 +554,7 @@ var HFTServer = function(options, startedCallback) {
}
var RelayServer = require('./relayserver.js');
relayServer = options.relayServer || new RelayServer(servers, {
address: g.address,
baseUrl: "http://" + g.address + ":" + g.port,
baseUrl: getBaseUrl(),
noMessageTimout: options.inactivityTimeout,
gameDB: g.gameDB,
});
Expand Down
19 changes: 14 additions & 5 deletions server/relayserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ var WSServer = require('./websocketserver');
* RelayServer options
* @typedef {Object} RelayServer~Options
* @property {AvailableGames} gameDB The game db
* @property {String} address - address that will
* replace "localhost" when a game's controllerUrl is
* passed in.
* @property {String?} baseUrl ???
* @property {WebSocketServer?} WebSocketServer constructor for WebSocketServer (for testing)
*/
Expand Down Expand Up @@ -79,10 +76,22 @@ var RelayServer = function(servers, options) {
var g_nextSessionId = 0;
var g_gameGroups = {};
var g_numGameGroups = 0;
var g_options = {};
var socketServers = [];
var eventEmitter = new events.EventEmitter();
var gameDB = options.gameDB;

this.setOptions = function(options) {
["baseUrl"].forEach(function(key) {
var value = options[key];
if (value !== undefined) {
g_options[key] = value;
}
});
};

this.setOptions(options);

this.on = eventEmitter.on.bind(eventEmitter);
this.addListener = this.on;
this.removeListener = eventEmitter.removeListener.bind(eventEmitter);
Expand Down Expand Up @@ -130,7 +139,7 @@ var RelayServer = function(servers, options) {
}
var gameGroup = g_gameGroups[gameId];
if (!gameGroup && makeGroup) {
gameGroup = new GameGroup(gameId, this, { gameDB: gameDB, baseUrl: options.baseUrl });
gameGroup = new GameGroup(gameId, this, { gameDB: gameDB });
g_gameGroups[gameId] = gameGroup;
++g_numGameGroups;
debug("added game group: " + gameId + ", num game groups = " + g_numGameGroups);
Expand Down Expand Up @@ -161,7 +170,7 @@ var RelayServer = function(servers, options) {
gameId: id,
serverName: computerName.get().trim(),
numPlayers: gameGroup.getNumPlayers(),
controllerUrl: gameGroup.controllerUrl(),
controllerUrl: gameGroup.getControllerUrl(g_options.baseUrl),
runtimeInfo: gameGroup.runtimeInfo,
});
}
Expand Down

0 comments on commit 3f78dd5

Please sign in to comment.