-
Notifications
You must be signed in to change notification settings - Fork 5
/
connection-manager.js
174 lines (140 loc) · 5.25 KB
/
connection-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright 2014 Fitbit, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
'use strict';
var connection = require('./connection');
var config = require('./config');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
function ConnectionManager () {
EventEmitter.call(this);
this.allConns = {};
this.userSessions = {};
}
util.inherits(ConnectionManager, EventEmitter);
// Add a connection to the connection manager
ConnectionManager.prototype.addConnection = function (conn) {
this.allConns[conn.hashId] = conn;
};
// Register an authenticated connection with a user session
ConnectionManager.prototype.registerConnection = function (conn) {
if (!conn) throw new Error('missing connection');
if (!conn.isConnected()) return;
var userId = conn.getAuthorizedUserId();
if (!userId) throw new Error('connection does not have an authorized user id');
var session = this.userSessions[userId];
if (!session) {
session = this.userSessions[userId] = {
userId: userId,
toolConn: null,
deviceConns: []
};
}
if (conn.isDevice) {
session.deviceConns.push(conn);
if (session.toolConn) {
// If a tool is connected, hook it up
// First disconnect old peer
var oldDevicePeer = session.toolConn.getPeer();
if (oldDevicePeer && oldDevicePeer !== conn) {
oldDevicePeer.setPeer(null);
}
session.toolConn.setPeer(conn);
conn.setPeer(session.toolConn);
}
// If there's too many device connections, close the oldest
if (session.deviceConns.length > config.maxDevicesPerAccount) {
var excessDeviceConn = session.deviceConns.shift();
excessDeviceConn.close(connection.CloseCode.ConnectionReplaced);
}
} else {
// Only one tool connection can be open at a time
if (session.toolConn) {
//console.warn("another tool connecting to same account; closing first");
session.toolConn.close(connection.CloseCode.ConnectionReplaced);
session.toolConn = null;
}
session.toolConn = conn;
// If there's any devices, connect the *last* one (most recent)
if (session.deviceConns.length > 0) {
var deviceConn = session.deviceConns[session.deviceConns.length - 1];
conn.setPeer(deviceConn);
deviceConn.setPeer(conn);
}
}
// Emit event for unit tests
this.emit('connection:add', conn);
};
// Remove a connection from the connection manager and from any user sessions
ConnectionManager.prototype.removeConnection = function (conn) {
delete this.allConns[conn.hashId];
var session = this.userSessions[conn.getAuthorizedUserId()];
if (session) {
if (conn.isDevice) {
session.deviceConns = session.deviceConns.filter(function (c) {
return c !== conn;
});
} else {
session.toolConn = null;
}
var peer = conn.getPeer();
if (peer) {
peer.setPeer(null);
}
conn.setPeer(null);
if (!session.toolConn && session.deviceConns.length === 0) {
// No connections left; delete the record
delete this.userSessions[session.userId];
}
}
// Emit event for unit tests
this.emit('connection:remove', conn);
};
ConnectionManager.prototype.getAllConnections = function () {
var conns = [];
for (var hashId in this.allConns) {
conns.push(this.allConns[hashId]);
}
return conns;
};
ConnectionManager.prototype.getNumberOfConnections = function () {
var count = 0;
/*jshint unused:false */
for (var key in this.allConns) {
count++;
}
return count;
};
ConnectionManager.prototype.getNumberOfAuthorizedConnections = function () {
var count = 0;
for (var userId in this.userSessions) {
var session = this.userSessions[userId];
if (session.toolConn) count++;
count += session.deviceConns.length;
}
return count;
};
module.exports = new ConnectionManager();