Skip to content

Commit

Permalink
Merge pull request #95 from Elao/develop
Browse files Browse the repository at this point in the history
Data Inspector
  • Loading branch information
Tom32i committed Dec 31, 2014
2 parents 435afe6 + 615954a commit 41528f1
Show file tree
Hide file tree
Showing 18 changed files with 471 additions and 39 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ web/index.html
bin/*
stats/*
config.json
*.sublime-*
11 changes: 10 additions & 1 deletion config.json.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
{
"googleAnalyticsId": false
"port": 8080,
"googleAnalyticsId": null,
"inspector": {
"enabled": false,
"host": "127.0.0.1",
"port": 8086,
"username": "root",
"password": "root",
"database": "curvytron"
}
}
2 changes: 1 addition & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var fs = require('fs'),
try {
config = require('./config.json');
} catch (error) {
config = { googleAnalyticsId: false };
config = { googleAnalyticsId: null };
}

var srcDir = './src/',
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@
},
"dependencies": {
"express": "~4.4.*",
"faye-websocket": "^0.7.*"
"faye-websocket": "^0.7.*",
"influx": "^3.2.0",
"usage": "^0.5.0"
},
"scripts": {
"install": "bower install"
Expand Down
2 changes: 1 addition & 1 deletion src/client/service/Analyser.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Analyser.prototype.onRouteChange = function(event, currentScope, previousScope)
*/
Analyser.prototype.getPath = function(path, params)
{
for (key in params) {
for (var key in params) {
if (Object.hasOwnProperty(key)) {
path = path.replace(':' + key, params[key]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server/controller/RoomController.js
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ RoomController.prototype.onName = function(client, data, callback)
var player = client.players.getById(data.player),
name = data.name.substr(0, Player.prototype.maxLength);

if (this.room.isNameAvailable(name)) {
if (player && this.room.isNameAvailable(name)) {
player.setName(name);
callback({success: true, name: player.name});
this.socketGroup.addEvent('player:name', { player: player.id, name: player.name });
Expand Down
4 changes: 4 additions & 0 deletions src/server/controller/RoomsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ RoomsController.prototype.onCreateRoom = function(client, data, callback)
var room = this.repository.create(data.name);

callback(room ? {success: true, room: room.serialize(false)} : {success: false});

if (room) {
this.emit('room:new', {room: room});
}
};

/**
Expand Down
217 changes: 193 additions & 24 deletions src/server/core/Inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,160 @@
* Inspector
*
* @param {Server} server
* @param {Object} config
*/
function Inspector (server, logDir)
function Inspector (server, config)
{
this.server = server;
this.path = path.resolve(logDir);
this.client = influx(config);

this.onGameNew = this.onGameNew.bind(this);
this.onGameRound = this.onGameRound.bind(this);
this.onGameEnd = this.onGameEnd.bind(this);
this.trackers = {
client: new Collection(),
room: new Collection(),
game: new Collection()
};

this.server.roomController.on('game:new', this.onGameNew);
this.onClientOpen = this.onClientOpen.bind(this);
this.onClientClose = this.onClientClose.bind(this);
this.onRoomOpen = this.onRoomOpen.bind(this);
this.onRoomClose = this.onRoomClose.bind(this);
this.onGameNew = this.onGameNew.bind(this);
this.onGameEnd = this.onGameEnd.bind(this);
this.onGameFPS = this.onGameFPS.bind(this);
this.onLog = this.onLog.bind(this);
this.logUsage = this.logUsage.bind(this);

this.server.on('client', this.onClientOpen);
this.server.roomRepository.on('room:open', this.onRoomOpen);
this.server.roomRepository.on('room:close', this.onRoomClose);

this.client.writePoint(this.CLIENTS, { value: this.server.clients.count() });
this.client.writePoint(this.ROOMS, { value: this.server.roomRepository.rooms.count() });

this.logInterval = setInterval(this.onLog, this.logFrequency);
}

Inspector.prototype.CLIENT = 'client';
Inspector.prototype.CLIENTS = 'client.total';
Inspector.prototype.CLIENT_PLAYER = 'client.player';
Inspector.prototype.CLIENT_GAME_PLAYER = 'client.game.player';
Inspector.prototype.ROOM = 'room';
Inspector.prototype.ROOMS = 'room.total';
Inspector.prototype.GAME = 'game';
Inspector.prototype.GAME_FPS = 'game.fps';
Inspector.prototype.USAGE_MEMORY = 'usage.memory';
Inspector.prototype.USAGE_CPU = 'usage.cpu';

/**
* On game add
* Usage log frequency
*
* @param {Game} game
* @type {Number}
*/
Inspector.prototype.onGameNew = function(data)
Inspector.prototype.logFrequency = 1000;

/**
* On client open
*
* @param {SocketClient} client
*/
Inspector.prototype.onClientOpen = function(client)
{
var game = data.game;
this.client.writePoint(this.CLIENTS, { value: this.server.clients.count() });

game.on('round:new', this.onGameRound);
game.on('end', this.onGameEnd);
client.on('close', this.onClientClose);

this.log(game, 'New game ' + game.name + '.');
this.trackers.client.add(new ClientTracker(this, client));
};

/**
* On game round
* On client close
*
* @param {SocketClient} client
*/
Inspector.prototype.onClientClose = function(client)
{
var tracker = this.trackers.client.getById(client.id);

this.client.writePoint(this.CLIENTS, { value: this.server.clients.count() });

if (tracker) {
this.client.writePoint(this.CLIENT, tracker.serialize());
this.trackers.client.remove(tracker.destroy());
}
};

/**
* On room open
*
* @param {Object} data
*/
Inspector.prototype.onRoomOpen = function(data)
{
var room = data.room;

this.trackers.room.add(new RoomTracker(this, room));

this.client.writePoint(this.ROOMS, { value: this.server.roomRepository.rooms.count() });

room.on('game:new', this.onGameNew);
};

/**
* On room open
*
* @param {Object} data
*/
Inspector.prototype.onRoomClose = function(data)
{
var room = data.room,
game = room.game,
tracker = this.trackers.room.getById(room.name);

room.removeListener('game:new', this.onGameNew);

this.client.writePoint(this.ROOMS, { value: this.server.roomRepository.rooms.count() });

if (game) {
this.onGameAbort(game);
}

if (tracker) {
this.client.writePoint(this.ROOM, tracker.serialize());
this.trackers.room.remove(tracker.destroy());
}
};

/**
* On game add
*
* @param {Game} game
*/
Inspector.prototype.onGameRound = function(data)
Inspector.prototype.onGameNew = function(data)
{
var game = data.game;
var game = data.game,
tracker = new GameTracker(this, game),
avatar, client;

if (game.rounds === 0) {
this.log(game, 'Game starts with ' + game.avatars.count() + ' players.');
this.trackers.game.add(tracker);

for (var i = game.avatars.items.length - 1; i >= 0; i--) {
avatar = game.avatars.items[i];
client = avatar.player.client;
clientTracker = this.trackers.client.getById(client.id);

this.client.writePoint(
this.CLIENT_GAME_PLAYER,
{
game: tracker.uniqId,
client: clientTracker.uniqId,
player: avatar.name,
color: avatar.color
}
);
}

tracker.on('fps', this.onGameFPS);
game.on('end', this.onGameEnd);
};

/**
Expand All @@ -51,17 +165,72 @@ Inspector.prototype.onGameRound = function(data)
*/
Inspector.prototype.onGameEnd = function(data)
{
var game = data.game;
var game = data.game,
tracker = this.trackers.game.getById(game.name);

game.removeListener('end', this.onGameEnd);

this.log(game, 'Game won in ' + game.rounds + ' rounds.');
if (tracker) {
tracker.removeListener('fps', this.onGameFPS);
this.collectGameTrackerData(tracker);
}
};

/**
* Log
* On game FPS
*
* @param {Object} data
*/
Inspector.prototype.onGameFPS = function(data)
{
this.client.writePoint(this.GAME_FPS, {
value: data.fps,
game: data.tracker.uniqId
});
};

/**
* On game is aborted
*
* @param {Game} game
*/
Inspector.prototype.log = function(game, data)
Inspector.prototype.onGameAbort = function(game)
{
var tracker = this.trackers.game.getById(game.name);

game.removeListener('end', this.onGameEnd);

if (tracker) {
this.collectGameTrackerData(tracker);
}
};

/**
* Collect data from the given tracker
*
* @param {GameTracker} tracker
*/
Inspector.prototype.collectGameTrackerData = function(tracker)
{
fs.appendFile(this.path + '/' + game.name + '.log', new Date() + ': ' + data + '\r\n', function (e) {});
};
this.client.writePoint(this.GAME, tracker.serialize());
this.trackers.game.remove(tracker.destroy());
};

/**
* On every frame
*/
Inspector.prototype.onLog = function()
{
usage.lookup(process.pid, this.logUsage);
};

/**
* Log usage
*/
Inspector.prototype.logUsage = function (err, result)
{
if (result) {
this.client.writePoint(this.USAGE_CPU, { value: result.cpu });
this.client.writePoint(this.USAGE_MEMORY, { value: result.memory });
}
};
6 changes: 6 additions & 0 deletions src/server/core/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
*/
function Server(config)
{
EventEmitter.call(this);

this.config = config;
this.app = express();
this.server = new http.Server(this.app);
Expand All @@ -25,6 +27,9 @@ function Server(config)
console.info('Listening on: %s', config.port);
}

Server.prototype = Object.create(EventEmitter.prototype);
Server.prototype.constructor = Server;

/**
* Authorization Handler
*
Expand Down Expand Up @@ -55,6 +60,7 @@ Server.prototype.onSocketConnection = function(socket)

client.addEvent('open', client.id);

this.emit('client', client);
console.info('Client connected', client.id);
};

Expand Down
1 change: 0 additions & 1 deletion src/server/core/SocketClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ function SocketClient(socket, interval)
BaseSocketClient.call(this, socket, interval);

this.id = null;
this.room = null;
this.players = new Collection([], 'id');
}

Expand Down
4 changes: 3 additions & 1 deletion src/server/dependencies.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
var EventEmitter = require('events').EventEmitter,
usage = require('usage'),
http = require('http'),
fs = require('fs'),
path = require('path'),
express = require('express'),
WebSocket = require('faye-websocket');
WebSocket = require('faye-websocket'),
influx = require('influx');
21 changes: 19 additions & 2 deletions src/server/launcher.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
var server = new Server({ port: 8080 });
var config;

//new Inspector(server, 'stats');
try {
config = require('../config.json');
} catch (error) {
config = {
port: 8080,
inspector: { enabled: false }
};
}

var server = new Server({ port: config.port });

if (config.inspector.enabled) {
//try {
new Inspector(server, config.inspector);
/*} catch (error) {
console.error('Inspector error:', error);
}*/
}

module.exports = server;
Loading

0 comments on commit 41528f1

Please sign in to comment.