-
Notifications
You must be signed in to change notification settings - Fork 0
/
TransferRateMonitor.server.js
120 lines (108 loc) · 3.11 KB
/
TransferRateMonitor.server.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
/**
* API for the server.
*
* @type {TransferRateMonitor}
*/
class TransferRateMonitor extends TransferRateMonitorCommon {
constructor() {
super();
this._callbacks = [];
this._options = {
maxSubscriptions: 10,
allowedUsers: null,
password: 'giveMeStats'
};
this._protocol = new TransferRateProtocol();
}
/**
* Sets new options.
* @param {Object} options - Object with options.
*/
configure(options) {
// TODO: validation
_.extend(this._options, options);
}
/**
* Getter for server transfer rate.
* @returns {{rateIn: *, rateOut: *, messagesIn: *, messagesOut: *}|*}
*/
getTransferRate() {
return this._currentTransferRate;
}
/**
* Calculates the rates per second and send the stats to all subscribed clients.
*
* @private
*/
_calculateCurrentTransferRate() {
this._calculateCurrentTransferRateCore();
this._protocol.send(
this._protocol.SERVER_TRANSFER_RATE_MESSAGE,
Object.keys(this._currentTransferRate).map(key => this._currentTransferRate[key]),
this._callbacks,
true
);
}
/**
* Subscribes client for receiving serve stats.
*
* @param {string} id - Meteor session id.
*/
registerConnection(id) {
if (this._callbacks.length < this._options.maxSubscriptions) {
this._callbacks.push(id);
} else {
throw new Error('Too many subscriptions.');
}
}
/**
* Unsubscribes client.
* @param {string} id - Meteor session id.
*/
unregisterConnection(id) {
if (~this._callbacks.indexOf(id)) {
this._callbacks.splice(this._callbacks.indexOf(id), 1);
}
}
/**
* Wraps direct stream's send method.
* @private
*/
_replaceDirectStreamAccessSend() {
const originalSend = Meteor.directStream.send;
Meteor.directStream.send = (message, sessionId) => {
this.bytesOut += message.length;
originalSend.call(Meteor.directStream, message, sessionId);
};
}
/**
* Checks if user is on the allowedUsers list.
*
* @param {string} password
* @param {int} userId
* @returns {boolean}
*/
isUserAuthorized(password, userId) {
if (this._options.password !== password) return false;
if (this._options.allowedUsers === null) return true;
return !!~this._options.allowedUsers.indexOf(userId);
}
}
transferRateMonitor = new TransferRateMonitor();
/**
* Publication that will gather session ids that are subscribed to stats from server.
*/
Meteor.publish('__serverTransferRate', function transferRate(password) {
if (!transferRateMonitor.isUserAuthorized(password, this.userId)) {
this.stop();
return;
}
this.onStop(() => {
transferRateMonitor.unregisterConnection(this.connection.id);
});
try {
transferRateMonitor.registerConnection(this.connection.id);
} catch (exception) {
this.stop();
}
});