From 1c5ce879229e3fe25e343e9e71080a88de2301e0 Mon Sep 17 00:00:00 2001 From: bobbydigitales Date: Fri, 30 May 2025 16:17:29 -0700 Subject: [PATCH] Improve status logging --- src/PeerManager.ts | 56 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) diff --git a/src/PeerManager.ts b/src/PeerManager.ts index 0169c9d..e203939 100644 --- a/src/PeerManager.ts +++ b/src/PeerManager.ts @@ -6,6 +6,7 @@ import { generateID } from "IDUtils"; import { log, logID } from "log"; +import { App } from "./App"; // Use a broadcast channel to only have one peer manager for multiple tabs, // then we won't need to have a session ID as all queries for a peerID will be coming from the same peer manager @@ -53,6 +54,36 @@ export class PeerManager { // } // } + animals = ['shrew', 'jerboa', 'lemur', 'weasel', 'possum', 'possum', 'marmoset', 'planigale', 'mole', 'narwhal']; + adjectives = ['snazzy', 'whimsical', 'jazzy', 'bonkers', 'wobbly', 'spiffy', 'chirpy', 'zesty', 'bubbly', 'perky', 'sassy']; + snakes = ['mamba', 'cobra', 'python', 'viper', 'krait', 'sidewinder', 'constrictor', 'boa', 'asp', 'anaconda', 'krait'] + + hashIdToIndices(id: string) { + let indices = []; + for (let char of id) { + if (char !== '0' && char !== '-') { + indices.push(parseInt(char, 16)); + if (indices.length == 2) { + break; + } + } + } + return [indices[0], indices[1]]; + } + + funkyName(id: string, listOne: string[], listTwo: string[]) { + let [one, two] = this.hashIdToIndices(id); + let first = listOne[one % listOne.length]; + let second = listTwo[two % listTwo.length]; + return { first, second } + } + + getPeername(peerID:string) { + let { first: adjective, second: snake } = this.funkyName(peerID, this.adjectives, this.snakes); + let peername = `${adjective}_${snake}` + return peername; + } + websocketSend(message: any) { if (!this.websocket) { throw new Error(); @@ -259,7 +290,21 @@ export class PeerManager { this.watchdogInterval = setInterval(() => { - if (!this.isBootstrapPeer && this.peers.size === 0) { + let numActive = 0; + + for (let [id, peer] of this.peers) { + if (id === this.bootstrapPeerID || + peer.rtcPeer?.connectionState === "new" || + peer.rtcPeer?.connectionState === "connecting" + ) { + continue; + } + + numActive++; + } + + + if (!this.isBootstrapPeer && numActive === 0) { console.log.apply(null, log(`No peers connected, will attempt to reconnect in ${this.reconnectPeriod} seconds...`)); // Websocket reconnect @@ -274,11 +319,14 @@ export class PeerManager { } - let output = `local peerID:${logID(this.peerID)}` + "\n"; + let output = `Current status:` + "\n" + `[${logID(this.peerID)}]${this.getPeername(this.peerID)}[local]` + "\n"; for (let [peerID, peer] of this.peers) { - output += `${logID(peerID)}: ${peer.rtcPeer?.connectionState}` + "\n"; + output += `[${logID(peerID)}]${peer.rtcPeer?.connectionState}:${this.getPeername(peerID)}${(peerID === this.bootstrapPeerID) ? "[Bootstrap]":""}` + "\n"; } + output += `numActivePeers: ${numActive}` + "\n"; + + console.log.apply(null, log(output)); }, this.reconnectPeriod * 1000); } @@ -845,7 +893,7 @@ class PeerConnection { if (type === "initial_peers") { for (let peerID of message.peers) { - console.log(log("Connecting to initial peer ", peerID)); + console.log.apply(null, log("Connecting to initial peer ", peerID)); this.peerManager.connectToPeer(peerID); } }