diff --git a/Desktop/DesktopApp.js b/Desktop/DesktopApp.js index 02e44aa7a..fc5396f93 100644 --- a/Desktop/DesktopApp.js +++ b/Desktop/DesktopApp.js @@ -1,126 +1,56 @@ exports.newDesktopApp = function newDesktopApp() { let thisObject = { + socialGraph: undefined, + p2pMetwork: undefined, + webSocketsClient: undefined, + webSocketsInterface: undefined, run: run } + DK.desktopApp = thisObject + return thisObject async function run() { - /* - Here we will store our running objects: - */ - DK.running = {} /* This is the Personal Social Graph for the user running this App. */ - DK.running.socialGraph = DK.projects.socialTrading.modules.socialGraph.newSocialTradingModulesSocialGraph() - await DK.running.socialGraph.initialize() + thisObject.socialGraph = DK.projects.socialTrading.modules.socialGraph.newSocialTradingModulesSocialGraph() + await thisObject.socialGraph.initialize() + /* + We can join now the P2P Network as a client app. + */ + thisObject.p2pMetwork = SA.projects.network.modules.p2pMetwork.newNetworkModulesP2PNetwork() + await thisObject.p2pMetwork.initialize('Network Client') + /* + Here we will pick a Network Node from all users profiles available that do have a Network Node running. // TODO + In the meantime, we will assume that we have chosen the following Network Node to connect to. + */ + selectedNetworkNode = { + userProfileHandle: "Luis-Fernando-Molina", + blockchainAccount: "0xeBDCB7a73c4796ca9F025d005630eCe773dd9e54", + ranking: 0, + host: "localhost", + port: global.env.NETWORK_WEB_SOCKETS_INTERFACE_PORT + } /* This is the Web Sockets client that interacts with the Superalgos Network. */ - DK.running.webSocketsClient = SA.projects.network.modules.webSocketsAppClient.newNetworkModulesWebSocketsAppClient() - await DK.running.webSocketsClient.initialize() + thisObject.webSocketsClient = SA.projects.network.modules.webSocketsAppClient.newNetworkModulesWebSocketsAppClient() + 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.*/ - - DK.running.webSocketsInterface = DK.projects.socialTrading.modules.webSocketsInterface.newDesktopModulesWebSocketsInterface() - DK.running.webSocketsInterface.initialize() + /* + These are the Network Interfaces by which the Web App interacts with this Desktop Client. + */ + thisObject.webSocketsInterface = DK.projects.socialTrading.modules.webSocketsInterface.newDesktopModulesWebSocketsInterface() + thisObject.webSocketsInterface.initialize() console.log('Desktop Client Web Sockets Interface ....................................... Listening at port ' + global.env.DESKTOP_WEB_SOCKETS_INTERFACE_PORT) - DK.running.httpInterface = DK.projects.socialTrading.modules.httpInterface.newDesktopModulesHttpInterface() - DK.running.httpInterface.initialize() + thisObject.webSocketsClient = DK.projects.socialTrading.modules.httpInterface.newDesktopModulesHttpInterface() + thisObject.httpInterface.initialize() console.log('Desktop Client Http Interface .............................................. Listening at port ' + global.env.DESKTOP_HTTP_INTERFACE_PORT) - return - - let queryMessage - let query - /* - Test Query User Profiles. - */ - queryMessage = { - queryType: SA.projects.socialTrading.globals.queryTypes.USER_PROFILES, - emitterUserProfileId: SA.secrets.map.get(global.env.DESKTOP_APP_SIGNING_ACCOUNT).userProfileId, - initialIndex: 'Last', - amountRequested: 10, - direction: 'Past' - } - - query = { - requestType: 'Query', - queryMessage: JSON.stringify(queryMessage) - } - - await DK.webSocketsClient.sendMessage( - JSON.stringify(query) - ) - .then(showProfiles) - .catch(onError) - - async function showProfiles(profiles) { - console.log(profiles) - - for (let i = 0; i < profiles.length; i++) { - let profile = profiles[i] - - /* - Test Following Profiles. - */ - let eventMessage = { - eventId: SA.projects.foundations.utilities.miscellaneousFunctions.genereteUniqueId(), - eventType: SA.projects.socialTrading.globals.eventTypes.FOLLOW_USER_PROFILE, - emitterUserProfileId: SA.secrets.map.get(global.env.DESKTOP_APP_SIGNING_ACCOUNT).userProfileId, - targetUserProfileId: profile.userProfileId - } - - let event = { - requestType: 'Event', - eventMessage: JSON.stringify(eventMessage) - } - - await DK.webSocketsClient.sendMessage( - JSON.stringify(event) - ) - .then(followAdded) - .catch(onError) - - function followAdded() { - console.log("User Profile " + profile.userProfileHandle + " followed.") - } - } - } - /* - Test Query User Profile Stats. - */ - queryMessage = { - queryType: SA.projects.socialTrading.globals.queryTypes.USER_PROFILE_STATS, - emitterUserProfileId: SA.secrets.map.get(global.env.DESKTOP_APP_SIGNING_ACCOUNT).userProfileId, - targetUserProfileId: SA.secrets.map.get(global.env.DESKTOP_APP_SIGNING_ACCOUNT).userProfileId - } - - query = { - requestType: 'Query', - queryMessage: JSON.stringify(queryMessage) - } - - await DK.webSocketsClient.sendMessage( - JSON.stringify(query) - ) - .then(showProfilesStats) - .catch(onError) - - function showProfilesStats(profile) { - console.log(profile) - } - - /* - Error Handling - */ - function onError(errorMessage) { - console.log('[ERROR] Query not executed. ' + errorMessage) - } } } diff --git a/Network/NetwokNode.js b/Network/NetwokNode.js index 74ffc6326..5aa0f2eb9 100644 --- a/Network/NetwokNode.js +++ b/Network/NetwokNode.js @@ -29,6 +29,6 @@ exports.newNetworkNode = function newNetworkNode() { We can join now the P2P Network as a new peer there. */ thisObject.p2pMetwork = SA.projects.network.modules.p2pMetwork.newNetworkModulesP2PNetwork() - await thisObject.p2pMetwork.initialize() + await thisObject.p2pMetwork.initialize('Network Peer') } } diff --git a/Projects/Network/SA/Modules/P2PNetwork.js b/Projects/Network/SA/Modules/P2PNetwork.js index 890e03bbc..5f6a90bc2 100644 --- a/Projects/Network/SA/Modules/P2PNetwork.js +++ b/Projects/Network/SA/Modules/P2PNetwork.js @@ -4,7 +4,7 @@ exports.newNetworkModulesP2PNetwork = function newNetworkModulesP2PNetwork() { of this current node to other peers. */ let thisObject = { - joinTheNetworkAsPeer: joinTheNetworkAsPeer, + joinTheNetworkAsNetworkPeer: joinTheNetworkAsNetworkPeer, /* Framework Functions */ initialize: initialize, finalize: finalize @@ -16,8 +16,9 @@ exports.newNetworkModulesP2PNetwork = function newNetworkModulesP2PNetwork() { } - async function initialize() { - p2pNodesToConnect = [] + async function initialize(callerRole) { + + let p2pNodesToConnect = [] /* We will setup here the maps and arryas we will need to operate within the P2P Network. */ @@ -28,9 +29,18 @@ exports.newNetworkModulesP2PNetwork = function newNetworkModulesP2PNetwork() { p2pNodesToConnect.push(p2pNetworkNode) } } + + switch (callerRole) { + case 'Network Client': { + break + } + case 'Network Peer': { + break + } + } } - function joinTheNetworkAsPeer() { + function joinTheNetworkAsNetworkPeer() { /* To join the network we will need to thorugh the list of Network Nodes and pick one to connect to. */ diff --git a/Projects/Network/SA/Modules/WebSocketsAppClient.js b/Projects/Network/SA/Modules/WebSocketsAppClient.js index 2fe199596..9d08ed187 100644 --- a/Projects/Network/SA/Modules/WebSocketsAppClient.js +++ b/Projects/Network/SA/Modules/WebSocketsAppClient.js @@ -10,7 +10,6 @@ exports.newNetworkModulesWebSocketsAppClient = function newNetworkModulesWebSock let web3 let called = {} - let selectedNetworkNode // This is a Network Node we pick to try to connect to. let onMessageFunctionsMap = new Map() return thisObject @@ -26,27 +25,14 @@ exports.newNetworkModulesWebSocketsAppClient = function newNetworkModulesWebSock onMessageFunctionsMap = undefined } - async function initialize() { - /* - Here we will pick a Network Node from all users profiles available that do have a Network Node running. // TODO - In the meantime, we will assume that we have chosen the following Network Node to connect to. - */ + async function initialize(callerRole, p2pNetworkNode) { + web3 = new SA.nodeModules.web3() - - selectedNetworkNode = { - userProfileHandle: "Luis-Fernando-Molina", - blockchainAccount: "0xeBDCB7a73c4796ca9F025d005630eCe773dd9e54", - ranking: 0, - host: "localhost", - port: global.env.NETWORK_WEB_SOCKETS_INTERFACE_PORT - } - - socketClient = new SA.nodeModules.ws('ws://' + selectedNetworkNode.host + ':' + selectedNetworkNode.port) - - await setUpWebsocketClient() + socketClient = new SA.nodeModules.ws('ws://' + p2pNetworkNode.host + ':' + p2pNetworkNode.port) + await setUpWebsocketClient(callerRole, p2pNetworkNode) } - async function setUpWebsocketClient() { + async function setUpWebsocketClient(callerRole) { return new Promise(connectToNewtwork) @@ -85,7 +71,7 @@ exports.newNetworkModulesWebSocketsAppClient = function newNetworkModulesWebSock let message = { messageType: 'Handshake', - callerRole: 'Network Client', + callerRole: callerRole, callerProfileHandle: SA.secrets.map.get(global.env.DESKTOP_APP_SIGNING_ACCOUNT).userProfileHandle, callerTimestamp: callerTimestamp, step: 'One' @@ -116,7 +102,7 @@ exports.newNetworkModulesWebSocketsAppClient = function newNetworkModulesWebSock We will check that the blockchain account taken from the signature matches the one we have on record for the user profile of the Network Node we are calling. */ - if (called.blockchainAccount !== selectedNetworkNode.blockchainAccount) { + if (called.blockchainAccount !== p2pNetworkNode.blockchainAccount) { console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node called does not have the expected Profile Handle.') reject() return @@ -138,7 +124,7 @@ exports.newNetworkModulesWebSocketsAppClient = function newNetworkModulesWebSock We will check that the Network Node that responded has the same User Profile Handle that we have on record, otherwise something is wrong and we should not proceed. */ - if (signedMessage.calledProfileHandle !== selectedNetworkNode.userProfileHandle) { + if (signedMessage.calledProfileHandle !== p2pNetworkNode.userProfileHandle) { console.log('[ERROR] Web Sockets Client -> stepOneResponse -> The Network Node called does not have the expected Profile Handle.') reject() return diff --git a/Projects/Social-Trading/DK/modules/WebAppInterface.js b/Projects/Social-Trading/DK/modules/WebAppInterface.js index 2eedc20bd..e9b7c75f5 100644 --- a/Projects/Social-Trading/DK/modules/WebAppInterface.js +++ b/Projects/Social-Trading/DK/modules/WebAppInterface.js @@ -58,11 +58,11 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule response = { result: 'Ok', message: 'Web App Interface Query Processed.', - data: await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) + data: await DK.desktopApp.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) } } else { - let events = await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) + let events = await DK.desktopApp.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) for (let i = 0; i < events.length; i++) { let event = events[i] if (event.eventType === SA.projects.socialTrading.globals.eventTypes.NEW_USER_POST) { @@ -132,7 +132,7 @@ exports.newSocialTradingModulesWebAppInterface = function newSocialTradingModule let response = { result: 'Ok', message: 'Web App Interface Event Processed.', - data: await DK.running.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) + data: await DK.desktopApp.webSocketsClient.sendMessage(JSON.stringify(messageHeader)) } return response }