telemersive-bus 0.6.2 → 0.6.3

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.
Files changed (2) hide show
  1. package/lib/BusManager.js +16 -13
  2. package/package.json +1 -1
package/lib/BusManager.js CHANGED
@@ -121,6 +121,7 @@ class BusManager {
121
121
  startHousekeeping = async () => {
122
122
  // we dont want to start another housekeeping process until the previous one has finished
123
123
  if(!this.houseKeepingInProgress){
124
+ this.houseKeepingInProgress = true;
124
125
  console.log(` -> starting housekeeping -> gather all peers ...`);
125
126
  this.peerHousekeep = {};
126
127
  this.roomHousekeep = Object.assign({}, this.rooms); // clone the current room list
@@ -130,7 +131,6 @@ class BusManager {
130
131
 
131
132
  // now we have to wait a moment
132
133
  setTimeout(this.pingAllPeersAlive, houseKeepingTimeOut);
133
- this.houseKeepingInProgress = true;
134
134
  } else {
135
135
  // if there is already a housekeeping going on,
136
136
  // we wait for some time to finish it and try again.
@@ -214,7 +214,7 @@ class BusManager {
214
214
  delete this.peerHousekeep[_message.peerId];
215
215
  } else {
216
216
  // this peer seems to be still alive and thinks it is joined.
217
- // we need to releave it from its misery and allow it to leave
217
+ // we need to relieve it from its misery and allow it to leave
218
218
  // gracefully..
219
219
  console.log(` -> peer '${_message.peerName}' not joined anymore - reject from room`);
220
220
  await this.publishRoomAccess(
@@ -228,6 +228,8 @@ class BusManager {
228
228
  _message.roomPwd,
229
229
  "not joined anymore");
230
230
  }
231
+ // if even one peer replies to the room ping,
232
+ // the room can be removed from this list
231
233
  if (this.roomHousekeep[_message.roomName] !== null) {
232
234
  delete this.roomHousekeep[_message.roomName];
233
235
  }
@@ -238,13 +240,14 @@ class BusManager {
238
240
  */
239
241
  cleanOut = async () => {
240
242
  if(this.houseKeepingInProgress) {
243
+ this.houseKeepingInProgress = false;
241
244
  console.log(` -> housekeeping -> cleaning out ...`);
242
- // first we send clear retain messages to
243
- // all the peer joined addresses that have not replied:
244
- const clearRetain = async (currentPeer) => {
245
+ const cleanupAndLeave = async (currentPeer) => {
245
246
  console.log(` <- remove peer '${currentPeer.peerName}' from room '${currentPeer.roomName}' `);
247
+ // first we send clear retain message to peer joined addresses that have not replied:
246
248
  await this.communicator.clearRetain(this.busTopics.roomPeerJoin(currentPeer.roomName, currentPeer.peerId).build());
247
- // send peer left to all remaining peers
249
+
250
+ // create the payload for the leave message
248
251
  const infoPayload = {
249
252
  timestamp: Math.round(new Date().getTime() / 1000),
250
253
  peerId: currentPeer.peerId,
@@ -256,9 +259,9 @@ class BusManager {
256
259
  await this.communicator.publish(
257
260
  new BusMsgPub(
258
261
  this.busTopics.roomPeerLeft(currentPeer.roomName, currentPeer.peerId).build(),
259
- PeerInfo, 2, false).encode(infoPayload));
262
+ PeerInfo, 2, true).encode(infoPayload));
260
263
  }
261
- await Object.values(this.peerHousekeep).every(clearRetain);
264
+ await Object.values(this.peerHousekeep).every(cleanupAndLeave);
262
265
 
263
266
  // and now we can delete the rooms
264
267
  const cleanupRooms = async (currentRoom) => await this.deleteRoom(currentRoom.roomName);
@@ -275,8 +278,6 @@ class BusManager {
275
278
  await this.communicator.unsubscribe(this.busTopics.baseRoomAlivePing("+").build());
276
279
  // cleanup the communicator
277
280
  this.communicator.clearAutoGeneratedSubscriptions();
278
- // and just to be sure
279
- this.houseKeepingInProgress = false;
280
281
  }
281
282
 
282
283
  /**
@@ -363,8 +364,7 @@ class BusManager {
363
364
  reason = `incompatible protocol version '${_message.protocolVersion}': room manager is running on tBus version '${this.protocolVersion}'` ;
364
365
  }
365
366
 
366
- //console.log(`send access to address: ${this.busTopics.basePeerRoomAccess(_message.peerId).build()}.`);
367
- // send access message
367
+ // send access message to peer only
368
368
  await this.publishRoomAccess(
369
369
  accessGrant,
370
370
  _message.peerId,
@@ -377,7 +377,9 @@ class BusManager {
377
377
  reason)
378
378
 
379
379
  if(accessGrant){
380
- //console.log(`publish info to address: ${this.busTopics.roomPeerJoin(_message.roomName, _message.peerId).build()}.`);
380
+ // This sends a message to all peers joined to the room
381
+ // first clear potentially retained leave message
382
+ await this.communicator.clearRetain(this.busTopics.roomPeerLeft(_message.roomName, _message.peerId).build());
381
383
  // send peer joined message to all the peers in the room
382
384
  await this.communicator.publish(
383
385
  new BusMsgPub(
@@ -393,6 +395,7 @@ class BusManager {
393
395
  }
394
396
  };
395
397
 
398
+ // This generates a message to the requesting peer only
396
399
  publishRoomAccess = async (_accessGranted, _peerId, _clientId, _peerName, _roomName, _roomId, _roomUuid, _roomPwd, _reason) => {
397
400
  await this.communicator.publish(
398
401
  new BusMsgPub(this.busTopics.basePeerRoomAccess(_clientId).build(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telemersive-bus",
3
- "version": "0.6.2",
3
+ "version": "0.6.3",
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": {