2022-01-23 19:51:23 +00:00
"use strict" ;
2022-02-01 03:05:27 +00:00
import { once , EventEmitter } from 'events' ;
2022-01-23 19:51:23 +00:00
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
2022-02-09 03:06:52 +00:00
import ItemQueue from './async/ItemQueue.mjs' ;
2022-02-01 03:05:27 +00:00
class SystemQuery extends EventEmitter {
2022-01-31 03:06:34 +00:00
constructor ( config , mode = "agent" ) {
2022-02-01 03:05:27 +00:00
super ( ) ;
2022-01-31 03:06:34 +00:00
// The operating mode. Possible values: agent [default], query_client
// TODO: Is this the best way of doing this? Maybe we should have a separate class for this? I'm not sure.
this . mode = mode ;
2022-01-23 19:51:23 +00:00
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
2022-02-09 03:06:52 +00:00
if ( typeof msg . name !== "string"
|| ! this . info . is _valid _table ( msg . name ) ) return ;
2022-01-23 19:51:23 +00:00
2022-01-30 00:51:28 +00:00
// 2: Fetch system info
2022-02-09 03:06:52 +00:00
let table = await this . info . fetch _table ( msg . name ) ;
2022-01-30 00:51:28 +00:00
if ( table === null ) return ;
// 3: Return to requester
2022-02-09 03:06:52 +00:00
await peer . send ( "query-response" , { name : msg . name , table } ) ;
2022-01-23 19:51:23 +00:00
}
async handle _query _response ( peer , msg ) {
}
2022-01-31 03:06:34 +00:00
2022-02-01 03:05:27 +00:00
async capture _query _response ( event _name , ac ) {
return await once ( this . agent , ` message- ${ event _name } ` , { signal : ac } ) ;
}
2022-01-31 03:06:34 +00:00
2022-02-01 03:05:27 +00:00
* fetch _table ( name ) {
2022-01-31 03:06:34 +00:00
// If it isn't valid for us, it ain't gonna be valid for anyone else....
if ( ! this . info . is _valid _table ( name ) ) return null ;
2022-02-09 03:06:52 +00:00
const queue = new ItemQueue ( ) ;
2022-02-01 03:05:27 +00:00
const handle _response = ( peer , msg ) => {
2022-01-31 03:06:34 +00:00
// TODO: Validate response. Is it the right table? Is it even a table? Note that multiple fetch_table calls may be running in parallel, so we should not make too much of a fuss if we get the wrong table by accident.
// TODO: It would be seriously cool to have fetch_table() be an async generator that yields pairs of peer ids and tables as they come in.
2022-02-09 03:06:52 +00:00
if ( typeof msg !== "object"
|| typeof msg . table !== "object"
|| typeof msg . name !== "string" ) {
l . debug ( ` Discarding invalid table from peer ${ peer . id _short } ` ) ;
return ;
}
// If it's not the right table, ignore it
if ( msg . name !== name ) return ;
queue . push ( { peer , table : msg . table } ) ;
2022-01-31 03:06:34 +00:00
} ;
this . on ( "message-query-response" , handle _response ) ;
2022-02-09 03:06:52 +00:00
// Only *after* we have our listeners in place do we then broadcast the
// query. Note that despite not having entered the below while loop yet,
// we do not drop any messages due to the use of the ItemQueue.
this . agent . broadcast ( ` query ` , { name } ) ;
while ( peers _seen . length < this . agent . connected _peers . length ) {
let next = queue . pop ( this . config . net . table _config ) ;
if ( typeof next === "undefined" ) // We timed out
break ;
yield next ;
}
this . off ( "message-query-response" , handle _response ) ;
2022-01-31 03:06:34 +00:00
// FUTURE: Add a cache here? Note that we also do not listen for query responses unless we've asked for a table.
}
2022-01-23 19:51:23 +00:00
}
SystemQuery . Create = async function ( config ) {
let result = new SystemQuery ( config ) ;
await result . init ( ) ;
return result ;
}
export default SystemQuery ;