-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinter-node.js
More file actions
executable file
·272 lines (257 loc) · 9.01 KB
/
inter-node.js
File metadata and controls
executable file
·272 lines (257 loc) · 9.01 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
var g = require('./global.js'),
api = require('./api.js');
function genid() {
// https://github.com/defunctzombie/node-uuid
// v4 - based on random numbers
// The original from broofa has been known to generate duplicates of v4 uuids!
return require('uuid').v4();
}
module.exports = function (redis, conf, id) {
var self = this;
var client0 = redis.createClient(conf.redis.port, conf.redis.host, { }),
writer = redis.createClient(conf.redis.port, conf.redis.host, { }),
uuid = g.isset(id) ? id : genid();
g.debug('This server has been assigned a uuid of ' + uuid);
var servers = { };
// Initialize the servers object with our own information
servers[uuid] = {
// Can be empty because we won't be using here
}
// When a server dies, the remaining servers must divy up the work
var _serverdeath = function (serverId) {
delete servers[serverId];
var active = Object.keys(servers);
// We must sort all servers by serverId to gaurantee that all sockets are handled by one of us
active.sort();
var socs = Object.keys(g.sockets[serverId].connected);
delete g.sockets[serverId];
socs.sort();
for (var i = 0; i < socs.length; i++) {
var clientId = socs[i];
if (!g.isconnected(clientId)) {
// Every server must expire access token if all client instances have disconnected
delete g.tokens[clientId];
// Then check to see if we are responsible for notifying trackers of changes in status
var serverSubstitute = active[i % active.length];
if (uuid == serverSubstitute) {
api.notify_status(clientId, 'disconnected');
}
}
}
};
// Increasing activeInterval to 3000 ms because 1000 ms may have been too short. In one incident, missed heartbeats (due to network
// latency?) caused all servers to believe the other had disconnected then reconnected in under 1 second.
var activeInterval = 3000; // ms
this.checkIfServerActive = function (uuid) {
var socs = g.sockets[uuid];
if (!g.isset(socs)) {
g.error('Error - checking if non-existent server {0} is active'.format(uuid));
return;
}
if ((new Date).getTime() - socs.heartbeatTs >= activeInterval) {
// dead
g.debug('Server {0} disconnected'.format(uuid));
_serverdeath(uuid);
} else {
//console.log(' checking if {0} active - yes'.format(uuid));
setTimeout(function () { self.checkIfServerActive(uuid); }, activeInterval);
}
};
// connect socket
this.csocket = function (clientId, sid, token) {
// create a socket connection event
var event = { uuid: uuid, clientId: clientId, sid: sid, connected: true, 'token': token };
_processSocketEvent(event);
// Let
writer.publish('soc-info', JSON.stringify(event));
};
// disconnect socket
this.dsocket = function (clientId, sid) {
var event = { uuid: uuid, clientId: clientId, sid: sid, connected: false };
_processSocketEvent(event);
writer.publish('soc-info', JSON.stringify(event));
};
var _processSocketEvent = function (o) {
var serverId = o.uuid,
clientId = o.clientId,
sid = o.sid,
connected = o.connected,
token = o.token;
if (connected) {
// If we are the first server to see a client connect, it is our responsibility to notify trackers
if (!g.isconnected(clientId) && serverId == uuid) {
console.log(' notifying status - {0}, connected'.format(clientId));
api.notify_status(clientId, 'connected');
}
console.log(' persisting connected - {0}, {1}, {2}'.format(serverId, clientId, sid));
g.setSocketId(serverId, clientId, sid);
if (g.isset(token)) {
console.log( 'token - {0}, {1}'.format(clientId, token));
g.tokens[clientId] = token;
}
} else {
console.log(' persisting disconnected - {0}, {1}, {2}'.format(serverId, clientId, sid));
g.removeSocketId(serverId, clientId, sid);
if (!g.isconnected(clientId)) {
console.log(' deleting g.tokens[{0}]'.format(clientId));
delete g.tokens[clientId];
if (o.uuid == uuid) {
// Notify subscribers if all user instances have disconnected
console.log(' notifying status - {0}, disconnected'.format(o.clientId));
api.notify_status(clientId, 'disconnected');
}
}
}
};
client0.on('message', function (channel, msg) {
switch (channel) {
case 'sigint':
var serverId = msg;
console.log(' got sigint from server {0} - treating as disconnect! There may be an error on the next checkIfServerActive'.format(serverId));
_serverdeath(serverId);
break;
case 'node-heartbeat':
var serverId = msg;
if (serverId == uuid) {
// Ignore our own heartbeats
return;
}
//console.log(' heartbeat - ' + serverId);
if (!g.isset(g.sockets[serverId])) {
g.debug('Server {0} connected'.format(serverId));
// new server connected
servers[serverId] = { ready: false, queue: [ ] };
// request socket states from server
console.log(' >> {0} get-connected-sockets'.format(serverId));
var p = {
from: uuid, subject: 'cmd', body: 'get-connected-sockets'
};
writer.publish(serverId + '-inbox', JSON.stringify(p));
setTimeout(function () { self.checkIfServerActive(serverId); }, activeInterval);
}
g.ackServerHeartbeat(serverId);
break;
case String(uuid) + '-inbox':
var push = JSON.parse(msg),
from = push.from,
subject = push.subject,
body = push.body;
switch (subject) {
case 'cmd':
if (body == 'get-connected-sockets') {
//console.log(' << {0} get-connected-sockets'.format(from));
var socs = { };
if (g.isset(g.sockets[uuid])) { socs = g.sockets[uuid].connected; }
var p = {
from: uuid, subject: 'connected-sockets', body: socs,
};
writer.publish(from + '-inbox', JSON.stringify(p));
} else {
g.debug('warning - received unrecognized cmd {0} from server {1}'.format(body, from));
}
break;
case 'connected-sockets':
var cnt = Object.keys(body).length;
console.log(' << {0} got {1} clients'.format(from, cnt));
if (!g.empty(g.sockets[from].connected)) {
g.debug('Warning - g.sockets[serverId] is not empty but about to overwrite');
}
g.sockets[from].connected = body;
servers[from].ready = true;
var event = servers[from].queue.pop();
while (event != null) {
_processSocketEvent(event);
event = servers[from].queue.pop();
}
break;
default:
g.debug('warning - unknown subject ' + subject);
}
break;
case 'soc-info':
var obj = JSON.parse(msg),
serverId = obj.uuid,
clientId = obj.clientId,
sid = obj.sid,
connected = obj.connected;
// Ignore our own socket events
if (serverId == uuid) {
//var s = connected ? 'connected' : 'disconnected';
//console.log(' ignoring socket event from us - {0} {1}'.format(clientId, s));
return;
}
if (!g.isset(servers[serverId])) {
// we're getting socket information but haven't received the server's heartbeat yet
return;
}
if (!servers[serverId].ready) {
servers[serverId].queue.push(obj);
return;
}
_processSocketEvent(obj);
break;
case 'soc-action':
var obj = JSON.parse(msg),
slave = obj.slave,
master = obj.master;
var sids = g.getSocketIds(uuid, slave);
var joined = false;
for (var i = 0; i < sids.length; i++) {
var soc = g.io.sockets.connected[sids[i]];
if (soc.ready) {
joined = true;
var room = g.roomTrackers(master);
soc.join(room);
}
}
if (joined) {
g.debug(' {0} joining {1}'.format(slave, room));
}
break;
case 'soc-action-untrack':
var obj = JSON.parse(msg),
slave = obj.slave,
master = obj.master;
var sids = g.getSocketIds(uuid, slave);
for (var i = 0; i < sids.length; i++) {
var soc = g.io.sockets.connected[sids[i]];
var room = g.roomTrackers(master);
soc.leave(room);
}
g.debug(' {0} left {1}'.format(slave, room));
break;
default:
g.debug('warning - msg event on unrecognized channel ' + channel);
}
});
this.signaldeath = function () {
writer.publish('sigint', uuid);
};
this.broadcastTrack = function (slave, master, b) {
obj = {
'slave': slave, 'master': master,
}
//g.debug(b);
if (b) {
g.debug('here emitting untrack eevent');
writer.publish('soc-action-untrack', JSON.stringify(obj));
} else {
writer.publish('soc-action', JSON.stringify(obj));
}
};
client0.subscribe('sigint');
client0.subscribe('node-heartbeat');
client0.subscribe(String(uuid) + '-inbox');
client0.subscribe('soc-info');
client0.subscribe('soc-action');
client0.subscribe('soc-action-untrack');
var heartbeatInterval = 500; // ms
this.beat = function () {
//console.log(' heartbeat');
writer.publish('node-heartbeat', uuid);
setTimeout(function () { self.beat(); }, heartbeatInterval);
};
g.debug('Starting heartbeat every {0} ms'.format(heartbeatInterval));
this.beat();
return this;
};