*Update DHT management

This commit is contained in:
Derrick Hammer 2022-08-31 15:35:24 -04:00
parent 4956592d1d
commit b25706e8ac
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 54 additions and 74 deletions

View File

@ -1,12 +0,0 @@
function idFactory(start = 1, step = 1, limit = 2 ** 32) {
let id = start;
return function nextId() {
const nextId = id;
id += step;
if (id >= limit) id = start;
return nextId;
};
}
export const nextId = idFactory(1);

View File

@ -3,7 +3,6 @@ import DHT from "@lumeweb/dht-web";
import type { ActiveQuery } from "libkmodule"; import type { ActiveQuery } from "libkmodule";
import { addHandler, getSeed, handleMessage } from "libkmodule"; import { addHandler, getSeed, handleMessage } from "libkmodule";
import { handlePresentSeed as handlePresentSeedModule } from "libkmodule/dist/seed.js"; import { handlePresentSeed as handlePresentSeedModule } from "libkmodule/dist/seed.js";
import { nextId } from "./id";
import type { Buffer } from "buffer"; import type { Buffer } from "buffer";
import { hexToBuf } from "libskynet"; import { hexToBuf } from "libskynet";
@ -13,11 +12,28 @@ interface DhtConnection {
} }
const connections = new Map<number, DhtConnection>(); const connections = new Map<number, DhtConnection>();
const dhtInstances = new Map()<number, DHT>; const dhtInstances = new Map<number, DHT>();
let defaultDht; let defaultDht: DHT;
let moduleReadyResolve: Function;
let moduleReady: Promise<void> = new Promise((resolve) => {
moduleReadyResolve = resolve;
});
onmessage = handleMessage; onmessage = handleMessage;
function idFactory(start = 1, step = 1, limit = 2 ** 32) {
let id = start;
return function nextId() {
const nextId = id;
id += step;
if (id >= limit) id = start;
return nextId;
};
}
const nextId = idFactory(1);
addHandler("presentSeed", handlePresentSeed); addHandler("presentSeed", handlePresentSeed);
addHandler("openDht", handleOpenDht); addHandler("openDht", handleOpenDht);
@ -38,10 +54,11 @@ addHandler("ready", handleReady);
async function handlePresentSeed(aq: ActiveQuery) { async function handlePresentSeed(aq: ActiveQuery) {
const keyPair = aq.callerInput.myskyRootKeypair; const keyPair = aq.callerInput.myskyRootKeypair;
handlePresentSeedModule({ callerInput: { seed: keyPair } }); handlePresentSeedModule({ callerInput: { seed: keyPair } } as ActiveQuery);
if (!defaultDht) { if (!defaultDht) {
defaultDht = await createDht(); defaultDht = dhtInstances.get(await createDht()) as DHT;
} }
moduleReadyResolve();
} }
async function handleOpenDht(aq: ActiveQuery) { async function handleOpenDht(aq: ActiveQuery) {
@ -84,11 +101,7 @@ async function handleConnect(aq: ActiveQuery) {
let socket: any; let socket: any;
const dht = validateDht(aq); const dht = await getDht(aq);
if (!dht) {
return;
}
try { try {
// @ts-ignore // @ts-ignore
@ -104,7 +117,12 @@ async function handleConnect(aq: ActiveQuery) {
const id = nextId(); const id = nextId();
socket.on("open", () => { socket.on("open", () => {
setDhtConnection(id, dht as number, socket); let dhtId: any = [...dhtInstances.entries()].filter(
(item) => item[1] === dht
);
dhtId = dhtId.shift()[0];
setDhtConnection(id, dhtId as number, socket);
aq.respond({ id }); aq.respond({ id });
}); });
@ -196,98 +214,72 @@ function validateConnection(aq: ActiveQuery): any | boolean {
return false; return false;
} }
return getDhtConnection(id).conn; return getDhtConnection(id)?.conn;
} }
function validateDht(aq: ActiveQuery): DHT | boolean { async function getDht(aq: ActiveQuery): Promise<DHT> {
let { dht = null } = aq.callerInput; await moduleReady;
let dht;
if ("callerInput" in aq && aq.callerInput) {
dht = aq.callerInput.dht ?? null;
if (dht && !dhtInstances.has(dht)) { if (dht && !dhtInstances.has(dht)) {
aq.reject("Invalid DHT id"); const error = "Invalid DHT id";
return false; aq.reject(error);
throw new Error(error);
}
} }
if (!dht) { if (!dht) {
dht = defaultDht; return defaultDht;
} }
return dhtInstances.get(dht); return dhtInstances.get(dht) as DHT;
} }
async function handleAddRelay(aq: ActiveQuery) { async function handleAddRelay(aq: ActiveQuery) {
const { pubkey = null } = aq.callerInput; const { pubkey = null } = aq.callerInput;
const dht = validateDht(aq);
if (!dht) {
return;
}
if (!pubkey) { if (!pubkey) {
aq.reject("invalid pubkey"); aq.reject("invalid pubkey");
return; return;
} }
const dht = await getDht(aq);
aq.respond(await dht.addRelay(pubkey)); aq.respond(await dht.addRelay(pubkey));
} }
function handleRemoveRelay(aq: ActiveQuery) { async function handleRemoveRelay(aq: ActiveQuery) {
const { pubkey = null } = aq.callerInput; const { pubkey = null } = aq.callerInput;
const dht = validateDht(aq);
if (!dht) {
return;
}
if (!pubkey) { if (!pubkey) {
aq.reject("invalid pubkey"); aq.reject("invalid pubkey");
return; return;
} }
const dht = await getDht(aq);
aq.respond(dht.removeRelay(pubkey)); aq.respond(dht.removeRelay(pubkey));
} }
function handleClearRelays(aq: ActiveQuery) { async function handleClearRelays(aq: ActiveQuery) {
const dht = validateDht(aq); const dht = await getDht(aq);
if (!dht) {
return;
}
dht.clearRelays(); dht.clearRelays();
aq.respond(); aq.respond();
} }
function handleGetRelays(aq: ActiveQuery) { async function handleGetRelays(aq: ActiveQuery) {
const dht = validateDht(aq); aq.respond(await (await getDht(aq)).relays);
if (!dht) {
return;
}
aq.respond(dht.relays);
} }
function handleGetRelayServers(aq: ActiveQuery) { async function handleGetRelayServers(aq: ActiveQuery) {
const dht = validateDht(aq); aq.respond(await (await getDht(aq)).relayServers);
if (!dht) {
return;
}
aq.respond(dht.relayServers);
} }
async function handleReady(aq: ActiveQuery) { async function handleReady(aq: ActiveQuery) {
const dht = validateDht(aq); await (await getDht(aq)).ready();
if (!dht) {
return;
}
// @ts-ignore
await dht.ready();
aq.respond(); aq.respond();
} }