2022-01-23 19:51:23 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
|
|
import Agent from './agent/Agent.mjs';
|
2022-01-30 00:51:28 +00:00
|
|
|
import InfoBroker from './core/InfoBroker.mjs';
|
2022-01-23 19:51:23 +00:00
|
|
|
|
|
|
|
class SystemQuery {
|
|
|
|
constructor(config) {
|
|
|
|
this.config = config;
|
2022-01-30 00:51:28 +00:00
|
|
|
this.info = new InfoBroker();
|
2022-01-23 19:51:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
///
|
|
|
|
// 1: Create agent
|
|
|
|
///
|
|
|
|
this.agent = new Agent(this.config);
|
|
|
|
await this.agent.init();
|
|
|
|
|
|
|
|
///
|
|
|
|
// 2: Attach listeners
|
|
|
|
///
|
|
|
|
this.agent.on("message-query", this.handle_query.bind(this));
|
|
|
|
this.agent.on("message-query-response", this.handle_query_response.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
async handle_query(peer, msg) {
|
2022-01-30 00:51:28 +00:00
|
|
|
// 1: Validate input
|
|
|
|
if(typeof msg.table !== "string"
|
|
|
|
|| !this.info.is_valid_table(msg.table)) return;
|
2022-01-23 19:51:23 +00:00
|
|
|
|
2022-01-30 00:51:28 +00:00
|
|
|
// 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 });
|
2022-01-23 19:51:23 +00:00
|
|
|
}
|
|
|
|
async handle_query_response(peer, msg) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SystemQuery.Create = async function(config) {
|
|
|
|
let result = new SystemQuery(config);
|
|
|
|
await result.init();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default SystemQuery;
|