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-10-20 21:39:33 +00:00
|
|
|
function addNewClient(client, clientSock) {
|
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 } );
|
|
|
|
|
2015-10-22 04:51:35 +00:00
|
|
|
var connInfo = {
|
|
|
|
ip : clientSock.remoteAddress,
|
2015-10-22 06:03:18 +00:00
|
|
|
serverName : client.session.serverName,
|
|
|
|
isSecure : client.session.isSecure,
|
2015-10-22 04:51:35 +00:00
|
|
|
};
|
2015-08-05 04:35:59 +00:00
|
|
|
|
|
|
|
if(client.log.debug()) {
|
2015-10-20 21:39:33 +00:00
|
|
|
connInfo.port = clientSock.localPort;
|
|
|
|
connInfo.family = clientSock.localFamily;
|
2015-08-05 04:35:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
*/
|