Some refactoring and comments added to code

This commit is contained in:
Luis Fernando Molina committed 2021-12-05 10:36:41 +01:00
1 parent e620174fa1
commit 65a141da5c
10 files changed
+70 -49

No files matched your search

+7 -7
View File
@@ -2,7 +2,7 @@ exports.newDesktopApp = function newDesktopApp() {
let thisObject = {
userProfiles: undefined,
p2pNetworkClient: undefined,
p2pNetworkClientIdentity: undefined,
p2pNetwork: undefined,
p2pNetworkPeers: undefined,
webSocketsInterface: undefined,
@@ -25,13 +25,13 @@ exports.newDesktopApp = function newDesktopApp() {
/*
We set up ourselves as a Network Client.
*/
thisObject.p2pNetworkClient = SA.projects.network.modules.p2pNetworkClient.newNetworkModulesP2PNetworkClient()
await thisObject.p2pNetworkClient.initialize()
thisObject.p2pNetworkClientIdentity = SA.projects.network.modules.p2pNetworkClientIdentity.newNetworkModulesP2PNetworkClientIdentity()
await thisObject.p2pNetworkClientIdentity.initialize()
/*
We will read all user profiles plugins and get from there our network identity.
*/
thisObject.userProfiles = SA.projects.network.modules.userProfiles.newNetworkModulesUserProfiles()
await thisObject.userProfiles.initialize(global.env.DESKTOP_APP_SIGNING_ACCOUNT, thisObject.p2pNetworkClient)
await thisObject.userProfiles.initialize(global.env.DESKTOP_APP_SIGNING_ACCOUNT, thisObject.p2pNetworkClientIdentity)
/*
We set up the P2P Network.
*/
@@ -43,7 +43,7 @@ exports.newDesktopApp = function newDesktopApp() {
thisObject.p2pNetworkPeers = SA.projects.network.modules.p2pNetworkPeers.newNetworkModulesP2PNetworkPeers()
await thisObject.p2pNetworkPeers.initialize(
'Network Client',
thisObject.p2pNetworkClient,
thisObject.p2pNetworkClientIdentity,
thisObject.p2pNetwork,
global.env.DESKTOP_APP_MAX_OUTGOING_PEERS
)
@@ -68,11 +68,11 @@ exports.newDesktopApp = function newDesktopApp() {
*/
thisObject.webSocketsInterface = DK.projects.socialTrading.modules.webSocketsInterface.newDesktopModulesWebSocketsInterface()
thisObject.webSocketsInterface.initialize()
console.log('Desktop Client Web Sockets Interface ......................................... Listening at port ' + JSON.parse(DK.desktopApp.p2pNetworkClient.node.config).webSocketsPort)
console.log('Desktop Client Web Sockets Interface ......................................... Listening at port ' + JSON.parse(DK.desktopApp.p2pNetworkClientIdentity.node.config).webSocketsPort)
thisObject.httpInterface = DK.projects.socialTrading.modules.httpInterface.newDesktopModulesHttpInterface()
thisObject.httpInterface.initialize()
console.log('Desktop Client Http Interface ................................................ Listening at port ' + JSON.parse(DK.desktopApp.p2pNetworkClient.node.config).webPort)
console.log('Desktop Client Http Interface ................................................ Listening at port ' + JSON.parse(DK.desktopApp.p2pNetworkClientIdentity.node.config).webPort)
}
}
}
+7 -7
View File
@@ -3,7 +3,7 @@ exports.newDesktopApp = function newDesktopApp() {
let thisObject = {
userProfiles: undefined,
p2pNetworkClient: undefined,
p2pNetworkClientIdentity: undefined,
p2pNetwork: undefined,
p2pNetworkPeers: undefined,
webSocketsInterface: undefined,
@@ -26,13 +26,13 @@ exports.newDesktopApp = function newDesktopApp() {
/*
We set up ourselves as a Network Client.
*/
thisObject.p2pNetworkClient = SA.projects.network.modules.p2pNetworkClient.newNetworkModulesP2PNetworkClient()
await thisObject.p2pNetworkClient.initialize()
thisObject.p2pNetworkClientIdentity = SA.projects.network.modules.p2pNetworkClientIdentity.newNetworkModulesP2PNetworkClientIdentity()
await thisObject.p2pNetworkClientIdentity.initialize()
/*
We will read all user profiles plugins and get from there our network identity.
*/
thisObject.userProfiles = SA.projects.network.modules.userProfiles.newNetworkModulesUserProfiles()
await thisObject.userProfiles.initialize(global.env.DESKTOP_APP_SIGNING_ACCOUNT, thisObject.p2pNetworkClient)
await thisObject.userProfiles.initialize(global.env.DESKTOP_APP_SIGNING_ACCOUNT, thisObject.p2pNetworkClientIdentity)
/*
We set up the P2P Network.
*/
@@ -44,7 +44,7 @@ exports.newDesktopApp = function newDesktopApp() {
thisObject.p2pNetworkPeers = SA.projects.network.modules.p2pNetworkPeers.newNetworkModulesP2PNetworkPeers()
await thisObject.p2pNetworkPeers.initialize(
'Network Client',
thisObject.p2pNetworkClient,
thisObject.p2pNetworkClientIdentity,
thisObject.p2pNetwork,
global.env.DESKTOP_APP_MAX_OUTGOING_PEERS
)
@@ -67,13 +67,13 @@ exports.newDesktopApp = function newDesktopApp() {
let express = require('./backend/src/index')
let expressPort = JSON.parse(DK.desktopApp.p2pNetworkClient.node.config).webPort;
let expressPort = JSON.parse(DK.desktopApp.p2pNetworkClientIdentity.node.config).webPort;
express.startExpress(expressPort,SA);
console.log('express Interface ................................................ Listening at port ' + expressPort);
/*TODO change this to have a definite port number*/
let react = require('./frontend/scripts/start')
let reactPort = (+JSON.parse(DK.desktopApp.p2pNetworkClient.node.config).webPort + 1);
let reactPort = (+JSON.parse(DK.desktopApp.p2pNetworkClientIdentity.node.config).webPort + 1);
react.start(reactPort);
console.log('react Interface ................................................ Listening at port ' + reactPort);
}
+3 -1
View File
@@ -1,7 +1,9 @@
exports.newNetworkModulesP2PNetwork = function newNetworkModulesP2PNetwork() {
/*
This module represents the P2P Network and it is needed to enable the connection
of this current node to other peers.
of this current node to other peers. The main feature here is the array on
nodes that we can potengially connect to. When we are running with the role of a Network Peer,
the list of available nodes will exclude ourselves.
*/
let thisObject = {
p2pNodesToConnect: undefined,
@@ -1,4 +1,4 @@
exports.newNetworkModulesP2PNetworkClient = function newNetworkModulesP2PNetworkClient() {
exports.newNetworkModulesP2PNetworkClientIdentity = function newNetworkModulesP2PNetworkClientIdentity() {
/*
This module represents the P2P Network Client running at the moment.
*/
+12 -2
View File
@@ -1,6 +1,16 @@
exports.newNetworkModulesP2PNetworkPeers = function newNetworkModulesP2PNetworkPeers() {
/*
This module holds the P2P Nodes we are coonected to.
Here we implement the mechanism to connect via websockets
to a set of Network Nodes and keep ourselves connected to them
so that when it is needed we can send messages to them, but also
we will receive incomming messages from them.
If connections are lost, we will re establish them so as to keep
ourselves connected to the configured amount of network nodes.
When an outgoing message is received, it will be rounted to one
of our pool of connected nodes.
*/
let thisObject = {
peers: undefined,
@@ -23,7 +33,7 @@ exports.newNetworkModulesP2PNetworkPeers = function newNetworkModulesP2PNetworkP
async function initialize(
callerRole,
p2pNetworkIdentity,
p2pNetworkClientIdentity,
p2pNetwork,
maxOutgoingPeers
) {
@@ -48,7 +58,7 @@ exports.newNetworkModulesP2PNetworkPeers = function newNetworkModulesP2PNetworkP
peer.p2pNetworkNode = p2pNetwork.p2pNodesToConnect[i]
if (isPeerConnected(peer) === true) { continue }
peer.webSocketsClient = SA.projects.network.modules.webSocketsNetworkClient.newNetworkModulesWebSocketsNetworkClient()
await peer.webSocketsClient.initialize(callerRole, p2pNetworkIdentity, peer.p2pNetworkNode, onConnectionClosed)
await peer.webSocketsClient.initialize(callerRole, p2pNetworkClientIdentity, peer.p2pNetworkNode, onConnectionClosed)
.then(addPeer)
.catch(onError)
@@ -1,7 +1,12 @@
exports.newNetworkModulesP2PNetworkHead = function newNetworkModulesP2PNetworkHead() {
exports.newNetworkModulesP2PNetworkStart = function newNetworkModulesP2PNetworkStart() {
/*
This has identified which are the nodes at the head of the network worm,
and it is ready to send messages to it.
This module assumes the p2p network is formed by network nodes connected to each other
not randombly but as a directed acyclic graph.
https://en.wikipedia.org/wiki/Directed_acyclic_graph
This module will identify which are the Start Nodes of the DAG, and be ready to send
messages via the http interface of nodes to some of them.
*/
let thisObject = {
peers: undefined,
@@ -24,7 +29,7 @@ exports.newNetworkModulesP2PNetworkHead = function newNetworkModulesP2PNetworkHe
async function initialize(
callerRole,
p2pNetworkIdentity,
p2pNetworkClientIdentity,
p2pNetwork,
maxOutgoingPeers
) {
@@ -69,7 +74,7 @@ exports.newNetworkModulesP2PNetworkHead = function newNetworkModulesP2PNetworkHe
*/
if (peer.httpClient === undefined) {
peer.httpClient = SA.projects.network.modules.webSocketsNetworkClient.newNetworkModulesHttpNetworkClient()
peer.httpClient.initialize(callerRole, p2pNetworkIdentity, peer.p2pNetworkNode)
peer.httpClient.initialize(callerRole, p2pNetworkClientIdentity, peer.p2pNetworkNode)
}
return await peer.httpClient.testConnection()
+4 -4
View File
@@ -18,7 +18,7 @@ exports.newNetworkModulesUserProfiles = function newNetworkModulesUserProfiles()
}
async function initialize(nodeCodeName, p2pNetworkIdentity) {
async function initialize(nodeCodeName, p2pNetworkClientIdentity) {
await loadAppSchemas()
await loadProfiles()
@@ -158,9 +158,9 @@ exports.newNetworkModulesUserProfiles = function newNetworkModulesUserProfiles()
if (
networkClient.id === SA.secrets.map.get(nodeCodeName).nodeId
) {
p2pNetworkIdentity.node = networkClient
p2pNetworkIdentity.blockchainAccount = blockchainAccount
p2pNetworkIdentity.userProfile = userProfile
p2pNetworkClientIdentity.node = networkClient
p2pNetworkClientIdentity.blockchainAccount = blockchainAccount
p2pNetworkClientIdentity.userProfile = userProfile
}
}
}
+7 -7
View File
@@ -1639,15 +1639,15 @@
},
{
"name": "P2P Network Head",
"propertyName": "p2pNetworkHead",
"functionName": "newNetworkModulesP2PNetworkHead",
"fileName": "P2PNetworkHead.js"
"propertyName": "p2pNetworkStart",
"functionName": "newNetworkModulesP2PNetworkStart",
"fileName": "P2PNetworkStart.js"
},
{
"name": "P2P Network Client",
"propertyName": "p2pNetworkClient",
"functionName": "newNetworkModulesP2PNetworkClient",
"fileName": "P2PNetworkClient.js"
"name": "P2P Network Client Identity",
"propertyName": "p2pNetworkClientIdentity",
"functionName": "newNetworkModulesP2PNetworkClientIdentity",
"fileName": "P2PNetworkClientIdentity.js"
}
],
"utilities": [
@@ -15,7 +15,7 @@ exports.newDesktopModulesHttpInterface = function newDesktopModulesHttpInterface
}
function initialize() {
let port = JSON.parse(DK.desktopApp.p2pNetworkClient.node.config).webPort
let port = JSON.parse(DK.desktopApp.p2pNetworkClientIdentity.node.config).webPort
/*
We will create an HTTP Server and leave it running forever.
*/
@@ -42,7 +42,7 @@ exports.newDesktopModulesHttpInterface = function newDesktopModulesHttpInterface
break
case 'ClientNode':
{
SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(DK.desktopApp.p2pNetworkClient.node), httpResponse)
SA.projects.foundations.utilities.httpResponses.respondWithContent(JSON.stringify(DK.desktopApp.p2pNetworkClientIdentity.node), httpResponse)
}
break
case 'ProjectsSchema':
+17 -13
View File
@@ -88,7 +88,7 @@ exports.newTaskServer = function newTaskServer() {
async function bootingProcess() {
try {
initializeProjectDefinitionNode()
await setupNetwork()
await setupP2PNetwork()
setupTaskHeartbeats()
startProcesses()
@@ -114,7 +114,7 @@ exports.newTaskServer = function newTaskServer() {
}
}
async function setupNetwork() {
async function setupP2PNetwork() {
/*
If we received a App Server Reference Node and a Signing Account, then we will connect to the P2P Network
*/
@@ -127,37 +127,41 @@ exports.newTaskServer = function newTaskServer() {
}
TS.projects.foundations.globals.taskConstants.P2P_NETWORK = {}
/*
We set up ourselves as a Network Client.
We set up the object that will hold our p2p network client identity, meaning the identity we will present to the network.
*/
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkClient = SA.projects.network.modules.p2pNetworkClient.newNetworkModulesP2PNetworkClient()
await TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkClient.initialize()
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkClientIdentity = SA.projects.network.modules.p2pNetworkClientIdentity.newNetworkModulesP2PNetworkClientIdentity()
await TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkClientIdentity.initialize()
/*
We will read all user profiles plugins and get from there our network identity.
We will read all user profiles plugins, store them in memory and get from there our own network client identity.
*/
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.userProfiles = SA.projects.network.modules.userProfiles.newNetworkModulesUserProfiles()
await TS.projects.foundations.globals.taskConstants.P2P_NETWORK.userProfiles.initialize(TS.projects.foundations.globals.taskConstants.TASK_NODE.taskServerAppReference.referenceParent.config.codeName, TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkClient)
await TS.projects.foundations.globals.taskConstants.P2P_NETWORK.userProfiles.initialize(
TS.projects.foundations.globals.taskConstants.TASK_NODE.taskServerAppReference.referenceParent.config.codeName,
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkClientIdentity
)
/*
We set up the P2P Network.
We set up the P2P Network, meaning the array of nodes we will be able to connect to.
*/
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork()
await TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetwork.initialize('Network Client')
/*
Set up the connections to network peers nodes. These connections will be used to consume signals.
In this context peers means network nodes with a similar ranking that our network client identity.
*/
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkPeers = SA.projects.network.modules.p2pNetworkPeers.newNetworkModulesP2PNetworkPeers()
await TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkPeers.initialize(
'Network Client',
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkClient,
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkClientIdentity,
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetwork,
global.env.TASK_SERVER_APP_MAX_OUTGOING_PEERS
)
/*
Set up the connections to network head nodes. These connections will be used to send signals.
Set up the connections to network start nodes. These connections will be used to send signals.
*/
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkPeers = SA.projects.network.modules.p2pNetworkPeers.newNetworkModulesP2PNetworkHead()
await TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkPeers.initialize(
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkStart = SA.projects.network.modules.p2pNetworkPeers.newNetworkModulesP2PNetworkStart()
await TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkStart.initialize(
'Network Client',
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkClient,
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetworkClientIdentity,
TS.projects.foundations.globals.taskConstants.P2P_NETWORK.p2pNetwork,
global.env.TASK_SERVER_APP_MAX_OUTGOING_HEADS
)