telemersive-bus 0.6.0 → 0.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/BusManager.js +37 -16
- package/lib/utils/BusTopics.js +1 -1
- package/package.json +1 -1
package/lib/BusManager.js
CHANGED
|
@@ -13,6 +13,9 @@ const {RoomAccess, RoomInfo, PeerCredentials, PeerInfo} = require('./protos/BusM
|
|
|
13
13
|
/* time the housekeeping thread waits until it continues with the next step
|
|
14
14
|
*/
|
|
15
15
|
const houseKeepingTimeOut = 500;
|
|
16
|
+
const houseKeepingTimeOut_onRepeat = 1500;
|
|
17
|
+
const houseKeepingTimeOut_onPeerLeft= 1000;
|
|
18
|
+
const houseKeepingTimeOut_onPeerJoined = 5000;
|
|
16
19
|
const ROOM_ID_MIN = 11;
|
|
17
20
|
const ROOM_ID_MAX = 50;
|
|
18
21
|
|
|
@@ -111,6 +114,9 @@ class BusManager {
|
|
|
111
114
|
|
|
112
115
|
/**
|
|
113
116
|
* start the housekeeping
|
|
117
|
+
* on startup
|
|
118
|
+
* when peer joins a room
|
|
119
|
+
* when peer leaves a room
|
|
114
120
|
*/
|
|
115
121
|
startHousekeeping = async () => {
|
|
116
122
|
// we dont want to start another housekeeping process until the previous one has finished
|
|
@@ -125,6 +131,10 @@ class BusManager {
|
|
|
125
131
|
// now we have to wait a moment
|
|
126
132
|
setTimeout(this.pingAllPeersAlive, houseKeepingTimeOut);
|
|
127
133
|
this.houseKeepingInProgress = true;
|
|
134
|
+
} else {
|
|
135
|
+
// if there is already a housekeeping going on,
|
|
136
|
+
// we wait for some time to finish it and try again.
|
|
137
|
+
setTimeout(this.startHousekeeping, houseKeepingTimeOut_onRepeat);
|
|
128
138
|
}
|
|
129
139
|
}
|
|
130
140
|
|
|
@@ -165,6 +175,10 @@ class BusManager {
|
|
|
165
175
|
}
|
|
166
176
|
// now we have to wait a moment
|
|
167
177
|
setTimeout(this.cleanOut, houseKeepingTimeOut);
|
|
178
|
+
} else {
|
|
179
|
+
// this means the housekeeping got interrupted and
|
|
180
|
+
// needs to finish as fast as possible to cleaning up all subscriptions.
|
|
181
|
+
setTimeout(this.cleanOut, 0);
|
|
168
182
|
}
|
|
169
183
|
}
|
|
170
184
|
|
|
@@ -238,6 +252,7 @@ class BusManager {
|
|
|
238
252
|
roomName: currentPeer.roomName
|
|
239
253
|
};
|
|
240
254
|
|
|
255
|
+
// sends the left message to all peers joined in the same room
|
|
241
256
|
await this.communicator.publish(
|
|
242
257
|
new BusMsgPub(
|
|
243
258
|
this.busTopics.roomPeerLeft(currentPeer.roomName, currentPeer.peerId).build(),
|
|
@@ -249,16 +264,18 @@ class BusManager {
|
|
|
249
264
|
const cleanupRooms = async (currentRoom) => await this.deleteRoom(currentRoom.roomName);
|
|
250
265
|
await Object.values(this.roomHousekeep).every(cleanupRooms);
|
|
251
266
|
|
|
252
|
-
// and we stop by unsubscribing to all peer info messages inside all rooms
|
|
253
|
-
await this.communicator.unsubscribe(this.busTopics.roomPeerJoin('+', '+').build());
|
|
254
|
-
// unsubscribing to all room ping alive topic
|
|
255
|
-
await this.communicator.unsubscribe(this.busTopics.baseRoomAlivePing("+").build());
|
|
256
|
-
// cleanup the communicator
|
|
257
|
-
this.communicator.clearAutoGeneratedSubscriptions();
|
|
258
267
|
console.log(` -> housekeeping DONE`);
|
|
259
268
|
} else {
|
|
260
269
|
console.log(` -> housekeeping was interrupted`);
|
|
261
270
|
}
|
|
271
|
+
|
|
272
|
+
// and we stop by unsubscribing to all peer info messages inside all rooms
|
|
273
|
+
await this.communicator.unsubscribe(this.busTopics.roomPeerJoin('+', '+').build());
|
|
274
|
+
// unsubscribing to all room ping alive topic
|
|
275
|
+
await this.communicator.unsubscribe(this.busTopics.baseRoomAlivePing("+").build());
|
|
276
|
+
// cleanup the communicator
|
|
277
|
+
this.communicator.clearAutoGeneratedSubscriptions();
|
|
278
|
+
// and just to be sure
|
|
262
279
|
this.houseKeepingInProgress = false;
|
|
263
280
|
}
|
|
264
281
|
|
|
@@ -270,13 +287,14 @@ class BusManager {
|
|
|
270
287
|
let shaPassword = sha1(_message.roomPwd).substr(0, 10)
|
|
271
288
|
if (this.rooms.hasOwnProperty(_message.roomName)){
|
|
272
289
|
if (this.rooms[_message.roomName].roomPwd === shaPassword){
|
|
273
|
-
|
|
274
|
-
//
|
|
290
|
+
// the actual message of this peer leaving the room is generated by the
|
|
291
|
+
// housekeeping routine 'cleanOut':
|
|
292
|
+
// we wait a moment to start with housekeeping, though.
|
|
275
293
|
// it could be, that this message was called due to a short disconnect by the
|
|
276
294
|
// peer to the broker - issuing a lastWill message by the broker.
|
|
277
295
|
// by giving it some time the housekeeping process will verify if the peer
|
|
278
|
-
// is still online or if it is gone..
|
|
279
|
-
setTimeout(this.startHousekeeping,
|
|
296
|
+
// is still online or if it is really gone..
|
|
297
|
+
setTimeout(this.startHousekeeping, houseKeepingTimeOut_onPeerLeft);
|
|
280
298
|
} else {
|
|
281
299
|
console.log(`!!! >BAD PLAYER<: somebody tried to remove peer without proper credentials!!!`);
|
|
282
300
|
}
|
|
@@ -327,13 +345,16 @@ class BusManager {
|
|
|
327
345
|
} else {
|
|
328
346
|
//room exists: checked if the doubled hashed roomPwd is equal
|
|
329
347
|
accessGrant = true;
|
|
330
|
-
|
|
331
|
-
|
|
348
|
+
console.log(` -> peer '${_message.peerName}' joining room '${_message.roomName}'`);
|
|
349
|
+
// if there is a housekeeping process in progress,
|
|
350
|
+
// we need it to stop it right here, otherwise the newly joined peer could be
|
|
351
|
+
// kicked out right away.
|
|
352
|
+
if (this.houseKeepingInProgress) {
|
|
353
|
+
console.log(` -> peer '${_message.peerName}' joining interrupts housekeeping.. `);
|
|
332
354
|
this.houseKeepingInProgress = false;
|
|
333
|
-
// and we wait a moment to start with houskeeping again
|
|
334
|
-
setTimeout(this.startHousekeeping, houseKeepingTimeOut);
|
|
335
355
|
}
|
|
336
|
-
|
|
356
|
+
// and we wait a moment to start with housekeeping again
|
|
357
|
+
setTimeout(this.startHousekeeping, houseKeepingTimeOut_onPeerJoined);
|
|
337
358
|
}
|
|
338
359
|
}
|
|
339
360
|
} else {
|
|
@@ -357,7 +378,7 @@ class BusManager {
|
|
|
357
378
|
|
|
358
379
|
if(accessGrant){
|
|
359
380
|
//console.log(`publish info to address: ${this.busTopics.roomPeerJoin(_message.roomName, _message.peerId).build()}.`);
|
|
360
|
-
// send peer joined message
|
|
381
|
+
// send peer joined message to all the peers in the room
|
|
361
382
|
await this.communicator.publish(
|
|
362
383
|
new BusMsgPub(
|
|
363
384
|
this.busTopics.roomPeerJoin(_message.roomName, _message.peerId).build(),
|
package/lib/utils/BusTopics.js
CHANGED
|
@@ -170,7 +170,7 @@ class BusTopics
|
|
|
170
170
|
|
|
171
171
|
/**
|
|
172
172
|
* setup topic for peer.
|
|
173
|
-
* iBus/rooms/'roomName'/peers/'peerId'/
|
|
173
|
+
* iBus/rooms/'roomName'/peers/'peerId'/left
|
|
174
174
|
*/
|
|
175
175
|
roomPeerLeft(_roomName, _peerId){
|
|
176
176
|
return this.roomPeer(_roomName, _peerId).appendTopic("left");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "telemersive-bus",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "MQTT based data protocol to manage unlimited peers, connected by rooms, where all peers joined to a room can exchange private data. It provides a simple chat mechanism, latency pinging, publish-subscribe mechanism (based on mqtt) and a OSC-like data stream. ",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|