2015-08-05 04:35:59 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var logger = require('./logger.js');
|
|
|
|
|
2016-01-30 22:18:55 +00:00
|
|
|
var _ = require('lodash');
|
|
|
|
var moment = require('moment');
|
|
|
|
|
2015-10-18 02:03:51 +00:00
|
|
|
exports.getActiveConnections = getActiveConnections;
|
2016-01-30 22:18:55 +00:00
|
|
|
exports.getActiveNodeList = getActiveNodeList;
|
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() {
|
2015-11-26 01:03:47 +00:00
|
|
|
return clientConnections;
|
2015-10-18 02:03:51 +00:00
|
|
|
}
|
|
|
|
|
2016-01-30 22:18:55 +00:00
|
|
|
function getActiveNodeList() {
|
|
|
|
const now = moment();
|
|
|
|
|
|
|
|
return _.map(getActiveConnections(), ac => {
|
|
|
|
let entry = {
|
|
|
|
node : ac.node,
|
|
|
|
authenticated : ac.user.isAuthenticated(),
|
|
|
|
userId : ac.user.userId,
|
|
|
|
action : _.has(ac, 'currentMenuModule.menuConfig.desc') ? ac.currentMenuModule.menuConfig.desc : 'Unknown',
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// There may be a connection, but not a logged in user as of yet
|
|
|
|
//
|
|
|
|
if(ac.user.isAuthenticated()) {
|
|
|
|
entry.userName = ac.user.username;
|
|
|
|
entry.realName = ac.user.properties.real_name;
|
|
|
|
entry.location = ac.user.properties.location;
|
|
|
|
entry.affils = ac.user.properties.affiliation;
|
|
|
|
|
|
|
|
const diff = now.diff(moment(ac.user.properties.last_login_timestamp), 'minutes');
|
|
|
|
entry.timeOn = moment.duration(diff, 'minutes');
|
|
|
|
}
|
|
|
|
return entry;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
2016-01-02 04:17:13 +00:00
|
|
|
// Note that this will be updated @ login with additional information
|
2015-08-05 04:35:59 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
*/
|