Ready to Test Peer Broadcasting

This commit is contained in:
Luis Fernando Molina committed 2021-11-07 10:33:44 +01:00
1 parent 5324c94306
commit 8b28e9c09b
11 files changed
+151 -48

No files matched your search

+26 -8
View File
@@ -1,10 +1,12 @@
exports.newDesktopApp = function newDesktopApp() { exports.newDesktopApp = function newDesktopApp() {
let thisObject = { let thisObject = {
socialGraph: undefined, userProfiles: undefined,
p2pNetworkClient: undefined,
p2pNetwork: undefined, p2pNetwork: undefined,
webSocketsClient: undefined, webSocketsClient: undefined,
webSocketsInterface: undefined, webSocketsInterface: undefined,
socialGraph: undefined,
run: run run: run
} }
@@ -13,13 +15,23 @@ exports.newDesktopApp = function newDesktopApp() {
return thisObject return thisObject
async function run() { async function run() {
setupNetwork()
setupServices()
async function setupNetwork() {
/* /*
This is the Personal Social Graph for the user running this App. We set up ourselves as a Network Client.
*/ */
thisObject.socialGraph = DK.projects.socialTrading.modules.socialGraph.newSocialTradingModulesSocialGraph() thisObject.p2pNetworkClient = SA.projects.network.modules.p2pNetworkClient.newNetworkModulesP2PNetworkClient()
await thisObject.socialGraph.initialize() await thisObject.p2pNetworkClient.initialize()
/* /*
We can join now the P2P Network as a client app. 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)
/*
We set up the P2P Network.
*/ */
thisObject.p2pNetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork() thisObject.p2pNetwork = SA.projects.network.modules.p2pNetwork.newNetworkModulesP2PNetwork()
await thisObject.p2pNetwork.initialize('Network Client') await thisObject.p2pNetwork.initialize('Network Client')
@@ -31,8 +43,15 @@ exports.newDesktopApp = function newDesktopApp() {
This is the Web Sockets client that interacts with the Superalgos Network. This is the Web Sockets client that interacts with the Superalgos Network.
*/ */
thisObject.webSocketsClient = SA.projects.network.modules.webSocketsNetworkClient.newNetworkModulesWebSocketsNetworkClient() thisObject.webSocketsClient = SA.projects.network.modules.webSocketsNetworkClient.newNetworkModulesWebSocketsNetworkClient()
await thisObject.webSocketsClient.initialize('Network Client', selectedNetworkNode) await thisObject.webSocketsClient.initialize('Network Client', thisObject.p2pNetworkClient, selectedNetworkNode)
}
async function setupServices() {
/*
This is the Personal Social Graph for the user running this App.
*/
thisObject.socialGraph = DK.projects.socialTrading.modules.socialGraph.newSocialTradingModulesSocialGraph()
await thisObject.socialGraph.initialize()
/* /*
These are the Network Interfaces by which the Web App interacts with this Desktop Client. These are the Network Interfaces by which the Web App interacts with this Desktop Client.
*/ */
@@ -40,10 +59,9 @@ exports.newDesktopApp = function newDesktopApp() {
thisObject.webSocketsInterface.initialize() thisObject.webSocketsInterface.initialize()
console.log('Desktop Client Web Sockets Interface ....................................... Listening at port ' + global.env.DESKTOP_WEB_SOCKETS_INTERFACE_PORT) console.log('Desktop Client Web Sockets Interface ....................................... Listening at port ' + global.env.DESKTOP_WEB_SOCKETS_INTERFACE_PORT)
thisObject.httpInterface = DK.projects.socialTrading.modules.httpInterface.newDesktopModulesHttpInterface() thisObject.httpInterface = DK.projects.socialTrading.modules.httpInterface.newDesktopModulesHttpInterface()
thisObject.httpInterface.initialize() thisObject.httpInterface.initialize()
console.log('Desktop Client Http Interface .............................................. Listening at port ' + global.env.DESKTOP_HTTP_INTERFACE_PORT) console.log('Desktop Client Http Interface .............................................. Listening at port ' + global.env.DESKTOP_HTTP_INTERFACE_PORT)
}
} }
} }
+34 -14
View File
@@ -1,11 +1,13 @@
exports.newNetworkNode = function newNetworkNode() { exports.newNetworkNode = function newNetworkNode() {
let thisObject = { let thisObject = {
userProfiles: undefined,
p2pNetwork: undefined,
p2pNetworkPeers: undefined,
p2pNetworkNode: undefined,
webSocketsInterface: undefined, webSocketsInterface: undefined,
socialGraphService: undefined, socialGraphService: undefined,
storage: undefined, storage: undefined,
p2pNetwork: undefined,
p2pNetworkPeers: undefined,
run: run run: run
} }
@@ -14,6 +16,34 @@ exports.newNetworkNode = function newNetworkNode() {
return thisObject return thisObject
async function run() { async function run() {
await setupNetwork()
await setupServices()
async function setupNetwork() {
/*
We set up ourselves as a Network Node.
*/
thisObject.p2pNetworkNode = SA.projects.network.modules.p2pNetworkNode.newNetworkModulesP2PNetworkNode()
await thisObject.p2pNetworkNode.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.P2P_NETWORK_NODE_SIGNING_ACCOUNT, thisObject.p2pNetworkNode)
/*
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()
}
async function setupServices() {
/* /*
The Social Graph Service is the first and for now, the only service this Network Node provides. The Social Graph Service is the first and for now, the only service this Network Node provides.
*/ */
@@ -25,21 +55,11 @@ exports.newNetworkNode = function newNetworkNode() {
thisObject.storage = NT.projects.socialTrading.modules.storage.newSocialTradingModulesStorage() thisObject.storage = NT.projects.socialTrading.modules.storage.newSocialTradingModulesStorage()
thisObject.storage.initialize() 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.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. 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 = NT.projects.network.modules.webSocketsInterface.newNetworkModulesWebSocketsInterface()
thisObject.webSocketsInterface.initialize() thisObject.webSocketsInterface.initialize()
console.log('Network Node Web Sockets Interface ....................................... Listening at port ' + JSON.parse(NT.networkNode.p2pNetwork.thisNetworkNode.node.config).webSocketsPort) console.log('Network Node Web Sockets Interface ....................................... Listening at port ' + JSON.parse(NT.networkNode.p2pNetworkNode.node.config).webSocketsPort)
}
} }
} }
@@ -39,7 +39,7 @@ exports.newNetworkModulesWebSocketsInterface = function newNetworkModulesWebSock
} }
function initialize() { function initialize() {
let port = JSON.parse(NT.networkNode.p2pNetwork.thisNetworkNode.node.config).webSocketsPort let port = JSON.parse(NT.networkNode.p2pNetworkNode.node.config).webSocketsPort
thisObject.socketServer = new SA.nodeModules.ws.Server({ port: port }) thisObject.socketServer = new SA.nodeModules.ws.Server({ port: port })
thisObject.clientInterface = NT.projects.socialTrading.modules.clientInterface.newSocialTradingModulesClientInterface() thisObject.clientInterface = NT.projects.socialTrading.modules.clientInterface.newSocialTradingModulesClientInterface()
@@ -64,7 +64,8 @@ exports.newNetworkModulesWebSocketsInterface = function newNetworkModulesWebSock
let caller = { let caller = {
socket: socket, socket: socket,
userProfile: undefined, userProfile: undefined,
role: undefined role: undefined,
node: undefined
} }
caller.socket.id = SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId() caller.socket.id = SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId()
@@ -250,6 +251,32 @@ exports.newNetworkModulesWebSocketsInterface = function newNetworkModulesWebSock
} }
} }
/* /*
The caller needs to provide it's Node.
*/
if (messageHeader.callerNode === undefined) {
let response = {
result: 'Error',
message: 'node Not Provided.'
}
caller.socket.send(JSON.stringify(response))
caller.socket.close()
return
}
/*
The caller's Node needs to be parseable.
*/
try {
caller.node = JSON.parse(messageHeader.callerNode)
} catch (err) {
let response = {
result: 'Error',
message: 'node Not Coorrect JSON Format.'
}
caller.socket.send(JSON.stringify(response))
caller.socket.close()
return
}
/*
The caller needs to provide it's User Profile Handle. The caller needs to provide it's User Profile Handle.
*/ */
if (messageHeader.callerProfileHandle === undefined) { if (messageHeader.callerProfileHandle === undefined) {
@@ -261,7 +288,6 @@ exports.newNetworkModulesWebSocketsInterface = function newNetworkModulesWebSock
caller.socket.close() caller.socket.close()
return return
} }
caller.userProfileHandle = messageHeader.callerProfileHandle caller.userProfileHandle = messageHeader.callerProfileHandle
/* /*
The caller needs to provide a callerTimestamp. The caller needs to provide a callerTimestamp.
@@ -491,13 +517,18 @@ exports.newNetworkModulesWebSocketsInterface = function newNetworkModulesWebSock
} }
function broadcastToPeers(message, caller) { function broadcastToPeers(message, caller) {
/*
The Boradcast to network peers is not done via
the network peers incomming connections, but
on the outgoing connections only.
*/
let callerIdToAVoid let callerIdToAVoid
if (caller.role === 'Network Peer') { if (caller.role === 'Network Peer') {
callerIdToAVoid = caller.socket.id callerIdToAVoid = caller.node.id
} }
for (let i = 0; i < thisObject.networkPeers.length; i++) { for (let i = 0; i < NT.networkNode.p2pNetworkPeers.outgoingConnectedPeers.length; i++) {
let networkPeer = thisObject.networkPeers[i] let networkPeer = NT.networkNode.p2pNetworkPeers.outgoingConnectedPeers[i]
if (networkPeer.socket.id === callerIdToAVoid) { continue } if (networkPeer.p2pNetworkNode.node.id === callerIdToAVoid) { continue }
networkPeer.socket.send(message) networkPeer.socket.send(message)
} }
} }
-10
View File
@@ -4,9 +4,7 @@ exports.newNetworkModulesP2PNetwork = function newNetworkModulesP2PNetwork() {
of this current node to other peers. of this current node to other peers.
*/ */
let thisObject = { let thisObject = {
joinTheNetworkAsNetworkPeer: joinTheNetworkAsNetworkPeer,
p2pNodesToConnect: undefined, p2pNodesToConnect: undefined,
thisNetworkNode: undefined,
/* Framework Functions */ /* Framework Functions */
initialize: initialize, initialize: initialize,
finalize: finalize finalize: finalize
@@ -38,18 +36,10 @@ exports.newNetworkModulesP2PNetwork = function newNetworkModulesP2PNetwork() {
let p2pNetworkNode = SA.projects.network.globals.memory.arrays.P2P_NETWORK_NODES[i] let p2pNetworkNode = SA.projects.network.globals.memory.arrays.P2P_NETWORK_NODES[i]
if (thisP2PNodeId !== p2pNetworkNode.node.id) { if (thisP2PNodeId !== p2pNetworkNode.node.id) {
thisObject.p2pNodesToConnect.push(p2pNetworkNode) thisObject.p2pNodesToConnect.push(p2pNetworkNode)
} else {
thisObject.thisNetworkNode = p2pNetworkNode
} }
} }
break break
} }
} }
} }
function joinTheNetworkAsNetworkPeer() {
/*
To join the network we will need to thorugh the list of Network Nodes and pick one to connect to.
*/
}
} }
@@ -0,0 +1,27 @@
exports.newNetworkModulesP2PNetworkClient = function newNetworkModulesP2PNetworkClient() {
/*
This module represents the P2P Network Client running at the moment.
*/
let thisObject = {
node: undefined,
blockchainAccount: undefined,
userProfile: undefined,
/* Framework Functions */
initialize: initialize,
finalize: finalize
}
return thisObject
function finalize() {
thisObject.node = undefined
thisObject.blockchainAccount = undefined
thisObject.userProfile = undefined
}
async function initialize() {
}
}
@@ -39,7 +39,7 @@ exports.newNetworkModulesP2PNetworkPeers = function newNetworkModulesP2PNetworkP
peer.p2pNetworkNode = NT.networkNode.p2pNetwork.p2pNodesToConnect[i] peer.p2pNetworkNode = NT.networkNode.p2pNetwork.p2pNodesToConnect[i]
if (isOutgoingConnectedPeer(peer) === true) { continue } if (isOutgoingConnectedPeer(peer) === true) { continue }
peer.webSocketsClient = SA.projects.network.modules.webSocketsNetworkClient.newNetworkModulesWebSocketsNetworkClient() peer.webSocketsClient = SA.projects.network.modules.webSocketsNetworkClient.newNetworkModulesWebSocketsNetworkClient()
await peer.webSocketsClient.initialize('Network Peer', peer.p2pNetworkNode, onConnectionClosed) await peer.webSocketsClient.initialize('Network Peer', NT.networkNode.p2pNetworkNode, peer.p2pNetworkNode, onConnectionClosed)
.then(addPeer) .then(addPeer)
.catch(onError) .catch(onError)
+11 -1
View File
@@ -18,7 +18,7 @@ exports.newNetworkModulesUserProfiles = function newNetworkModulesUserProfiles()
} }
async function initialize() { async function initialize(signingAccountChildCodeName, p2pNetworkIdentity) {
let pluginFileNames = await SA.projects.communityPlugins.utilities.plugins.getPluginFileNames( let pluginFileNames = await SA.projects.communityPlugins.utilities.plugins.getPluginFileNames(
'Governance', 'Governance',
@@ -91,6 +91,16 @@ exports.newNetworkModulesUserProfiles = function newNetworkModulesUserProfiles()
p2pNetworkNode.initialize(signingAccount.signingAccountChild, userProfile, blockchainAccount) p2pNetworkNode.initialize(signingAccount.signingAccountChild, userProfile, blockchainAccount)
SA.projects.network.globals.memory.arrays.P2P_NETWORK_NODES.push(p2pNetworkNode) SA.projects.network.globals.memory.arrays.P2P_NETWORK_NODES.push(p2pNetworkNode)
} }
/*
Now, we will extract the information from the User Profile, specifically of our identity at the p2p network.
*/
if (
signingAccount.signingAccountChild.id === SA.secrets.map.get(signingAccountChildCodeName).signingAccountChildId
) {
p2pNetworkIdentity.node = signingAccount.signingAccountChild
p2pNetworkIdentity.blockchainAccount = blockchainAccount
p2pNetworkIdentity.userProfile = userProfile
}
} }
} }
} }
@@ -8,6 +8,7 @@ exports.newNetworkModulesWebSocketsNetworkClient = function newNetworkModulesWeb
port: undefined, port: undefined,
callerRole: undefined, callerRole: undefined,
p2pNetworkNode: undefined, p2pNetworkNode: undefined,
p2pNetworkIdentity: undefined,
onConnectionClosedCallBack: undefined, onConnectionClosedCallBack: undefined,
sendMessage: sendMessage, sendMessage: sendMessage,
initialize: initialize, initialize: initialize,
@@ -29,6 +30,7 @@ exports.newNetworkModulesWebSocketsNetworkClient = function newNetworkModulesWeb
thisObject.port = undefined thisObject.port = undefined
thisObject.callerRole = undefined thisObject.callerRole = undefined
thisObject.p2pNetworkNode = undefined thisObject.p2pNetworkNode = undefined
thisObject.p2pNetworkIdentity = undefined
thisObject.onConnectionClosedCallBack = undefined thisObject.onConnectionClosedCallBack = undefined
web3 = undefined web3 = undefined
@@ -36,9 +38,10 @@ exports.newNetworkModulesWebSocketsNetworkClient = function newNetworkModulesWeb
onMessageFunctionsMap = undefined onMessageFunctionsMap = undefined
} }
async function initialize(callerRole, p2pNetworkNode, onConnectionClosedCallBack) { async function initialize(callerRole, p2pNetworkIdentity, p2pNetworkNode, onConnectionClosedCallBack) {
thisObject.callerRole = callerRole thisObject.callerRole = callerRole
thisObject.p2pNetworkIdentity = p2pNetworkIdentity
thisObject.p2pNetworkNode = p2pNetworkNode thisObject.p2pNetworkNode = p2pNetworkNode
thisObject.onConnectionClosedCallBack = onConnectionClosedCallBack thisObject.onConnectionClosedCallBack = onConnectionClosedCallBack
@@ -99,6 +102,7 @@ exports.newNetworkModulesWebSocketsNetworkClient = function newNetworkModulesWeb
callerRole: thisObject.callerRole, callerRole: thisObject.callerRole,
callerProfileHandle: SA.secrets.map.get(global.env.DESKTOP_APP_SIGNING_ACCOUNT).userProfileHandle, callerProfileHandle: SA.secrets.map.get(global.env.DESKTOP_APP_SIGNING_ACCOUNT).userProfileHandle,
callerTimestamp: callerTimestamp, callerTimestamp: callerTimestamp,
callerNode: JSON.stringify(thisObject.p2pNetworkIdentity.node),
step: 'One' step: 'One'
} }
thisObject.socketClient.send(JSON.stringify(message)) thisObject.socketClient.send(JSON.stringify(message))
+6
View File
@@ -1419,6 +1419,12 @@
"propertyName": "p2pNetworkPeers", "propertyName": "p2pNetworkPeers",
"functionName": "newNetworkModulesP2PNetworkPeers", "functionName": "newNetworkModulesP2PNetworkPeers",
"fileName": "P2PNetworkPeers.js" "fileName": "P2PNetworkPeers.js"
},
{
"name": "P2P Network Client",
"propertyName": "p2pNetworkClient",
"functionName": "newNetworkModulesP2PNetworkClient",
"fileName": "P2PNetworkClient.js"
} }
], ],
"utilities": [ "utilities": [
@@ -14,7 +14,6 @@ exports.newSocialTradingModulesSocialGraph = function newSocialTradingModulesSoc
no need to ask it to a network node. no need to ask it to a network node.
*/ */
let thisObject = { let thisObject = {
userProfiles: undefined,
initialize: initialize, initialize: initialize,
finalize: finalize finalize: finalize
} }
@@ -26,7 +25,6 @@ exports.newSocialTradingModulesSocialGraph = function newSocialTradingModulesSoc
} }
async function initialize() { async function initialize() {
thisObject.userProfiles = SA.projects.network.modules.userProfiles.newNetworkModulesUserProfiles()
await thisObject.userProfiles.initialize()
} }
} }
@@ -9,7 +9,7 @@ exports.newNetworkModulesSocialGraph = function newNetworkModulesSocialGraph() {
their posts. their posts.
*/ */
let thisObject = { let thisObject = {
userProfiles: undefined,
/* Framework Functions */ /* Framework Functions */
initialize: initialize, initialize: initialize,
finalize: finalize finalize: finalize
@@ -22,7 +22,6 @@ exports.newNetworkModulesSocialGraph = function newNetworkModulesSocialGraph() {
} }
async function initialize() { async function initialize() {
thisObject.userProfiles = SA.projects.network.modules.userProfiles.newNetworkModulesUserProfiles()
await thisObject.userProfiles.initialize()
} }
} }