PeerServer, Peer: Fully support exchanging friendly names

....we should have done this a while back :P

This doesn't change logging messages though
This commit is contained in:
Starbeamrainbowlabs 2022-02-26 00:20:58 +00:00
parent fd16ca4078
commit 7e5b0725e9
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
4 changed files with 21 additions and 3 deletions

View file

@ -6,7 +6,6 @@ import path from 'path';
import { EventEmitter } from 'events'; import { EventEmitter } from 'events';
import log from '../io/NamespacedLog.mjs'; const l = log("agent"); import log from '../io/NamespacedLog.mjs'; const l = log("agent");
import settings from '../../settings.mjs'; import settings from '../../settings.mjs';
import PeerServer from './PeerServer.mjs'; import PeerServer from './PeerServer.mjs';
@ -61,6 +60,7 @@ class Agent extends EventEmitter {
/// ///
this.server = new PeerServer( this.server = new PeerServer(
this.peer_id, this.peer_id,
this.peer_name,
await this.find_secret() await this.find_secret()
); );
this.server.retries = this.config.net.peer_retries; this.server.retries = this.config.net.peer_retries;

View file

@ -22,6 +22,7 @@ class Peer extends EventEmitter {
address: this.address, address: this.address,
port: this.port, port: this.port,
id: this.id, id: this.id,
name: this.name,
listening_address: this.listening_address, listening_address: this.listening_address,
listening_port: this.listening_port listening_port: this.listening_port
}; };
@ -30,7 +31,18 @@ class Peer extends EventEmitter {
constructor(server, connection) { constructor(server, connection) {
super(); super();
/**
* The ID of this peer.
* @type {string}
*/
this.id = null; 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. * The parent server this Peer is part of.
@ -91,6 +103,7 @@ class Peer extends EventEmitter {
*/ */
__handle_hello(msg) { __handle_hello(msg) {
this.id = msg.id; this.id = msg.id;
this.name = msg.name;
this.listening_address = msg.listening_address; this.listening_address = msg.listening_address;
this.listening_port = msg.listening_port; this.listening_port = msg.listening_port;
@ -115,6 +128,7 @@ class Peer extends EventEmitter {
async __send_hello() { async __send_hello() {
await this.send("hello", { await this.send("hello", {
id: this.server.our_id, id: this.server.our_id,
name: this.server.our_name,
listening_address: this.server.host, listening_address: this.server.host,
listening_port: this.server.port listening_port: this.server.port
}); });

View file

@ -25,10 +25,11 @@ class PeerServer extends EventEmitter {
return this.server instanceof net.Server ? this.server.listening : false; return this.server instanceof net.Server ? this.server.listening : false;
} }
constructor(our_id, secret_join) { constructor(our_id, our_name, secret_join) {
super(); super();
this.our_id = our_id; this.our_id = our_id;
this.our_name = our_name;
this.secret_join = secret_join; this.secret_join = secret_join;
// The number of retries when attempting to connect to a peer // The number of retries when attempting to connect to a peer

View file

@ -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, "")); let table_name = sys.fetch_table(ctx.params.table_name.replace(/[^0-9a-zA-Z_-]/g, ""));
for await (let next of table_name) { for await (let next of table_name) {
stream.write_json(`table`, { stream.write_json(`table`, {
peer: next.peer.id, peer: {
id: next.peer.id,
name: next.peer.name
},
table: next.table table: next.table
}); });
} }