From e98b1fbd6c3ea161a6d5a171fa490c4339e7b2b5 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 5 Nov 2021 12:05:38 +0100 Subject: [PATCH 1/3] Update NetwokNode.js --- Network/NetwokNode.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Network/NetwokNode.js b/Network/NetwokNode.js index f70c5a100..d3c300e2e 100644 --- a/Network/NetwokNode.js +++ b/Network/NetwokNode.js @@ -1,20 +1,23 @@ exports.newNetworkNode = function newNetworkNode() { let thisObject = { + webSocketsInterface: undefined, socialGraphService: undefined, storage: undefined, p2pMetwork: undefined, run: run } + NT.networkNode = thisObject + return thisObject async function run() { /* Other Network Nodes and Client Apps will communicate with this Network Node via it's Websocket Interface. */ - NT.webSocketsInterface = NT.projects.network.modules.webSocketsInterface.newNetworkModulesWebSocketsInterface() - NT.webSocketsInterface.initialize() + thisObject.webSocketsInterface = NT.projects.network.modules.webSocketsInterface.newNetworkModulesWebSocketsInterface() + thisObject.webSocketsInterface.initialize() /* The Social Graph Service is the first and for now, the only service this Network Node provides. */ @@ -28,7 +31,7 @@ exports.newNetworkNode = function newNetworkNode() { /* We can join now the P2P Network as a new peer there. */ - thisObject.p2pMetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork() - await thisObject.p2pMetwork.initialize('Network Peer') + thisObject.p2pMetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork() + await thisObject.p2pMetwork.initialize('Network Peer') } } From 48c35d8148c92268e8c38eb620a538f40d802fa8 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 5 Nov 2021 12:44:58 +0100 Subject: [PATCH 2/3] 3 Network Nodes Running in Debug Mode --- .vscode/Network-Node-1.code-workspace | 10 +++++++ .vscode/Network-Node-2.code-workspace | 10 +++++++ .vscode/Network-Node-3.code-workspace | 10 +++++++ Desktop/DesktopApp.js | 8 +++--- Network/NetwokNode.js | 18 +++++++------ NetworkRoot.js | 6 ++++- .../Network/NT/Modules/WebSocketsInterface.js | 4 ++- Projects/Network/SA/Modules/P2PNetwork.js | 3 +++ .../Network/SA/Modules/P2PNetworkPeers.js | 26 +++++++++++++++++++ network-node-1.js | 16 ++++++++++++ network-node-2.js | 16 ++++++++++++ network-node-3.js | 16 ++++++++++++ 12 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 .vscode/Network-Node-1.code-workspace create mode 100644 .vscode/Network-Node-2.code-workspace create mode 100644 .vscode/Network-Node-3.code-workspace create mode 100644 Projects/Network/SA/Modules/P2PNetworkPeers.js create mode 100644 network-node-1.js create mode 100644 network-node-2.js create mode 100644 network-node-3.js diff --git a/.vscode/Network-Node-1.code-workspace b/.vscode/Network-Node-1.code-workspace new file mode 100644 index 000000000..97e2d4c8f --- /dev/null +++ b/.vscode/Network-Node-1.code-workspace @@ -0,0 +1,10 @@ +{ + "folders": [ + { + "path": "C:\\Superalgos" + } + ], + "settings": { + "search.maintainFileSearchCache": true + } +} \ No newline at end of file diff --git a/.vscode/Network-Node-2.code-workspace b/.vscode/Network-Node-2.code-workspace new file mode 100644 index 000000000..97e2d4c8f --- /dev/null +++ b/.vscode/Network-Node-2.code-workspace @@ -0,0 +1,10 @@ +{ + "folders": [ + { + "path": "C:\\Superalgos" + } + ], + "settings": { + "search.maintainFileSearchCache": true + } +} \ No newline at end of file diff --git a/.vscode/Network-Node-3.code-workspace b/.vscode/Network-Node-3.code-workspace new file mode 100644 index 000000000..97e2d4c8f --- /dev/null +++ b/.vscode/Network-Node-3.code-workspace @@ -0,0 +1,10 @@ +{ + "folders": [ + { + "path": "C:\\Superalgos" + } + ], + "settings": { + "search.maintainFileSearchCache": true + } +} \ No newline at end of file diff --git a/Desktop/DesktopApp.js b/Desktop/DesktopApp.js index df7d7f570..2be0b2ac6 100644 --- a/Desktop/DesktopApp.js +++ b/Desktop/DesktopApp.js @@ -2,7 +2,7 @@ exports.newDesktopApp = function newDesktopApp() { let thisObject = { socialGraph: undefined, - p2pMetwork: undefined, + p2pNetwork: undefined, webSocketsClient: undefined, webSocketsInterface: undefined, run: run @@ -21,12 +21,12 @@ exports.newDesktopApp = function newDesktopApp() { /* We can join now the P2P Network as a client app. */ - thisObject.p2pMetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork() - await thisObject.p2pMetwork.initialize('Network Client') + thisObject.p2pNetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork() + await thisObject.p2pNetwork.initialize('Network Client') /* Here we will pick a Network Node from all users profiles available that do have a Network Node running. */ - let selectedNetworkNode = thisObject.p2pMetwork.p2pNodesToConnect[0] + let selectedNetworkNode = thisObject.p2pNetwork.p2pNodesToConnect[0] /* This is the Web Sockets client that interacts with the Superalgos Network. */ diff --git a/Network/NetwokNode.js b/Network/NetwokNode.js index d3c300e2e..26763ca92 100644 --- a/Network/NetwokNode.js +++ b/Network/NetwokNode.js @@ -4,7 +4,7 @@ exports.newNetworkNode = function newNetworkNode() { webSocketsInterface: undefined, socialGraphService: undefined, storage: undefined, - p2pMetwork: undefined, + p2pNetwork: undefined, run: run } @@ -13,11 +13,6 @@ exports.newNetworkNode = function newNetworkNode() { return thisObject async function run() { - /* - Other Network Nodes and Client Apps will communicate with this Network Node via it's Websocket Interface. - */ - thisObject.webSocketsInterface = NT.projects.network.modules.webSocketsInterface.newNetworkModulesWebSocketsInterface() - thisObject.webSocketsInterface.initialize() /* The Social Graph Service is the first and for now, the only service this Network Node provides. */ @@ -31,7 +26,14 @@ exports.newNetworkNode = function newNetworkNode() { /* We can join now the P2P Network as a new peer there. */ - thisObject.p2pMetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork() - await thisObject.p2pMetwork.initialize('Network Peer') + thisObject.p2pNetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork() + await thisObject.p2pNetwork.initialize('Network Peer') + /* + Other Network Nodes and Client Apps will communicate with this Network Node via it's Websocket Interface. + */ + thisObject.webSocketsInterface = NT.projects.network.modules.webSocketsInterface.newNetworkModulesWebSocketsInterface() + thisObject.webSocketsInterface.initialize() + console.log('Network Node Web Sockets Interface ....................................... Listening at port ' + JSON.parse(NT.networkNode.p2pNetwork.thisNetworkNode.node.config).webSocketsPort) + } } diff --git a/NetworkRoot.js b/NetworkRoot.js index daa007839..a09e3cd62 100644 --- a/NetworkRoot.js +++ b/NetworkRoot.js @@ -10,7 +10,7 @@ exports.newNetworkRoot = function newNetworkRoot() { return thisObject - async function run() { + async function run(debugSettings) { /* The NT object is accessible everywhere at the Superalgos Network. It provides access to all modules built for this Network. @@ -25,6 +25,10 @@ exports.newNetworkRoot = function newNetworkRoot() { let ENVIRONMENT = require('./Environment.js'); let ENVIRONMENT_MODULE = ENVIRONMENT.newEnvironment() global.env = ENVIRONMENT_MODULE + + if (debugSettings !== undefined && debugSettings.P2P_NETWORK_NODE_SIGNING_ACCOUNT !== undefined) { + global.env.P2P_NETWORK_NODE_SIGNING_ACCOUNT = debugSettings.P2P_NETWORK_NODE_SIGNING_ACCOUNT + } /* First thing is to load the project schema file. */ diff --git a/Projects/Network/NT/Modules/WebSocketsInterface.js b/Projects/Network/NT/Modules/WebSocketsInterface.js index e79a51bb2..7374e869e 100644 --- a/Projects/Network/NT/Modules/WebSocketsInterface.js +++ b/Projects/Network/NT/Modules/WebSocketsInterface.js @@ -40,7 +40,9 @@ exports.newNetworkModulesWebSocketsInterface = function newNetworkModulesWebSock } function initialize() { - socketServer = new SA.nodeModules.ws.Server({ port: global.env.NETWORK_WEB_SOCKETS_INTERFACE_PORT }) + let port = JSON.parse(NT.networkNode.p2pNetwork.thisNetworkNode.node.config).webSocketsPort + + socketServer = new SA.nodeModules.ws.Server({ port: port }) clientInterface = NT.projects.socialTrading.modules.clientInterface.newSocialTradingModulesClientInterface() peerInterface = NT.projects.socialTrading.modules.peerInterface.newSocialTradingModulesPeerInterface() diff --git a/Projects/Network/SA/Modules/P2PNetwork.js b/Projects/Network/SA/Modules/P2PNetwork.js index 61ae16c87..131f82e60 100644 --- a/Projects/Network/SA/Modules/P2PNetwork.js +++ b/Projects/Network/SA/Modules/P2PNetwork.js @@ -6,6 +6,7 @@ exports.newNetworkModulesP2PNetwork = function newNetworkModulesP2PNetwork() { let thisObject = { joinTheNetworkAsNetworkPeer: joinTheNetworkAsNetworkPeer, p2pNodesToConnect: undefined, + thisNetworkNode: undefined, /* Framework Functions */ initialize: initialize, finalize: finalize @@ -37,6 +38,8 @@ exports.newNetworkModulesP2PNetwork = function newNetworkModulesP2PNetwork() { let p2pNetworkNode = SA.projects.network.globals.memory.arrays.P2P_NETWORK_NODES[i] if (thisP2PNodeId !== p2pNetworkNode.node.id) { thisObject.p2pNodesToConnect.push(p2pNetworkNode) + } else { + thisObject.thisNetworkNode = p2pNetworkNode } } break diff --git a/Projects/Network/SA/Modules/P2PNetworkPeers.js b/Projects/Network/SA/Modules/P2PNetworkPeers.js new file mode 100644 index 000000000..2e40bd9ca --- /dev/null +++ b/Projects/Network/SA/Modules/P2PNetworkPeers.js @@ -0,0 +1,26 @@ +exports.newNetworkModulesP2PNetworkPeers = function newNetworkModulesP2PNetworkPeers() { + /* + This module represents the P2P Nodes we are connected to. + */ + let thisObject = { + + /* Framework Functions */ + initialize: initialize, + finalize: finalize + } + + return thisObject + + function finalize() { + + } + + async function initialize() { + /* + On a first iteration, we will try to connect to 2 Network Peers, one upstream and one downstream, + meaning one with higher ranking that us, and the other with lower ranking than us. + */ + + + } +} \ No newline at end of file diff --git a/network-node-1.js b/network-node-1.js new file mode 100644 index 000000000..ffb2ccd1e --- /dev/null +++ b/network-node-1.js @@ -0,0 +1,16 @@ +/* +The Superalgos Network offers 3 types of services: + + * Social Graph Service + * Search Index Service + * Private Message Service + +Users can decide which services to run at their node.(TODO) + +This module represents the Nodejs command that users have to start the Network Node.. + +*/ + +let APP_ROOT = require('./NetworkRoot.js') +let APP_ROOT_MODULE = APP_ROOT.newNetworkRoot() +APP_ROOT_MODULE.run({P2P_NETWORK_NODE_SIGNING_ACCOUNT: 'P2P-Network-Node-1'}) \ No newline at end of file diff --git a/network-node-2.js b/network-node-2.js new file mode 100644 index 000000000..0f36a5e2d --- /dev/null +++ b/network-node-2.js @@ -0,0 +1,16 @@ +/* +The Superalgos Network offers 3 types of services: + + * Social Graph Service + * Search Index Service + * Private Message Service + +Users can decide which services to run at their node.(TODO) + +This module represents the Nodejs command that users have to start the Network Node.. + +*/ + +let APP_ROOT = require('./NetworkRoot.js') +let APP_ROOT_MODULE = APP_ROOT.newNetworkRoot() +APP_ROOT_MODULE.run({P2P_NETWORK_NODE_SIGNING_ACCOUNT: 'P2P-Network-Node-2'}) \ No newline at end of file diff --git a/network-node-3.js b/network-node-3.js new file mode 100644 index 000000000..f8a3ac41e --- /dev/null +++ b/network-node-3.js @@ -0,0 +1,16 @@ +/* +The Superalgos Network offers 3 types of services: + + * Social Graph Service + * Search Index Service + * Private Message Service + +Users can decide which services to run at their node.(TODO) + +This module represents the Nodejs command that users have to start the Network Node.. + +*/ + +let APP_ROOT = require('./NetworkRoot.js') +let APP_ROOT_MODULE = APP_ROOT.newNetworkRoot() +APP_ROOT_MODULE.run({P2P_NETWORK_NODE_SIGNING_ACCOUNT: 'P2P-Network-Node-3'}) \ No newline at end of file From b5eb7dd425aa9f3f1b59ccd1d7eb172693f4b0d2 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 5 Nov 2021 14:40:51 +0100 Subject: [PATCH 3/3] Network Nodes connecting to other Network Nodes --- Desktop/DesktopApp.js | 2 +- Network/NetwokNode.js | 14 ++++++--- .../Network/SA/Modules/P2PNetworkPeers.js | 30 +++++++++++++++++-- .../SA/Modules/WebSocketsNetworkClient.js | 17 +++++++++-- Projects/ProjectsSchema.json | 6 ++++ 5 files changed, 58 insertions(+), 11 deletions(-) diff --git a/Desktop/DesktopApp.js b/Desktop/DesktopApp.js index 2be0b2ac6..6581230a1 100644 --- a/Desktop/DesktopApp.js +++ b/Desktop/DesktopApp.js @@ -32,7 +32,7 @@ exports.newDesktopApp = function newDesktopApp() { */ thisObject.webSocketsClient = SA.projects.network.modules.webSocketsNetworkClient.newNetworkModulesWebSocketsNetworkClient() await thisObject.webSocketsClient.initialize('Network Client', selectedNetworkNode) - console.log('Desktop Client Connected to Network Node via Web Sockets ................... Connected to port ' + global.env.NETWORK_WEB_SOCKETS_INTERFACE_PORT) + /* These are the Network Interfaces by which the Web App interacts with this Desktop Client. */ diff --git a/Network/NetwokNode.js b/Network/NetwokNode.js index 26763ca92..91fb26487 100644 --- a/Network/NetwokNode.js +++ b/Network/NetwokNode.js @@ -5,6 +5,7 @@ exports.newNetworkNode = function newNetworkNode() { socialGraphService: undefined, storage: undefined, p2pNetwork: undefined, + p2pNetworkPeers: undefined, run: run } @@ -24,16 +25,21 @@ exports.newNetworkNode = function newNetworkNode() { thisObject.storage = NT.projects.socialTrading.modules.storage.newSocialTradingModulesStorage() thisObject.storage.initialize() /* - We can join now the P2P Network as a new peer there. + Let's discover who is at the p2p network. */ thisObject.p2pNetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork() await thisObject.p2pNetwork.initialize('Network Peer') /* + We can join now the P2P Network as a new peer there. + */ + thisObject.p2pNetworkPeers = SA.projects.network.modules.p2pNetworkPeers.newNetworkModulesP2PNetworkPeers() + await thisObject.p2pNetworkPeers.initialize() + /* Other Network Nodes and Client Apps will communicate with this Network Node via it's Websocket Interface. */ - thisObject.webSocketsInterface = NT.projects.network.modules.webSocketsInterface.newNetworkModulesWebSocketsInterface() - thisObject.webSocketsInterface.initialize() - console.log('Network Node Web Sockets Interface ....................................... Listening at port ' + JSON.parse(NT.networkNode.p2pNetwork.thisNetworkNode.node.config).webSocketsPort) + thisObject.webSocketsInterface = NT.projects.network.modules.webSocketsInterface.newNetworkModulesWebSocketsInterface() + thisObject.webSocketsInterface.initialize() + console.log('Network Node Web Sockets Interface ....................................... Listening at port ' + JSON.parse(NT.networkNode.p2pNetwork.thisNetworkNode.node.config).webSocketsPort) } } diff --git a/Projects/Network/SA/Modules/P2PNetworkPeers.js b/Projects/Network/SA/Modules/P2PNetworkPeers.js index 2e40bd9ca..ddcc56637 100644 --- a/Projects/Network/SA/Modules/P2PNetworkPeers.js +++ b/Projects/Network/SA/Modules/P2PNetworkPeers.js @@ -2,7 +2,8 @@ exports.newNetworkModulesP2PNetworkPeers = function newNetworkModulesP2PNetworkP /* This module represents the P2P Nodes we are connected to. */ - let thisObject = { + let thisObject = { + peers: undefined, /* Framework Functions */ initialize: initialize, @@ -12,7 +13,7 @@ exports.newNetworkModulesP2PNetworkPeers = function newNetworkModulesP2PNetworkP return thisObject function finalize() { - + thisObject.peers = undefined } async function initialize() { @@ -20,7 +21,30 @@ exports.newNetworkModulesP2PNetworkPeers = function newNetworkModulesP2PNetworkP On a first iteration, we will try to connect to 2 Network Peers, one upstream and one downstream, meaning one with higher ranking that us, and the other with lower ranking than us. */ + thisObject.peers = [] + let totalConnected = 0 - + for (let i = 0; i < NT.networkNode.p2pNetwork.p2pNodesToConnect.length; i++) { + if (totalConnected === 2) { break } + let peer = {} + peer.p2pNetworkNode = NT.networkNode.p2pNetwork.p2pNodesToConnect[i] + peer.webSocketsClient = SA.projects.network.modules.webSocketsNetworkClient.newNetworkModulesWebSocketsNetworkClient() + await peer.webSocketsClient.initialize('Network Peer', peer.p2pNetworkNode) + .then(addPeer) + .catch(onError) + + function addPeer() { + thisObject.peers.push(peer) + totalConnected++ + } + + function onError(err) { + if (err !== undefined) { + console.log('[ERROR] P2P Network Peers -> onError -> While connecting to node -> ' + peer.p2pNetworkNode.userProfile.userProfileHandle + ' -> ' + peer.p2pNetworkNode.node.name + ' -> ' + err.message) + } else { + console.log('[WARN] P2P Network Peers -> onError -> Peer Not Available at the Moment -> ' + peer.p2pNetworkNode.userProfile.userProfileHandle + ' -> ' + peer.p2pNetworkNode.node.name) + } + } + } } } \ No newline at end of file diff --git a/Projects/Network/SA/Modules/WebSocketsNetworkClient.js b/Projects/Network/SA/Modules/WebSocketsNetworkClient.js index 16ffbafd7..c2db2516a 100644 --- a/Projects/Network/SA/Modules/WebSocketsNetworkClient.js +++ b/Projects/Network/SA/Modules/WebSocketsNetworkClient.js @@ -1,6 +1,8 @@ exports.newNetworkModulesWebSocketsNetworkClient = function newNetworkModulesWebSocketsNetworkClient() { let thisObject = { + host: undefined, + port: undefined, sendMessage: sendMessage, initialize: initialize, finalize: finalize @@ -29,11 +31,13 @@ exports.newNetworkModulesWebSocketsNetworkClient = function newNetworkModulesWeb web3 = new SA.nodeModules.web3() - let host = JSON.parse(p2pNetworkNode.node.config).host - let port = JSON.parse(p2pNetworkNode.node.config).webSocketsPort + thisObject.host = JSON.parse(p2pNetworkNode.node.config).host + thisObject.port = JSON.parse(p2pNetworkNode.node.config).webSocketsPort - socketClient = new SA.nodeModules.ws('ws://' + host + ':' + port) + socketClient = new SA.nodeModules.ws('ws://' + thisObject.host + ':' + thisObject.port) await setUpWebsocketClient(callerRole, p2pNetworkNode) + + console.log('Websockets Client Connected to Network Node via Web Sockets .............. Connected to ' + p2pNetworkNode.userProfile.userProfileHandle + ' -> ' + p2pNetworkNode.node.name + ' -> ' + thisObject.host + ':' + thisObject.port) } async function setUpWebsocketClient(callerRole, p2pNetworkNode) { @@ -197,8 +201,15 @@ exports.newNetworkModulesWebSocketsNetworkClient = function newNetworkModulesWeb } function onError(err) { + if (err.message.indexOf('ECONNREFUSED') >= 0) { + console.log('[WARN] Web Sockets Client -> onError -> Nobody home at ' + thisObject.host + ':' + thisObject.port) + reject() + return + } console.log('[ERROR] Web Sockets Client -> onError -> err.message = ' + err.message) console.log('[ERROR] Web Sockets Client -> onError -> err.stack = ' + err.stack) + reject() + return } } catch (err) { diff --git a/Projects/ProjectsSchema.json b/Projects/ProjectsSchema.json index 2182d5c83..feebfdbbf 100644 --- a/Projects/ProjectsSchema.json +++ b/Projects/ProjectsSchema.json @@ -1413,6 +1413,12 @@ "propertyName": "p2pNetworkNode", "functionName": "newNetworkModulesP2PNetworkNode", "fileName": "P2PNetworkNode.js" + }, + { + "name": "P2P Network Peers", + "propertyName": "p2pNetworkPeers", + "functionName": "newNetworkModulesP2PNetworkPeers", + "fileName": "P2PNetworkPeers.js" } ], "utilities": [