telemersive-bus 0.6.2 → 0.6.4

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/BusClient.js CHANGED
@@ -65,9 +65,9 @@ class BusClient {
65
65
  this.connectTimeout = setTimeout(this.connectionTimeout,1000);
66
66
  await this.communicator.connect();
67
67
  // if connection was successful, subscribe to room info topic
68
- await this.communicator.subscribe(new BusMsgSub(this.busTopics.baseRoomInfo('+').build(), this.onRooms, RoomInfo, 0));
68
+ await this.communicator.subscribe(new BusMsgSub(this.busTopics.baseRoomInfo('+').build(), this.onRooms, RoomInfo, 2));
69
69
  // if connection was successful, subscribe to room delete topic
70
- await this.communicator.subscribe(new BusMsgSub(this.busTopics.roomDelete().build(), this.onRoomDelete, RoomInfo, 0));
70
+ await this.communicator.subscribe(new BusMsgSub(this.busTopics.roomDelete().build(), this.onRoomDelete, RoomInfo, 2));
71
71
  //console.log(`...subscribe to. ${this.busTopics.basePeerRoomAccess(this.peerId).build()}`);
72
72
  this.bubbleUpCallback('bus', [ 'broker', 'connected', 1]);
73
73
  console.log("...successfully connected.");
@@ -135,7 +135,7 @@ class BusClient {
135
135
  await this.communicator.reconnect(null);
136
136
 
137
137
  // if connection was successful, subscribe to roomAccess topic
138
- await this.communicator.subscribe(new BusMsgSub(this.busTopics.basePeerRoomAccess(this.peer.getClientId()).build(), this.onRoomJoined, RoomAccess, 0));
138
+ await this.communicator.subscribe(new BusMsgSub(this.busTopics.basePeerRoomAccess(this.peer.getClientId()).build(), this.onRoomJoined, RoomAccess, 2));
139
139
 
140
140
  // now we start the timeout thread
141
141
  this.joinTimeout = setTimeout(this.joinedTimeout,5000);
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.
@@ -186,7 +186,10 @@ class BusManager {
186
186
  * return messages from peers on room ping requests
187
187
  */
188
188
  onRoomAlive = async (_topic, _message, _packet) => {
189
- console.log(` -> room '${_message.roomName}' alive with peer '${_message.peerName}'`);
189
+ let accessGrant;
190
+ let reason = "unknown";
191
+ console.log(` -> room '${_message.roomName}' alive ping from peer '${_message.peerName}'`);
192
+
190
193
  // if we receive this message, it means this peer is alive and we can remove it
191
194
  // from our housekeeping lists
192
195
  if (_message.peerId in this.peerHousekeep) {
@@ -200,37 +203,36 @@ class BusManager {
200
203
  // now we resend the access message
201
204
  // we need to do this in case the manager has crashed and restarted...
202
205
  // send access message
203
- await this.publishRoomAccess(
204
- true,
205
- _message.peerId,
206
- _message.clientId,
207
- _message.peerName,
208
- _message.roomName,
209
- this.rooms[_message.roomName].roomId,
210
- this.rooms[_message.roomName].roomUUID,
211
- _message.roomPwd,
212
- "confirmation");
206
+ accessGrant = true;
207
+ reason = "confirmation";
208
+ delete this.peerHousekeep[_message.peerId];
209
+
210
+ // if even one joined peer replies to the room ping,
211
+ // the room can be removed from this list
212
+ if (this.roomHousekeep[_message.roomName] !== null) {
213
+ delete this.roomHousekeep[_message.roomName];
214
+ }
213
215
  }
214
- delete this.peerHousekeep[_message.peerId];
215
216
  } else {
216
217
  // 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
218
+ // we need to relieve it from its misery and allow it to leave
218
219
  // gracefully..
220
+ accessGrant = false;
221
+ reason = "not joined anymore";
219
222
  console.log(` -> peer '${_message.peerName}' not joined anymore - reject from room`);
220
- await this.publishRoomAccess(
221
- false,
222
- _message.peerId,
223
- _message.clientId,
224
- _message.peerName,
225
- _message.roomName,
226
- this.rooms[_message.roomName].roomId,
227
- this.rooms[_message.roomName].roomUUID,
228
- _message.roomPwd,
229
- "not joined anymore");
230
- }
231
- if (this.roomHousekeep[_message.roomName] !== null) {
232
- delete this.roomHousekeep[_message.roomName];
233
223
  }
224
+
225
+ await this.publishRoomAccess(
226
+ accessGrant,
227
+ _message.peerId,
228
+ _message.clientId,
229
+ _message.peerName,
230
+ _message.roomName,
231
+ this.rooms[_message.roomName].roomId,
232
+ this.rooms[_message.roomName].roomUUID,
233
+ _message.roomPwd,
234
+ reason);
235
+
234
236
  };
235
237
 
236
238
  /**
@@ -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
  /**
@@ -285,21 +286,18 @@ class BusManager {
285
286
  onPeerLeaving = async (_topic, _message, _packet) => {
286
287
  console.log(` <- peer '${_message.peerName}' leaving`);
287
288
  let shaPassword = sha1(_message.roomPwd).substr(0, 10)
288
- if (this.rooms.hasOwnProperty(_message.roomName)){
289
- if (this.rooms[_message.roomName].roomPwd === shaPassword){
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.
293
- // it could be, that this message was called due to a short disconnect by the
294
- // peer to the broker - issuing a lastWill message by the broker.
295
- // by giving it some time the housekeeping process will verify if the peer
296
- // is still online or if it is really gone..
297
- setTimeout(this.startHousekeeping, houseKeepingTimeOut_onPeerLeft);
298
- } else {
299
- console.log(`!!! >BAD PLAYER<: somebody tried to remove peer without proper credentials!!!`);
300
- }
289
+ if (this.rooms[_message.roomName].roomPwd === shaPassword){
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.
293
+ // it could be, that this message was called due to a short disconnect by the
294
+ // peer to the broker - issuing a lastWill message by the broker.
295
+ // by giving it some time the housekeeping process will verify if the peer
296
+ // is still online or if it is really gone..
297
+ setTimeout(this.startHousekeeping, houseKeepingTimeOut_onPeerLeft);
298
+ } else {
299
+ console.log(`!!! >BAD PLAYER<: somebody tried to remove peer without proper credentials!!!`);
301
300
  }
302
-
303
301
  };
304
302
 
305
303
  /*********************************************************************
@@ -333,7 +331,10 @@ class BusManager {
333
331
  console.log(` -> peer '${_message.peerName}' creating new room '${_message.roomName}' for app version: '${_message.appVersion}'`);
334
332
  reason = "no room of this name exists";
335
333
  await this.publishNewRoom(_message.roomName, shaPassword, _message.appVersion);
334
+ // and we wait a moment to start with housekeeping again
335
+ setTimeout(this.startHousekeeping, houseKeepingTimeOut_onPeerJoined);
336
336
  } else {
337
+ //room exists: checked if the doubled hashed roomPwd is equal
337
338
  if (this.rooms[_message.roomName].roomPwd !== shaPassword) {
338
339
  console.log(` -> peer '${_message.peerName}' declined access to room: ${_message.roomName} | invalid password`);
339
340
  accessGrant = false;
@@ -343,7 +344,6 @@ class BusManager {
343
344
  accessGrant = false;
344
345
  reason = `app version incompatible: required version is '${this.rooms[_message.roomName].appVersion}', yours is '${_message.appVersion}'`;
345
346
  } else {
346
- //room exists: checked if the doubled hashed roomPwd is equal
347
347
  accessGrant = true;
348
348
  console.log(` -> peer '${_message.peerName}' joining room '${_message.roomName}'`);
349
349
  // if there is a housekeeping process in progress,
@@ -363,8 +363,7 @@ class BusManager {
363
363
  reason = `incompatible protocol version '${_message.protocolVersion}': room manager is running on tBus version '${this.protocolVersion}'` ;
364
364
  }
365
365
 
366
- //console.log(`send access to address: ${this.busTopics.basePeerRoomAccess(_message.peerId).build()}.`);
367
- // send access message
366
+ // send access message to peer only
368
367
  await this.publishRoomAccess(
369
368
  accessGrant,
370
369
  _message.peerId,
@@ -377,7 +376,9 @@ class BusManager {
377
376
  reason)
378
377
 
379
378
  if(accessGrant){
380
- //console.log(`publish info to address: ${this.busTopics.roomPeerJoin(_message.roomName, _message.peerId).build()}.`);
379
+ // This sends a message to all peers joined to the room
380
+ // first clear potentially retained leave message
381
+ await this.communicator.clearRetain(this.busTopics.roomPeerLeft(_message.roomName, _message.peerId).build());
381
382
  // send peer joined message to all the peers in the room
382
383
  await this.communicator.publish(
383
384
  new BusMsgPub(
@@ -393,6 +394,7 @@ class BusManager {
393
394
  }
394
395
  };
395
396
 
397
+ // This generates a message to the requesting peer only
396
398
  publishRoomAccess = async (_accessGranted, _peerId, _clientId, _peerName, _roomName, _roomId, _roomUuid, _roomPwd, _reason) => {
397
399
  await this.communicator.publish(
398
400
  new BusMsgPub(this.busTopics.basePeerRoomAccess(_clientId).build(),
package/lib/utils/Peer.js CHANGED
@@ -193,10 +193,10 @@ class Peer {
193
193
 
194
194
  // it is important to start the subscription after a successfull joining.
195
195
  // subscribe to all peer joined messages
196
- await this.communicator.subscribe(new BusMsgSub(this.busTopics.roomPeerJoin(this.roomName, '+').build(), this.onPeerJoined, PeerInfo, 0));
196
+ await this.communicator.subscribe(new BusMsgSub(this.busTopics.roomPeerJoin(this.roomName, '+').build(), this.onPeerJoined, PeerInfo, 2));
197
197
 
198
198
  // subscribe to peer left messages
199
- await this.communicator.subscribe(new BusMsgSub(this.busTopics.roomPeerLeft(this.roomName, '+').build(), this.onPeerLeft, PeerInfo, 0));
199
+ await this.communicator.subscribe(new BusMsgSub(this.busTopics.roomPeerLeft(this.roomName, '+').build(), this.onPeerLeft, PeerInfo, 2));
200
200
 
201
201
  // subscribe to my room ping messages
202
202
  await this.communicator.subscribe(new BusMsgSub(this.busTopics.baseRoomAlive(this.roomName).build(), this.onRoomPing, RoomInfo, 0));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telemersive-bus",
3
- "version": "0.6.2",
3
+ "version": "0.6.4",
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": {