Status of Network Clients working

This commit is contained in:
Luis Fernando Molina committed 2020-11-16 17:02:52 +01:00
1 parent 411a8ca2d7
commit 759ff66c9e
6 files changed
+217 -115

No files matched your search

+6 -6
View File
@@ -1,17 +1,17 @@
process.on('uncaughtException', function (err) {
if (err.message.indexOf("EADDRINUSE") > 0) {
console.log("A Superalgos Backend Server cannot be started. Reason: the port " + port + " is already in use by another application.")
console.log("A Superalgos Client cannot be started. Reason: the port " + port + " is already in use by another application.")
return
}
console.log('[ERROR] Backend Server -> server -> uncaughtException -> err.message = ' + err.message)
console.log('[ERROR] Backend Server -> server -> uncaughtException -> err.stack = ' + err.stack)
console.log('[ERROR] Client -> client-> uncaughtException -> err.message = ' + err.message)
console.log('[ERROR] Client -> client-> uncaughtException -> err.stack = ' + err.stack)
process.exit(1)
})
process.on('unhandledRejection', (reason, p) => {
console.log('[ERROR] Backend Server -> server -> unhandledRejection -> reason = ' + JSON.stringify(reason))
console.log('[ERROR] Backend Server -> server -> unhandledRejection -> p = ' + JSON.stringify(p))
console.log('[ERROR] Client -> client-> unhandledRejection -> reason = ' + JSON.stringify(reason))
console.log('[ERROR] Client -> client-> unhandledRejection -> p = ' + JSON.stringify(p))
process.exit(1)
})
@@ -131,6 +131,6 @@ try {
} catch (err) {
console.log('[ERROR] Client -> Task Manager -> server -> Error = ' + err.stack)
console.log('[ERROR] Client -> Error = ' + err.stack)
}
+49 -3
View File
@@ -91,6 +91,46 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
switch (requestParameters[1]) {
case 'WEB3':
{
getBody(processRequest)
async function processRequest(body) {
try {
let params = JSON.parse(body)
switch (params.method) {
case 'getNetworkClientStatus': {
let status = await WEB3_SERVER.getNetworkClientStatus(
params.host,
params.port,
params.interface
)
respondWithContent(JSON.stringify(status), response)
return
}
default: {
respondWithContent(JSON.stringify({ error: 'Method ' + params.method + ' is invalid.' }), response)
}
}
} catch (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] httpInterface -> WEB3s -> Method call produced an error.') }
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] httpInterface -> WEB3s -> err.stack = ' + err.stack) }
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] httpInterface -> WEB3s -> Params Received = ' + body) }
let error = {
result: 'Fail Because',
message: err.message,
stack: err.stack
}
respondWithContent(JSON.stringify(error), response)
}
}
break
}
case 'CCXT':
{
getBody(processRequest)
@@ -482,7 +522,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
function onDirRead(err, fileList) {
if (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) }
if (CONSOLE_ERROR_LOG === true) { console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) }
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
return
} else {
@@ -501,6 +541,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
} catch (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) }
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] err.stack = ' + err.stack) }
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
return
}
@@ -515,7 +556,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
function onDirRead(err, fileList) {
if (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) }
if (CONSOLE_ERROR_LOG === true) { console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) }
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
return
} else {
@@ -531,6 +572,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
} catch (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) }
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] err.stack = ' + err.stack) }
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
return
}
@@ -647,6 +689,8 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
} catch (err) {
console.log('[ERROR] Error reading a directory content. filePath = ' + path)
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] err.stack = ' + err.stack) }
console.log(err.stack)
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
return
@@ -728,7 +772,7 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
function onDirRead(err, fileList) {
if (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) }
if (CONSOLE_ERROR_LOG === true) { console.log('[WARN] Error reading a directory content. filePath = ' + dirPath) }
if (CONSOLE_ERROR_LOG === true) { console.log(err.stack) }
return
} else {
@@ -747,6 +791,8 @@ exports.newHttpInterface = function newHttpInterface(WEB_SERVER, DATA_FILE_SERVE
}
} catch (err) {
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] Error reading a directory content. filePath = ' + dirPath) }
if (CONSOLE_ERROR_LOG === true) { console.log('[ERROR] err.stack = ' + err.stack) }
respondWithContent(JSON.stringify(global.DEFAULT_FAIL_RESPONSE), response)
return
}
+57 -3
View File
@@ -3,15 +3,19 @@ exports.newWeb3Server = function newWeb3Server() {
const MODULE = "Web3 Server"
let thisObject = {
getNetworkClientStatus: getNetworkClientStatus,
initialize: initialize,
finalize: finalize,
run: run
}
const Web3 = require('web3');
let web3Map = new Map()
return thisObject
function finalize() {
web3Map = undefined
}
function initialize() {
@@ -19,6 +23,56 @@ exports.newWeb3Server = function newWeb3Server() {
}
function run() {
}
}
async function getNetworkClientStatus(host, port, interface) {
let key = 'host (' + host + ') at port (' + port + ') via interface (' + interface + ')'
try {
let web3 = web3Map.get(key)
if (web3 === undefined) {
/* This means that we never got connected to this client before */
switch (interface) {
case 'ws': {
web3 = new Web3('ws://' + host + ':' + port + '');
break
}
case 'http': {
web3 = new Web3('http://' + host + ':' + port + '');
break
}
default: {
return { error: 'Interface parameter must be eithre ws or http. Received ' + interface }
}
}
return await testConnection()
async function testConnection() {
let accounts = await web3.eth.getAccounts()
if (web3.currentProvider.connected === false) {
return { error: 'Connection to ' + key + ' could not be established.' }
} else {
return await returnStatus(web3)
}
}
} else {
return await returnStatus(web3)
}
async function returnStatus(web3) {
let status = {}
status.isSyncing = await web3.eth.isSyncing()
status.chainId = await web3.eth.getChainId()
status.result = 'Ok'
return status
}
} catch(err) {
return { error: 'Could not connect to ' + key }
}
}
}