eventReceived of Client Interface Implemented
This commit is contained in:
1 parent
06d0f3bf53
commit
6858abf5e2
6 files changed
+238
-144
No files matched your search
@@ -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()
|
||||
|
||||
|
||||
@@ -1,181 +0,0 @@
|
||||
exports.newWebSocketsInterface = function newWebSocketsInterface() {
|
||||
|
||||
let thisObject = {
|
||||
initialize: initialize,
|
||||
finalize: finalize,
|
||||
run: run
|
||||
}
|
||||
|
||||
let socketServer
|
||||
let port = global.env.Network_WEB_SOCKETS_INTERFACE_PORT
|
||||
|
||||
return thisObject
|
||||
|
||||
function finalize() {
|
||||
|
||||
}
|
||||
|
||||
function initialize() {
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
try {
|
||||
let messageHeader = JSON.parse(message)
|
||||
/*
|
||||
We will run some validations.
|
||||
*/
|
||||
if (messageHeader.messageType === undefined) {
|
||||
let response = {
|
||||
result: 'Error',
|
||||
message: 'messageType Not Provided.'
|
||||
}
|
||||
socket.send(JSON.stringify(response))
|
||||
return
|
||||
}
|
||||
|
||||
switch (messageHeader.messageType) {
|
||||
case "Handshake": {
|
||||
handShakeProducedure(socket, caller, messageHeader)
|
||||
break
|
||||
|
||||
}
|
||||
case "Request": {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function handShakeProducedure(socket, caller, messageHeader) {
|
||||
/*
|
||||
The caller needs to identify itself as either a Network Client or Peer.
|
||||
*/
|
||||
if (messageHeader.callerRole === undefined) {
|
||||
let response = {
|
||||
result: 'Error',
|
||||
message: 'callerRole Not Provided.'
|
||||
}
|
||||
socket.send(JSON.stringify(response))
|
||||
return
|
||||
}
|
||||
|
||||
if (messageHeader.callerRole !== 'Network Client' && messageHeader.callerRole !== 'Network Peer') {
|
||||
let response = {
|
||||
result: 'Error',
|
||||
message: 'callerRole Not Supported.'
|
||||
}
|
||||
socket.send(JSON.stringify(response))
|
||||
return
|
||||
}
|
||||
|
||||
caller.role = messageHeader.callerRole
|
||||
/*
|
||||
We will identify the caller by the signature at the message.
|
||||
*/
|
||||
if (messageHeader.signature === undefined) {
|
||||
let response = {
|
||||
result: 'Error',
|
||||
message: 'signature Not Provided.'
|
||||
}
|
||||
socket.send(JSON.stringify(response))
|
||||
return
|
||||
}
|
||||
|
||||
let web3 = new SA.nodeModules.web3()
|
||||
let blockchainAccount = web3.eth.accounts.recover(messageHeader.signature)
|
||||
|
||||
if (caller.blockchainAccount === undefined) {
|
||||
let response = {
|
||||
result: 'Error',
|
||||
message: 'Bad signature.'
|
||||
}
|
||||
socket.send(JSON.stringify(response))
|
||||
return
|
||||
}
|
||||
/*
|
||||
The signature gives us the blockchain account, and the account the user profile.
|
||||
*/
|
||||
let userProfile = NT.projects.network.globals.memory.maps.USER_PROFILES_BY_BLOCHAIN_ACCOUNT.set(blockchainAccount)
|
||||
|
||||
if (userProfile === undefined) {
|
||||
let response = {
|
||||
result: 'Error',
|
||||
message: 'userProfile Not Found.'
|
||||
}
|
||||
socket.send(JSON.stringify(response))
|
||||
return
|
||||
}
|
||||
/*
|
||||
We will remember the user profile behind this caller.
|
||||
*/
|
||||
caller.userProfile = userProfile
|
||||
|
||||
let response = {
|
||||
result: 'Ok',
|
||||
message: 'Handshake Successful.'
|
||||
}
|
||||
socket.send(JSON.stringify(response))
|
||||
}
|
||||
|
||||
} catch (err) {
|
||||
console.log('[ERROR] Network -> Web Sockets Interface -> run -> setUpWebSocketServer -> err.message = ' + err.message.substring(0, 1000))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
exports.newPeerInterface = function newPeerInterface() {
|
||||
|
||||
let thisObject = {
|
||||
initialize: initialize,
|
||||
finalize: finalize,
|
||||
run: run
|
||||
}
|
||||
|
||||
return thisObject
|
||||
|
||||
function finalize() {
|
||||
|
||||
}
|
||||
|
||||
function initialize() {
|
||||
|
||||
}
|
||||
|
||||
function run() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user