eventReceived of Client Interface Implemented

This commit is contained in:
Luis Fernando Molina committed 2021-08-31 10:53:34 +02:00
1 parent 06d0f3bf53
commit 6858abf5e2
6 files changed
+238 -144

No files matched your search

+5
View File
@@ -45,6 +45,11 @@ SA.nodeModules = {
web3: require('web3'),
ws: require('ws')
}
/*
Let's start the Network Interfaces
*/
NT.webSocketsInterface = NT.projects.network.modules.webSocketsInterface.newNetworkModulesWebSocketsInterface()
NT.webSocketsInterface.initialize()
start()
-87
View File
@@ -1,87 +0,0 @@
exports.newClientInterface = function newClientInterface() {
/*
This module represents the Interface the Network Node have
with Network Clients connected to it. There are two things
Network Clients can do with this Network Node:
* Send an Event that happened at the Network Client to the Network.
* Make a Query to the Network Node for info needed at the Network Client.
*/
let thisObject = {
eventReceived: eventReceived,
queryReceived: queryReceived,
initialize: initialize,
finalize: finalize
}
return thisObject
function finalize() {
}
function initialize() {
}
function eventReceived(eventMessage) {
/*
We expect here a JSON string with some or all of the following properties:
{
"eventId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"eventType": 10,
"emitterUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"emitterBotProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetBBotProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"emitterPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"timestamp": 124234234234,
"botAsset": "BTC",
"botExchange": "Binance",
"botEnabled": true
}
*/
let eventReceived = JSON.parse(eventMessage)
if (NT.projects.network.globals.memory.maps.EVENTS.get(eventReceived.eventId) !== undefined) {
throw ('Event Already Exists.')
}
let event = NT.projects.network.modules.event.newNetworkModulesEvent()
event.initialize(eventReceived)
NT.projects.network.globals.memory.maps.EVENTS.set(eventReceived.eventId, event)
}
function queryReceived(queryMessage) {
/*
We expect here a JSON string with some or all of the following properties:
{
"equeryId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"queryType": 10,
"emitterUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"emitterUserProfileHandle": "Luis-Fernando-Molina",
"targetUserProfileHandle": "Luis-Fernando-Molina",
"emitterBotProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetBBotProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"emitterPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"timestamp": 124234234234,
"botAsset": "BTC",
"botExchange": "Binance",
"botEnabled": true,
"initialIndex": 35,
"amountRequested": 50,
"direction": "Past"
}
*/
let queryReceived = JSON.parse(queryMessage)
let query = NT.projects.network.modules.queriesQuery.newNetworkModulesQuery()
query.initialize(queryReceived)
return query.execute()
}
}
@@ -0,0 +1,166 @@
exports.newNetworkModulesClientInterface = function newNetworkModulesClientInterface() {
/*
This module represents the Interface the Network Node have
with Network Clients connected to it. There are two things
Network Clients can do with this Network Node:
* Send an Event that happened at the Network Client to the Network.
* Make a Query to the Network Node for info needed at the Network Client.
*/
let thisObject = {
messageReceived: messageReceived,
initialize: initialize,
finalize: finalize
}
return thisObject
function finalize() {
}
function initialize() {
}
async function messageReceived(message, userProfile) {
let messageHeader
try {
messageHeader = JSON.parse(message)
} catch (err) {
let response = {
result: 'Error',
message: 'Client Interface message Not Coorrect JSON Format.'
}
return response
}
if (messageHeader.messageType === undefined) {
let response = {
result: 'Error',
message: 'Client Interface messageType Not Provided.'
}
return response
}
if (messageHeader.messageType !== 'Event' && messageHeader.messageType !== 'Query') {
let response = {
result: 'Error',
message: 'Client Interface messageType Not Supported.'
}
return response
}
switch (messageHeader.messageType) {
case 'Event': {
return await eventReceived(messageHeader.eventMessage, userProfile)
}
case 'Query': {
return await queryReceived(messageHeader.queryMessage, userProfile)
}
}
}
async function eventReceived(eventMessage, userProfile) {
/*
We expect here a JSON string with some or all of the following properties:
{
"eventId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"eventType": 10,
"emitterUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"emitterBotProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetBBotProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"emitterPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"timestamp": 124234234234,
"botAsset": "BTC",
"botExchange": "Binance",
"botEnabled": true
}
*/
let eventReceived
try {
eventReceived = JSON.parse(eventMessage)
} catch (err) {
let response = {
result: 'Error',
message: 'Client Interface eventMessage Not Coorrect JSON Format.'
}
return response
}
/*
At the Client Interface, events need to be emmitted by the same userProfile that is
connected at the Network Node.
*/
if (eventReceived.emitterUserProfileId !== userProfile.userProfileId) {
let response = {
result: 'Error',
message: 'Client Interface emitterUserProfileId !== userProfileId Connected to Network Node.'
}
return response
}
/*
We will not accept events that have already been processed.
*/
if (NT.projects.network.globals.memory.maps.EVENTS.get(eventReceived.eventId) !== undefined) {
let response = {
result: 'Error',
message: 'Client Interface Event Already Exists.'
}
return response
}
/*
Here we will process the event and change the state of the Social Graph.
*/
try {
let event = NT.projects.network.modules.event.newNetworkModulesEvent()
event.initialize(eventReceived)
NT.projects.network.globals.memory.maps.EVENTS.set(eventReceived.eventId, event)
} catch (err) {
/*
Any exception that happens while trying to change the state of the Social Graph,
will be returned to the caller without doing anything else here.
*/
let response = {
result: 'Error',
message: 'Client Interface ' + err.message
}
return response
}
}
async function queryReceived(queryMessage) {
/*
We expect here a JSON string with some or all of the following properties:
{
"equeryId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"queryType": 10,
"emitterUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetUserProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"emitterUserProfileHandle": "Luis-Fernando-Molina",
"targetUserProfileHandle": "Luis-Fernando-Molina",
"emitterBotProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetBBotProfileId": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"emitterPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"targetPostHash": "a8de78f0-c3e4-4a2a-b7e8-f659073969db",
"timestamp": 124234234234,
"botAsset": "BTC",
"botExchange": "Binance",
"botEnabled": true,
"initialIndex": 35,
"amountRequested": 50,
"direction": "Past"
}
*/
let queryReceived = JSON.parse(queryMessage)
let query = NT.projects.network.modules.queriesQuery.newNetworkModulesQuery()
query.initialize(queryReceived)
return query.execute()
}
}
@@ -1,4 +1,4 @@
exports.newPeerInterface = function newPeerInterface() {
exports.newNetworkModulesPeerInterface = function newNetworkModulesPeerInterface() {
let thisObject = {
initialize: initialize,
@@ -1,41 +1,51 @@
exports.newWebSocketsInterface = function newWebSocketsInterface() {
exports.newNetworkModulesWebSocketsInterface = function newNetworkModulesWebSocketsInterface() {
let thisObject = {
initialize: initialize,
finalize: finalize,
run: run
finalize: finalize
}
let socketServer
let port = global.env.Network_WEB_SOCKETS_INTERFACE_PORT
let clientInterface
let peerInterface
return thisObject
function finalize() {
socketServer = undefined
clientInterface = undefined
peerInterface = undefined
}
function initialize() {
socketServer = new SA.nodeModule.ws.Server({ port: global.env.Network_WEB_SOCKETS_INTERFACE_PORT })
clientInterface = NT.projects.network.modules.clientInterface.newNetworkModulesClientInterface()
peerInterface = NT.projects.network.modules.peerInterface.newNetworkModulesPeerInterface()
}
function run() {
setUpWebSocketServer()
}
function setUpWebSocketServer() {
try {
socketServer = new SA.nodeModule.ws.Server({ port: port })
socketServer.on('connection', onConnection)
function onConnection(socket) {
let caller = {}
socket.on('message', onMenssage)
function onMenssage(message) {
async function onMenssage(message) {
try {
let messageHeader = JSON.parse(message)
let messageHeader
try {
messageHeader = JSON.parse(message)
} catch (err) {
let response = {
result: 'Error',
message: 'messageHeader Not Coorrect JSON Format.'
}
socket.send(JSON.stringify(response))
return
}
/*
We will run some validations.
*/
@@ -50,60 +60,42 @@ exports.newWebSocketsInterface = function newWebSocketsInterface() {
switch (messageHeader.messageType) {
case "Handshake": {
handShakeProducedure(socket, caller, messageHeader)
handshakeProducedure(socket, caller, messageHeader)
break
}
case "Request": {
if (caller.userProfile === undefined) {
let response = {
result: 'Error',
message: 'Handshake Not Done Yet.'
}
socket.send(JSON.stringify(response))
return
}
switch (caller.role) {
case 'Network Client': {
let response = await clientInterface.messageReceived(messageHeader.payload, caller.userProfile)
socket.send(JSON.stringify(response))
break
}
case 'Network Peer': {
let response = await peerInterface.messageReceived(messageHeader.payload)
socket.send(JSON.stringify(response))
break
}
}
break
}
}
let origin = messageArray[0]
let nonce = messageArray[1]
let messageToEventServer = messageArray[2]
if (origin === 'Web Browser') {
try {
JSON.parse(messageToEventServer)
} catch (err) {
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> Message received from the browser is not a valid JSON. message = ' + message.substring(0, 1000))
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> Message received from the browser is not a valid JSON. messageToEventServer = ' + messageToEventServer)
return
}
let acknowledgeMessage = {
action: 'Acknowledge',
nonce: nonce
}
socket.send(JSON.stringify(acknowledgeMessage))
}
CL.servers.EVENT_SERVER.onMessage(messageToEventServer, onResponse)
function onResponse(message) {
socket.send(message)
}
} catch (err) {
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> Nonce received is less than Last Nonce. err = ' + err.stack)
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> err.stack = ' + err.stack)
}
}
}
function handShakeProducedure(socket, caller, messageHeader) {
function handshakeProducedure(socket, caller, messageHeader) {
/*
The caller needs to identify itself as either a Network Client or Peer.
*/
@@ -175,7 +167,7 @@ exports.newWebSocketsInterface = function newWebSocketsInterface() {
}
} catch (err) {
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> err.message = ' + err.message.substring(0, 1000))
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> err.stack = ' + err.stack)
}
}
}
+18
View File
@@ -1370,6 +1370,24 @@
"propertyName": "event",
"functionName": "newNetworkModulesEvent",
"fileName": "Event.js"
},
{
"name": "Client Interface",
"propertyName": "clientInterface",
"functionName": "newNetworkModulesClientInterface",
"fileName": "ClientInterface.js"
},
{
"name": "Peer Interface",
"propertyName": "peerInterface",
"functionName": "newNetworkModulesPeerInterface",
"fileName": "PeerInterface.js"
},
{
"name": "Web Sockets Interface",
"propertyName": "webSocketsInterface",
"functionName": "newNetworkModulesWebSocketsInterface",
"fileName": "WebSocketsInterface.js"
}
],
"utilities": [