From 7e5b0725e90ba0ea5e69d57ed12af79596f8cd04 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 26 Feb 2022 00:20:58 +0000 Subject: [PATCH] PeerServer, Peer: Fully support exchanging friendly names ....we should have done this a while back :P This doesn't change logging messages though --- src/lib/agent/Agent.mjs | 2 +- src/lib/agent/Peer.mjs | 14 ++++++++++++++ src/lib/agent/PeerServer.mjs | 3 ++- src/lib/agent/subsystems/http/routes/table.mjs | 5 ++++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/lib/agent/Agent.mjs b/src/lib/agent/Agent.mjs index 7577a5e..56d9ceb 100644 --- a/src/lib/agent/Agent.mjs +++ b/src/lib/agent/Agent.mjs @@ -6,7 +6,6 @@ import path from 'path'; import { EventEmitter } from 'events'; import log from '../io/NamespacedLog.mjs'; const l = log("agent"); - import settings from '../../settings.mjs'; import PeerServer from './PeerServer.mjs'; @@ -61,6 +60,7 @@ class Agent extends EventEmitter { /// this.server = new PeerServer( this.peer_id, + this.peer_name, await this.find_secret() ); this.server.retries = this.config.net.peer_retries; diff --git a/src/lib/agent/Peer.mjs b/src/lib/agent/Peer.mjs index 18d4b96..a96a6aa 100644 --- a/src/lib/agent/Peer.mjs +++ b/src/lib/agent/Peer.mjs @@ -22,6 +22,7 @@ class Peer extends EventEmitter { address: this.address, port: this.port, id: this.id, + name: this.name, listening_address: this.listening_address, listening_port: this.listening_port }; @@ -30,7 +31,18 @@ class Peer extends EventEmitter { constructor(server, connection) { super(); + /** + * The ID of this peer. + * @type {string} + */ this.id = null; + /** + * The friendly name of this peer. + * Unlike the ID, this *may* not be unique (though it is strongly + * encouraged to be unique). + * @type {string} + */ + this.name = null; /** * The parent server this Peer is part of. @@ -91,6 +103,7 @@ class Peer extends EventEmitter { */ __handle_hello(msg) { this.id = msg.id; + this.name = msg.name; this.listening_address = msg.listening_address; this.listening_port = msg.listening_port; @@ -115,6 +128,7 @@ class Peer extends EventEmitter { async __send_hello() { await this.send("hello", { id: this.server.our_id, + name: this.server.our_name, listening_address: this.server.host, listening_port: this.server.port }); diff --git a/src/lib/agent/PeerServer.mjs b/src/lib/agent/PeerServer.mjs index f786ae2..cfac39a 100644 --- a/src/lib/agent/PeerServer.mjs +++ b/src/lib/agent/PeerServer.mjs @@ -25,10 +25,11 @@ class PeerServer extends EventEmitter { return this.server instanceof net.Server ? this.server.listening : false; } - constructor(our_id, secret_join) { + constructor(our_id, our_name, secret_join) { super(); this.our_id = our_id; + this.our_name = our_name; this.secret_join = secret_join; // The number of retries when attempting to connect to a peer diff --git a/src/lib/agent/subsystems/http/routes/table.mjs b/src/lib/agent/subsystems/http/routes/table.mjs index 41a2428..076b1e0 100644 --- a/src/lib/agent/subsystems/http/routes/table.mjs +++ b/src/lib/agent/subsystems/http/routes/table.mjs @@ -8,7 +8,10 @@ export default async function(sys, ctx, _next) { let table_name = sys.fetch_table(ctx.params.table_name.replace(/[^0-9a-zA-Z_-]/g, "")); for await (let next of table_name) { stream.write_json(`table`, { - peer: next.peer.id, + peer: { + id: next.peer.id, + name: next.peer.name + }, table: next.table }); }