Permalink
Browse files

mechanics.socket.Connections and related stuff removed

  • Loading branch information...
1 parent 1cccc6c commit 7c15bd750043638cb5b00bf761efedb38d9dd56b @dimsmol committed Feb 17, 2013
View
@@ -37,7 +37,6 @@ For any app, if it cannot be found, apis default will be used.
* core.logging.engines.syslog - syslog logging engine
* core.mechanics.web - web mechanics, enables responding on HTTP requests
* core.mechanics.socket - socket mechanics, enables web socket communications, runs on top of web mechanics
- * core.mechanics.socket.connections - web socket connections tracker
* core.mechanics.socket.stat - web socket statistics
* core.settings - settings, will be searched at cwd()+'/lib/settings'
View
@@ -7,7 +7,6 @@
* think about combining all apis headers into single http header - be careful, can have single row length issues
* remove framerequest
* cleanup http mechanics
-* remove Connections, DoubleDict, etc.
* make event names camelCase ? (check guidelines)
* make Loader command set easier to extend
View
@@ -75,8 +75,7 @@ Loader.prototype.standardUnitPaths = {
// sockets
'core.mechanics.socket': 'mechanics/socket/mechanics',
- 'core.mechanics.socket.connections': 'mechanics/socket/connections',
- 'core.mechanics.socket.stat': 'mechanics/socket/stat/stat',
+ 'core.mechanics.socket.stat': 'mechanics/socket/stat/stat'
};
Loader.prototype.unitPaths = {
@@ -1,108 +0,0 @@
-"use strict";
-var inherits = require('util').inherits;
-var EventEmitter = require('events').EventEmitter;
-var ConnectionsDict = require('./connections_dict');
-
-
-var Connections = function () {
- EventEmitter.call(this);
-
- this.connections = {};
- this.connectionCount = 0;
-
- this.userConnections = new ConnectionsDict();
- this.endpointConnections = new ConnectionsDict();
-};
-inherits(Connections, EventEmitter);
-
-Connections.prototype.unitInit = function (units) {
- var mechanics = units.require('core.mechanics.socket');
- var self = this;
- mechanics.on('connect', function (connection) {
- self.add(connection);
- });
- mechanics.on('disconnect', function (connection) {
- self.remove(connection);
- });
-};
-
-Connections.prototype.isPresent = function (userId) {
- return userId in this.userConnections.dict;
-};
-
-Connections.prototype.getEndpoint = function (connection) {
- return connection.userData.endpointInfo;
-};
-
-Connections.prototype.registerEndpoint = function (connection, endpointInfo) {
- this.emit('endpoint_register', connection, endpointInfo);
-
- this.unregisterEndpoint(connection); // one endpoint per connection please
-
- connection.userData.endpointInfo = endpointInfo;
-
- if (endpointInfo.userId != null) {
- this.userConnections.add(endpointInfo.userId, connection);
- }
-
- var endpointKey = this.getEndpointKey(endpointInfo);
- this.endpointConnections.add(endpointKey, connection);
-};
-
-Connections.prototype.unregisterEndpoint = function (connection) {
- var endpointInfo = connection.userData.endpointInfo;
-
- if (endpointInfo != null)
- {
- if (endpointInfo.userId != null)
- {
- this.userConnections.remove(endpointInfo.userId, connection);
- }
-
- if (endpointInfo.endpointId != null)
- {
- var endpointKey = this.getEndpointKey(endpointInfo);
- this.endpointConnections.remove(endpointKey, connection);
- }
-
- this.emit('endpoint_unregistered', connection, endpointInfo);
- }
-};
-
-Connections.prototype.getEndpointKey = function (endpointInfo) {
- return [endpointInfo.userId, endpointInfo.endpointId].join('\n');
-};
-
-Connections.prototype.add = function (connection) {
- connection.userData = connection.userData || {};
- connection.userData.startTime = Date.now();
- var id = connection.id;
- if (!(id in this.connections))
- {
- this.connectionCount++;
- }
- this.connections[id] = connection;
-};
-
-Connections.prototype.remove = function (connection) {
- var id = connection.id;
- if (id in this.connections)
- {
- this.connectionCount--;
- delete this.connections[id];
- }
-
- this.unregisterEndpoint(connection);
-};
-
-Connections.prototype.getUserConnections = function (userId) {
- return this.userConnections.get(userId);
-};
-
-Connections.prototype.getEndpointConnections = function (endpointInfo) {
- var endpointKey = this.getEndpointKey(endpointInfo);
- return this.endpointConnections.get(endpointKey);
-};
-
-
-module.exports = Connections;
@@ -1,22 +0,0 @@
-"use strict";
-var inherits = require('util').inherits;
-var DoubleDict = require('../../tools/double_dict');
-
-
-var ConnectionsDict = function() {
- ConnectionsDict.super_.call(this);
-};
-inherits(ConnectionsDict, DoubleDict);
-
-ConnectionsDict.prototype.add = function(key, connection) {
- var subKey = connection.id;
- ConnectionsDict.super_.prototype.add.call(this, key, subKey, connection);
-};
-
-ConnectionsDict.prototype.remove = function(key, connection) {
- var subKey = connection.id;
- ConnectionsDict.super_.prototype.remove.call(this, key, subKey);
-};
-
-
-module.exports = ConnectionsDict;
@@ -1,17 +1,13 @@
"use strict";
var Mechanics = require('./mechanics');
var Transport = require('./transport');
-var Connections = require('./connections');
-var ConnectionsDict = require('./connections_dict');
var stat = require('./stat');
var Request = require('./request');
module.exports = {
Mechanics: Mechanics,
Transport: Transport,
- Connections: Connections,
- ConnectionsDict: ConnectionsDict,
stat: stat,
Request: Request
};
@@ -9,12 +9,10 @@ var SocketStat = function () {
this.freq = null;
this.per = null;
this.old = null;
- this.connections = null;
};
inherits(SocketStat, Unit);
SocketStat.prototype.unitInit = function (units) {
- this.connections = units.get('core.connections');
var coreSettings = units.require('core.settings').core;
var mechanics = units.require('core.mechanics.socket');
@@ -53,170 +51,16 @@ SocketStat.prototype.unitInit = function (units) {
SocketStat.prototype.getStat = function () {
var result = {};
-
- this.updateResult(result, 'connections', this.getConnectionsStat());
this.updateResult(result, 'socket', this.getSocketStat());
-
return result;
};
SocketStat.prototype.updateResult = function (result, key, value) {
- if (value != null)
- {
+ if (value != null) {
result[key] = value;
}
};
-SocketStat.prototype.getConnectionsStat = function () {
- var result = null;
-
- var connections = this.connections;
- if (connections != null)
- {
- result = {
- counts: {
- connection: connections.connectionCount,
- user: connections.userConnections.count,
- endpoint: connections.endpointConnections.count
- }
- };
-
- var noEndpointCount = 0;
- var noEndpointOldCount = 0;
- var now = Date.now();
- var count = 0;
- var totalLifetime = 0;
- var oldest = null;
- var byTransport = {};
- for (var k in connections.connections)
- {
- count++;
- var connection = connections.connections[k];
- var connectionStartTime = connection.userData.startTime;
- var connectionTime = now - connectionStartTime;
-
- totalLifetime += connectionTime;
- if (oldest == null || oldest > connectionStartTime) {
- oldest = connectionStartTime;
- }
-
- var endpointInfo = connections.getEndpoint(connection);
- if (endpointInfo == null || endpointInfo.endpointId == null)
- {
- noEndpointCount++;
- if (connectionTime >= this.old) {
- noEndpointOldCount++;
- }
- }
-
- var protocol = this.getProtocol(connection);
- var byTransportCount = byTransport[protocol];
- byTransport[protocol] = (byTransportCount == null ? 1 : byTransportCount + 1);
- }
-
- result.counts.connectionWithoutEndpoint = {
- total: noEndpointCount,
- old: noEndpointOldCount
- };
- result.counts.byTransport = byTransport;
-
- if (oldest != null) {
- result.lifetime = {
- longest: {
- started: new Date(oldest),
- duration: now - oldest
- },
- medium: Math.round(totalLifetime / count)
- };
- }
-
- var endpointConnectionsStat = this.getEndpointConnectionsStat(connections.endpointConnections);
- if (endpointConnectionsStat != null)
- {
- result.counts.endpointWithManyConnections = endpointConnectionsStat;
- }
- }
-
- return result;
-};
-
-SocketStat.prototype.getEndpointConnectionsStat = function (endpointConnections) {
- var result = {
- count: 0
- };
- var transports = {};
- var found = false;
- for (var k in endpointConnections.dict)
- {
- var singleEndpointConnections = endpointConnections.get(k);
- var count = 0;
- var byTransport = {};
- for (var j in singleEndpointConnections)
- {
- count++;
- var connection = singleEndpointConnections[j];
- var protocol = this.getProtocol(connection);
- var byTransportCount = byTransport[protocol];
- byTransport[protocol] = (byTransportCount == null ? 1 : byTransportCount + 1);
- }
- if (count > 1)
- {
- found = true;
- result.count++;
- var hash = this.getTransportsHash(byTransport);
- var byHash = transports[hash];
- if (byHash == null)
- {
- transports[hash] = {
- transports: byTransport,
- count: 1
- };
- }
- else
- {
- byHash.count++;
- }
- }
- }
- if (found)
- {
- var combinations = [];
- for (var i in transports)
- {
- combinations.push(transports[i]);
- }
- result.combinations = combinations;
- }
- else
- {
- result = null;
- }
- return result;
-};
-
-SocketStat.prototype.getTransportsHash = function (transportCounts) {
- var arr = [];
- for (var transport in transportCounts)
- {
- arr.push([
- transport,
- transportCounts[transport]
- ].join('\n'));
- }
- return arr.sort().join('\n\n');
-};
-
-SocketStat.prototype.getProtocol = function (connection) {
- // TODO replace code below with connection.protocol when sockjs will be updated
- var recv = connection._session.recv;
- var protocol = null;
- if (recv != null)
- {
- protocol = recv.protocol;
- }
- return protocol || 'unknown';
-};
-
SocketStat.prototype.getSocketStat = function () {
return {
frameSize: this.freq.frameSize,
Oops, something went wrong.

0 comments on commit 7c15bd7

Please sign in to comment.