Merge pull request #2518 from Luis-Fernando-Molina/develop

Contribution: Network Nodes connecting to other Network Nodes
This commit is contained in:
Luis Fernando Molina authored and GitHub committed 2021-11-05 09:50:17 -04:00
commit 16f7d92603
14 files changed
+183 -18

No files matched your search

+10
View File
@@ -0,0 +1,10 @@
{
"folders": [
{
"path": "C:\\Superalgos"
}
],
"settings": {
"search.maintainFileSearchCache": true
}
}
+10
View File
@@ -0,0 +1,10 @@
{
"folders": [
{
"path": "C:\\Superalgos"
}
],
"settings": {
"search.maintainFileSearchCache": true
}
}
+10
View File
@@ -0,0 +1,10 @@
{
"folders": [
{
"path": "C:\\Superalgos"
}
],
"settings": {
"search.maintainFileSearchCache": true
}
}
+5 -5
View File
@@ -2,7 +2,7 @@ exports.newDesktopApp = function newDesktopApp() {
let thisObject = {
socialGraph: undefined,
p2pMetwork: undefined,
p2pNetwork: undefined,
webSocketsClient: undefined,
webSocketsInterface: undefined,
run: run
@@ -21,18 +21,18 @@ 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.
*/
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.
*/
+19 -8
View File
@@ -1,20 +1,19 @@
exports.newNetworkNode = function newNetworkNode() {
let thisObject = {
webSocketsInterface: undefined,
socialGraphService: undefined,
storage: undefined,
p2pMetwork: undefined,
p2pNetwork: undefined,
p2pNetworkPeers: 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()
/*
The Social Graph Service is the first and for now, the only service this Network Node provides.
*/
@@ -26,9 +25,21 @@ exports.newNetworkNode = function newNetworkNode() {
thisObject.storage = NT.projects.socialTrading.modules.storage.newSocialTradingModulesStorage()
thisObject.storage.initialize()
/*
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.p2pMetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork()
await thisObject.p2pMetwork.initialize('Network Peer')
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)
}
}
+5 -1
View File
@@ -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.
*/
@@ -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()
@@ -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
@@ -0,0 +1,50 @@
exports.newNetworkModulesP2PNetworkPeers = function newNetworkModulesP2PNetworkPeers() {
/*
This module represents the P2P Nodes we are connected to.
*/
let thisObject = {
peers: undefined,
/* Framework Functions */
initialize: initialize,
finalize: finalize
}
return thisObject
function finalize() {
thisObject.peers = undefined
}
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.
*/
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)
}
}
}
}
}
@@ -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) {
+6
View File
@@ -1413,6 +1413,12 @@
"propertyName": "p2pNetworkNode",
"functionName": "newNetworkModulesP2PNetworkNode",
"fileName": "P2PNetworkNode.js"
},
{
"name": "P2P Network Peers",
"propertyName": "p2pNetworkPeers",
"functionName": "newNetworkModulesP2PNetworkPeers",
"fileName": "P2PNetworkPeers.js"
}
],
"utilities": [
+16
View File
@@ -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'})
+16
View File
@@ -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'})
+16
View File
@@ -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'})