2024-12-25 16:13:48 -06:00
|
|
|
import Debug from 'debug';
|
2024-12-31 11:35:09 -06:00
|
|
|
import {CREATOR, HTTP_API_ADDR, HTTP_API_ENABLE, HTTP_API_PORT, PEER_ID, PUB_SUB_TOPIC, PUBLISH_BIND_ADDR, PUBLISH_BIND_HOST, PUBLISH_BIND_PORT, REQUEST_BIND_ADDR, REQUEST_BIND_HOST, REQUEST_BIND_PORT, SEED_PEERS} from './config.js';
|
|
|
|
import {DeltaStream} from './deltas.js';
|
|
|
|
import {HttpServer} from './http/index.js';
|
|
|
|
import {Lossless} from './lossless.js';
|
|
|
|
import {Peers} from './peers.js';
|
|
|
|
import {PubSub} from './pub-sub.js';
|
|
|
|
import {RequestReply} from './request-reply.js';
|
|
|
|
import {PeerAddress} from './types.js';
|
2024-12-25 16:13:48 -06:00
|
|
|
const debug = Debug('rhizome-node');
|
|
|
|
|
|
|
|
export type RhizomeNodeConfig = {
|
|
|
|
requestBindAddr: string;
|
|
|
|
requestBindHost: string;
|
|
|
|
requestBindPort: number;
|
|
|
|
publishBindAddr: string;
|
|
|
|
publishBindHost: string;
|
|
|
|
publishBindPort: number;
|
|
|
|
httpAddr: string;
|
|
|
|
httpPort: number;
|
|
|
|
httpEnable: boolean;
|
|
|
|
seedPeers: PeerAddress[];
|
|
|
|
peerId: string;
|
|
|
|
creator: string; // TODO each host should be able to support multiple users
|
2024-12-31 11:35:09 -06:00
|
|
|
pubSubTopic: string;
|
2024-12-25 16:13:48 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
// So that we can run more than one instance in the same process (for testing)
|
|
|
|
export class RhizomeNode {
|
|
|
|
config: RhizomeNodeConfig;
|
|
|
|
pubSub: PubSub;
|
|
|
|
requestReply: RequestReply;
|
2024-12-27 13:43:43 -06:00
|
|
|
httpServer: HttpServer;
|
2024-12-25 16:13:48 -06:00
|
|
|
deltaStream: DeltaStream;
|
2024-12-30 01:23:11 -06:00
|
|
|
lossless: Lossless;
|
2024-12-25 16:13:48 -06:00
|
|
|
peers: Peers;
|
|
|
|
myRequestAddr: PeerAddress;
|
|
|
|
myPublishAddr: PeerAddress;
|
|
|
|
|
|
|
|
constructor(config?: Partial<RhizomeNodeConfig>) {
|
|
|
|
this.config = {
|
|
|
|
requestBindAddr: REQUEST_BIND_ADDR,
|
|
|
|
requestBindHost: REQUEST_BIND_HOST,
|
|
|
|
requestBindPort: REQUEST_BIND_PORT,
|
|
|
|
publishBindAddr: PUBLISH_BIND_ADDR,
|
|
|
|
publishBindHost: PUBLISH_BIND_HOST,
|
|
|
|
publishBindPort: PUBLISH_BIND_PORT,
|
|
|
|
httpAddr: HTTP_API_ADDR,
|
|
|
|
httpPort: HTTP_API_PORT,
|
|
|
|
httpEnable: HTTP_API_ENABLE,
|
|
|
|
seedPeers: SEED_PEERS,
|
|
|
|
peerId: PEER_ID,
|
|
|
|
creator: CREATOR,
|
2024-12-31 11:35:09 -06:00
|
|
|
pubSubTopic: PUB_SUB_TOPIC,
|
2024-12-25 16:13:48 -06:00
|
|
|
...config
|
|
|
|
};
|
|
|
|
debug('config', this.config);
|
2024-12-25 16:15:38 -06:00
|
|
|
this.myRequestAddr = new PeerAddress(
|
|
|
|
this.config.requestBindHost,
|
|
|
|
this.config.requestBindPort
|
|
|
|
);
|
|
|
|
this.myPublishAddr = new PeerAddress(
|
|
|
|
this.config.publishBindHost,
|
|
|
|
this.config.publishBindPort
|
|
|
|
);
|
2024-12-25 16:13:48 -06:00
|
|
|
this.pubSub = new PubSub(this);
|
|
|
|
this.requestReply = new RequestReply(this);
|
2024-12-27 13:43:43 -06:00
|
|
|
this.httpServer = new HttpServer(this);
|
2024-12-25 16:13:48 -06:00
|
|
|
this.deltaStream = new DeltaStream(this);
|
|
|
|
this.peers = new Peers(this);
|
2024-12-30 01:23:11 -06:00
|
|
|
this.lossless = new Lossless();
|
2024-12-25 16:13:48 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async start() {
|
2024-12-30 01:23:11 -06:00
|
|
|
// Connect our lossless view to the delta stream
|
|
|
|
this.deltaStream.subscribeDeltas((delta) => this.lossless.ingestDelta(delta));
|
|
|
|
|
2024-12-26 15:59:03 -06:00
|
|
|
// Start ZeroMQ publish and reply sockets
|
2024-12-31 11:35:09 -06:00
|
|
|
await this.pubSub.start();
|
2024-12-25 16:13:48 -06:00
|
|
|
this.requestReply.start();
|
2024-12-26 15:59:03 -06:00
|
|
|
|
|
|
|
// Start HTTP server
|
2024-12-25 16:13:48 -06:00
|
|
|
if (this.config.httpEnable) {
|
2024-12-27 13:43:43 -06:00
|
|
|
this.httpServer.start();
|
2024-12-25 16:13:48 -06:00
|
|
|
}
|
2024-12-26 15:59:03 -06:00
|
|
|
|
2024-12-31 11:35:09 -06:00
|
|
|
// Start libp2p subscription
|
|
|
|
this.peers.start();
|
|
|
|
|
2024-12-26 15:59:03 -06:00
|
|
|
// Wait a short time for sockets to initialize
|
2024-12-25 16:13:48 -06:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
2024-12-26 15:59:03 -06:00
|
|
|
|
|
|
|
// Subscribe to seed peers
|
2024-12-25 16:13:48 -06:00
|
|
|
this.peers.subscribeToSeeds();
|
2024-12-26 15:59:03 -06:00
|
|
|
|
|
|
|
// Wait a short time for sockets to initialize
|
2024-12-25 16:13:48 -06:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 500));
|
2024-12-26 15:59:03 -06:00
|
|
|
|
|
|
|
// Ask all peers for all deltas
|
2024-12-25 16:13:48 -06:00
|
|
|
this.peers.askAllPeersForDeltas();
|
2024-12-26 15:59:03 -06:00
|
|
|
|
|
|
|
// Wait to receive all deltas
|
2024-12-25 16:13:48 -06:00
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
}
|
|
|
|
|
|
|
|
async stop() {
|
|
|
|
await this.pubSub.stop();
|
|
|
|
await this.requestReply.stop();
|
2024-12-27 13:43:43 -06:00
|
|
|
await this.httpServer.stop();
|
2024-12-25 16:13:48 -06:00
|
|
|
}
|
|
|
|
}
|