Server peering improvements.

Ylian Saint-Hilaire committed Nov 15, 2020 at 18:40 UTC cd6d5b12ac21eb516944cb05e47fd3df11337b54
2 files changed +26 -9
meshuser.js
+5 -5
@@ -282,8 +282,8 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
282
283 // Route a command to all targets in a mesh
284 function routeCommandToMesh(meshid, command) {
285 - // Send the request to all peer servers
286 - // TODO !!!!
285 + // If we have peer servers, inform them of this command to send to all agents of this device group
286 + if (parent.parent.multiServer != null) { parent.parent.multiServer.DispatchMessage({ action: 'agentMsgByMeshId', meshid: meshid, command: command }); }
287
288 // See if the node is connected
289 for (var nodeid in parent.wsagents) {
@@ -3347,10 +3347,10 @@ module.exports.CreateMeshUser = function (parent, db, ws, req, args, domain, use
3347 if (db.changeStream) { event.noact = 1; } // If DB change stream is active, don't use this event to change the mesh. Another event will come.
3348 parent.parent.DispatchEvent(parent.CreateMeshDispatchTargets(mesh, [user._id]), obj, event);
3349
3350 - // Send new policy to all computers on this mesh
3351 - //routeCommandToMesh(command.meshid, { action: 'amtPolicy', amtPolicy: amtpolicy });
3350 + // If we have peer servers, inform them of the new Intel AMT policy for this device group
3351 + if (parent.parent.multiServer != null) { parent.parent.multiServer.DispatchMessage({ action: 'newIntelAmtPolicy', meshid: command.meshid, amtpolicy: amtpolicy }); }
3352
3353 - // See if the node is connected
3353 + // See if any agents for the affected device group is connected, if so, update the Intel AMT policy
3354 for (var nodeid in parent.wsagents) {
3355 const agent = parent.wsagents[nodeid];
3356 if (agent.dbMeshKey == command.meshid) { agent.sendUpdatedIntelAmtPolicy(amtpolicy); }
multiserver.js
+21 -4
@@ -570,6 +570,23 @@ module.exports.CreateMultiServer = function (parent, args) {
570 }
571 break;
572 }
573 + case 'newIntelAmtPolicy': {
574 + // See if any agents for the affected device group is connected, if so, update the Intel AMT policy
575 + for (var nodeid in obj.parent.webserver.wsagents) {
576 + const agent = obj.parent.webserver.wsagents[nodeid];
577 + if (agent.dbMeshKey == msg.meshid) { agent.sendUpdatedIntelAmtPolicy(msg.amtpolicy); }
578 + }
579 + break;
580 + }
581 + case 'agentMsgByMeshId': {
582 + // See if any agents for the target device group is connected, if so, send the message
583 + const jsonCmd = JSON.stringify(msg.command);
584 + for (var nodeid in obj.parent.webserver.wsagents) {
585 + var agent = obj.parent.webserver.wsagents[nodeid];
586 + if (agent.dbMeshKey == msg.meshid) { try { agent.send(jsonCmd); } catch (ex) { } }
587 + }
588 + break;
589 + }
590 default: {
591 // Unknown peer server command
592 console.log('Unknown action from peer server ' + peerServerId + ': ' + msg.action + '.');
@@ -651,12 +668,12 @@ module.exports.CreateMultiServer = function (parent, args) {
668 peerTunnel.close = function (arg) {
669 if (arg == 2) {
670 // Hard close, close the TCP socket
654 - if (peerTunnel.ws1 != null) { try { peerTunnel.ws1._socket._parent.end(); peerTunnel.parent.parent.debug('peer', 'FTunnel1: Hard disconnect'); } catch (e) { console.log(e); } }
655 - if (peerTunnel.ws2 != null) { try { peerTunnel.ws2._socket._parent.end(); peerTunnel.parent.parent.debug('peer', 'FTunnel2: Hard disconnect'); } catch (e) { console.log(e); } }
671 + if (peerTunnel.ws1 != null) { try { peerTunnel.ws1._socket._parent.end(); peerTunnel.parent.parent.debug('peer', 'FTunnel1: Hard disconnect'); } catch (e) { console.log(e); } delete peerTunnel.ws1; }
672 + if (peerTunnel.ws2 != null) { try { peerTunnel.ws2._socket._parent.end(); peerTunnel.parent.parent.debug('peer', 'FTunnel2: Hard disconnect'); } catch (e) { console.log(e); } delete peerTunnel.ws2; }
673 } else {
674 // Soft close, close the websocket
658 - if (peerTunnel.ws1 != null) { try { peerTunnel.ws1.close(); peerTunnel.parent.parent.debug('peer', 'FTunnel1: Soft disconnect '); } catch (e) { console.log(e); } }
659 - if (peerTunnel.ws2 != null) { try { peerTunnel.ws2.close(); peerTunnel.parent.parent.debug('peer', 'FTunnel2: Soft disconnect '); } catch (e) { console.log(e); } }
675 + if (peerTunnel.ws1 != null) { try { peerTunnel.ws1.close(); peerTunnel.parent.parent.debug('peer', 'FTunnel1: Soft disconnect '); } catch (e) { console.log(e); } delete peerTunnel.ws1; }
676 + if (peerTunnel.ws2 != null) { try { peerTunnel.ws2.close(); peerTunnel.parent.parent.debug('peer', 'FTunnel2: Soft disconnect '); } catch (e) { console.log(e); } delete peerTunnel.ws2; }
677 }
678 };
679