2015-08-05 04:35:59 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var logger = require('./logger.js');
|
|
|
|
|
2015-10-18 02:03:51 +00:00
|
|
|
exports.getActiveConnections = getActiveConnections;
|
2015-08-05 04:35:59 +00:00
|
|
|
exports.addNewClient = addNewClient;
|
|
|
|
exports.removeClient = removeClient;
|
|
|
|
|
|
|
|
var clientConnections = [];
|
|
|
|
exports.clientConnections = clientConnections;
|
|
|
|
|
2015-10-18 02:03:51 +00:00
|
|
|
function getActiveConnections() {
|
|
|
|
return clientConnections.length;
|
|
|
|
}
|
|
|
|
|
2015-08-05 04:35:59 +00:00
|
|
|
function addNewClient(client) {
|
2015-09-26 05:10:18 +00:00
|
|
|
var id = client.session.id = clientConnections.push(client) - 1;
|
2015-08-05 04:35:59 +00:00
|
|
|
|
|
|
|
// Create a client specific logger
|
|
|
|
client.log = logger.log.child( { clientId : id } );
|
|
|
|
|
|
|
|
var connInfo = { ip : client.input.remoteAddress };
|
|
|
|
|
|
|
|
if(client.log.debug()) {
|
|
|
|
connInfo.port = client.input.localPort;
|
|
|
|
connInfo.family = client.input.localFamily;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.log.info(connInfo, 'Client connected');
|
|
|
|
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeClient(client) {
|
|
|
|
client.end();
|
|
|
|
|
|
|
|
var i = clientConnections.indexOf(client);
|
|
|
|
if(i > -1) {
|
|
|
|
clientConnections.splice(i, 1);
|
|
|
|
|
|
|
|
logger.log.info(
|
|
|
|
{
|
|
|
|
connectionCount : clientConnections.length,
|
2015-09-26 05:10:18 +00:00
|
|
|
clientId : client.session.id
|
2015-08-05 04:35:59 +00:00
|
|
|
},
|
|
|
|
'Client disconnected'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* :TODO: make a public API elsewhere
|
|
|
|
function getActiveClientInformation() {
|
|
|
|
var info = {};
|
|
|
|
|
|
|
|
clientConnections.forEach(function connEntry(cc) {
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|
|
|
|
*/
|