Add based query response system, info broker

The new InfoBroker class' job is to fetch system information and return 
it in a standardised format.
This commit is contained in:
Starbeamrainbowlabs 2022-01-30 00:51:28 +00:00
parent bee1ffc2de
commit e63111e64f
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
2 changed files with 43 additions and 0 deletions

View file

@ -3,10 +3,12 @@
import sysinfo from 'systeminformation'; import sysinfo from 'systeminformation';
import Agent from './agent/Agent.mjs'; import Agent from './agent/Agent.mjs';
import InfoBroker from './core/InfoBroker.mjs';
class SystemQuery { class SystemQuery {
constructor(config) { constructor(config) {
this.config = config; this.config = config;
this.info = new InfoBroker();
} }
async init() { async init() {
@ -24,7 +26,16 @@ class SystemQuery {
} }
async handle_query(peer, msg) { async handle_query(peer, msg) {
// 1: Validate input
if(typeof msg.table !== "string"
|| !this.info.is_valid_table(msg.table)) return;
// 2: Fetch system info
let table = await this.info.fetch_table(msg.table);
if(table === null) return;
// 3: Return to requester
await peer.send("query-response", { result: table });
} }
async handle_query_response(peer, msg) { async handle_query_response(peer, msg) {

View file

@ -0,0 +1,32 @@
"use strict";
import sysinfo from 'systeminformation';
import log from '../../lib/io/NamespacedLog.mjs'; const l = log("infobroker");
class InfoBroker {
constructor() {
this.allowed_tables = {
// name → sysinfo name
cpu: "cpu",
};
}
is_valid_table(name) {
return Object.keys(this.allowed_tables).includes(name);
}
async fetch_table(name) {
if(!(typeof name === "string"))
throw new Exception(`Error: Expected name to be of type string, but received value of type ${typeof name} instead.`);
if(!this.is_valid_table(name)) {
l.warn(`Unrecognised table '${name}' requested, returning null`);
return null;
}
return await sysinfo[this.allowed_tables[name]]();
}
}
export default InfoBroker;