Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
diff --git a/modules/blobcast.js b/modules/blobcast.js
index a1c2e8f..e4e66b7 100644
--- a/modules/blobcast.js
+++ b/modules/blobcast.js
@@ -28,7 +28,9 @@ function BlobCast(cfg)
this.sessionModules = require('./session-modules').setup(cfg);
- this.serverManager.resetServer();
+ this.serverManager.resetServer(function(err) {
+ // log the server reset error here.
+ });
this.checkRoomTimer = setInterval(this.checkRooms.bind(_this), checkRoomTime);
this.checkRoomCache = {};
@@ -133,6 +135,7 @@ BlobCast.prototype.cleanupRoom = function(roomId, cb) {
};
BlobCast.prototype.onActionCreateRoom = function(user, message, cb) {
+ log.info("create room user: %j message: %j", user, message, {/*lol*/});
if(!message.hasOwnProperty('appId')) {
return cb(this.createErrorResult('CreateRoom'), new Error('No app id provided'));
}
@@ -144,7 +147,13 @@ BlobCast.prototype.onActionCreateRoom = function(user, message, cb) {
var server = this.serverManager.getServerName();
var options = (message.options === undefined || message.options === null ) ? {} : message.options;
- _this.roomManager.reserveRoom({appId:appId, server:server, owner:user.id, options:options}, function(err, res) {
+ var args = {
+ appId: appId,
+ server: server,
+ owner: user.id,
+ options: options
+ };
+ _this.roomManager.reserveRoom(args, function(err, res) {
if (err) {
return cb(_this.createErrorResult(message, err));
}
diff --git a/modules/blobmanager-local.js b/modules/blobmanager-local.js
index 98edee1..f9812aa 100644
--- a/modules/blobmanager-local.js
+++ b/modules/blobmanager-local.js
@@ -77,16 +77,17 @@ function BlobManager(cfg) {
};
this.destroyRoomBlob = function(roomId, cb) {
-
- // Delete room blob on S3 here too?
- this.blobManagerS3.updateRoomBlob( roomId, blob );
-
cb = cb || function() {};
if (!me.rooms[roomId]) {
var err = new Error('Room blob doesnt exist for room ' + roomId);
err.name = 'InvalidRoom';
return cb(err);
}
+
+ // Delete room blob on S3 here too?
+ var blob = me.rooms[roomId].blob;
+ this.blobManagerS3.updateRoomBlob( roomId, blob );
+
delete me.rooms[roomId];
cb();
};
diff --git a/modules/connectionmanager.js b/modules/connectionmanager.js
index b7280e0..0530357 100644
--- a/modules/connectionmanager.js
+++ b/modules/connectionmanager.js
@@ -18,8 +18,10 @@ function ConnectionManager(cfg)
var wsPort = (cfg.hasOwnProperty('wsPort')) ? cfg.wsPort : BASE_PORT + 2;
var wssPort = (cfg.hasOwnProperty('wssPort')) ? cfg.wssPort : BASE_PORT + 3;
var socketioLogLevel = cfg.socketioLogLevel || 2;
+ console.log("socket.io listening on port " + wsPort);
var wsServer = new SocketIONetworkAdapter({port:wsPort, secure:false, logLevel:socketioLogLevel});
var wssServer = new SocketIONetworkAdapter({port:wssPort, secure:true, logLevel:socketioLogLevel});
+ console.log("socket.io listening on secure port " + wssPort);
this.networkAdapters = [wsServer, wssServer];
}
@@ -421,4 +423,4 @@ ConnectionManager.prototype.handleRoutedMessageToOwner = function(roomId, messag
console.log(message);
};
-exports = module.exports = ConnectionManager;
\ No newline at end of file
+exports = module.exports = ConnectionManager;
diff --git a/modules/messenger-redis-polling.js b/modules/messenger-redis-polling.js
index 49b9152..55a7184 100644
--- a/modules/messenger-redis-polling.js
+++ b/modules/messenger-redis-polling.js
@@ -163,4 +163,4 @@ RedisMessenger.prototype.turnOnMultiserver = function(roomId, cb) {
this.msgToServers({type:'action', action:'multiserver', roomId:roomId}, cb);
};
-exports = module.exports = RedisMessenger;
\ No newline at end of file
+exports = module.exports = RedisMessenger;
diff --git a/modules/roommanager-redis.js b/modules/roommanager-redis.js
index d187dae..781c78d 100644
--- a/modules/roommanager-redis.js
+++ b/modules/roommanager-redis.js
@@ -4,7 +4,8 @@ var async = require('async');
var RoomUtils = require('./utils/room-utils');
var RedisUtils = require('./utils/redis-utils');
var crypto = require('crypto');
- // var util = require("util");
+var log = require('winston');
+// var util = require("util");
// var _ = require("underscore");
function RoomManager(cfg) {
@@ -131,6 +132,14 @@ RoomManager.prototype.destroyRoom = function(roomId, cb) {
};
RoomManager.prototype.reserveRoom = function(data, cb) {
+ log.info("reserveRoom called with data: %j", data, {});
+ var onRoomConflict = "fail";
+ var _this = this;
+ var numAttempts = 0;
+ var doneTrying = false;
+ var resultData = null;
+ var options = {};
+
if (!data || !data.hasOwnProperty('appId')) {
return cb(new Error('No appid specified'));
}
@@ -141,16 +150,18 @@ RoomManager.prototype.reserveRoom = function(data, cb) {
return cb(new Error('No owner specified'));
}
- if ( data.hasOwnProperty('options') ) {
- if ( data.options.hasOwnProperty( 'forceRoomId') ) {
- data.roomId = data.options.forceRoomId;
- }
+ if (data.hasOwnProperty('options')) {
+ options = data.options;
+ }
+
+ if (options.hasOwnProperty('forceRoomId')) {
+ data.roomId = options.forceRoomId;
+ }
+
+ if (options.hasOwnProperty('onConflict')) {
+ data.onConflict = options.onConflict;
}
- var _this = this;
- var numAttempts = 0;
- var doneTrying = false;
- var resultData = null;
async.whilst(
function () {
return !doneTrying && numAttempts < 5;
@@ -197,6 +208,7 @@ RoomManager.prototype.reserveRoom = function(data, cb) {
* roomId (optional) a room id to force
**/
RoomManager.prototype.reserveRoomAttempt = function(data, cb) {
+ log.info("reserve room attempt with data: %j", data, {});
var _this = this;
var roomId;
@@ -204,6 +216,23 @@ RoomManager.prototype.reserveRoomAttempt = function(data, cb) {
var appData;
var options = (data.options===undefined)?{}:data.options;
+ if (options.roomId) {
+ roomId = options.roomId;
+ }
+ if (data.roomId) {
+ roomId = data.roomId;
+ }
+
+ if (!roomId && options.onConflict == "destroy") {
+ log.info("refusing to honor onConflict destroy: no room id was provided!");
+ delete options.onConflict;
+ }
+
+ if (!roomId) {
+ roomId = RoomUtils.getRandomRoomId().toUpperCase();
+ }
+ roomId = roomId.toUpperCase();
+
async.series([
function (callback) {
_this.redisGetApp(data.appId, function(err, result) {
@@ -211,8 +240,6 @@ RoomManager.prototype.reserveRoomAttempt = function(data, cb) {
return callback(err);
}
appData = result;
- roomId = data.roomId || RoomUtils.getRandomRoomId();
- roomId = roomId.toUpperCase();
roomKey = _this.getRoomKey(roomId);
callback();
});
@@ -224,6 +251,14 @@ RoomManager.prototype.reserveRoomAttempt = function(data, cb) {
_this.redisClient.hexists(roomKey + ':data', 'server', function(err, res) {
if (res) {
_this.redisClient.unwatch();
+
+ // the room is occupied but we want to destroy it and
+ // route all new players to this new room.
+ if (options.onConflict == "destroy") {
+ log.info("destroying existing room for room key %s", roomId);
+ return callback();
+ }
+
var occupiedError = new Error('Room ' + roomId + ' is occupied');
occupiedError.name = 'RoomOccupied';
return callback(occupiedError);
diff --git a/modules/servermanager-redis.js b/modules/servermanager-redis.js
index 3137fcc..483da58 100644
--- a/modules/servermanager-redis.js
+++ b/modules/servermanager-redis.js
@@ -57,6 +57,7 @@ ServerManager.prototype.createServer = function(name, region, cb) {
// check if server exists before creating it
_this.getServer(name, function(err) {
+ console.log("getServer name: " + name);
if (err && err.name === 'ServerNotFound') {
_this.redisCreateServer(name, region, function(err, result) {
if (_this.autoEnable) {
@@ -137,6 +138,7 @@ ServerManager.prototype.getServerScore = function(connections, rooms) {
ServerManager.prototype.redisCreateServer = function(name, region, cb) {
var _this = this;
var serverKey = 'server:' + name;
+ console.log('ServerManager redisCreateServer with name: ' + name + ' region: ' + region);
async.series([
function (callback) {
_this.redisClient.watch(serverKey, callback);
diff --git a/modules/socketionetworkadapter.js b/modules/socketionetworkadapter.js
index 05c0fb7..f6918fa 100644
--- a/modules/socketionetworkadapter.js
+++ b/modules/socketionetworkadapter.js
@@ -57,18 +57,22 @@ function SocketIONetworkAdapter(cfg) {
var _this = this;
this.io.sockets.on('connection', function (socket) {
var _thisSocket = socket;
+ log.info("[sockets/%s] create", socket.id);
socket.on('msg', function(data) {
+ log.info("[sockets/%s/msg] %j", socket.id, data, {/*lol*/});
if(_this.delegate) {
_this.delegate.onMessageReceived(_this, _thisSocket, data);
}
});
socket.on('disconnect', function() {
+ log.info("[sockets/%s] disconnect", socket.id);
_this.delegate.onDisconnected(_this, _thisSocket);
});
socket.on('error', function(err) {
+ log.info("[sockets/%s/error] %j", socket.id, err, {/*lol*/});
log.error('Blobcast Sockets Error: ' + err.message, { stack: err.stack });
});
@@ -79,6 +83,7 @@ function SocketIONetworkAdapter(cfg) {
});
this.send = function(message, addressInfo) {
+ log.info("[socket/%s/send] %j", addressInfo.id, message, {/*lol*/});
addressInfo.emit('msg', message);
};
diff --git a/server.js b/server.js
index 00c00c9..f035af4 100644
--- a/server.js
+++ b/server.js
@@ -71,6 +71,7 @@ if (cfg.local) {
}
require('./modules/log')(cfg);
+log.info("using config file " + configFile);
for (var prop in cfg) {
log.info('CFG ' + prop + '=' + cfg[prop]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment