|
| 1 | +// A DAS bus node over the generated gRPC stubs (das-proto `DistributedAlgorithmNode`). This is the |
| 2 | +// real wire layer the live transport is built on: a participant hosts an inbound node (a gRPC |
| 3 | +// server) and sends `execute_message` commands to peers. The full pattern-matching choreography |
| 4 | +// (the proxy that issues a `bus_command_proxy` and assembles the streamed `query_answer_tokens_flow`) |
| 5 | +// is ported on top of this from the Python `hyperon_das` service_bus/proxy; this layer is what makes |
| 6 | +// a node reachable and lets it ping/exchange messages. Node-only. |
| 7 | +import { |
| 8 | + Server, |
| 9 | + ServerCredentials, |
| 10 | + credentials, |
| 11 | + type ServerUnaryCall, |
| 12 | + type sendUnaryData, |
| 13 | +} from "@grpc/grpc-js"; |
| 14 | +import { |
| 15 | + DistributedAlgorithmNodeClient, |
| 16 | + DistributedAlgorithmNodeService, |
| 17 | + type MessageData, |
| 18 | +} from "./gen/distributed_algorithm_node"; |
| 19 | +import { type Empty, type Ack } from "./gen/common"; |
| 20 | + |
| 21 | +/** The bus command vocabulary (from the Python/Rust clients' `service_bus`). */ |
| 22 | +export const BusCommand = { |
| 23 | + ping: "ping", |
| 24 | + ack: "ack", |
| 25 | + nodeJoinedNetwork: "node_joined_network", |
| 26 | + busCommandProxy: "bus_command_proxy", |
| 27 | + queryAnswerTokensFlow: "query_answer_tokens_flow", |
| 28 | +} as const; |
| 29 | + |
| 30 | +export type MessageHandler = (msg: MessageData) => void; |
| 31 | + |
| 32 | +/** An inbound bus node: a gRPC server implementing `DistributedAlgorithmNode`, plus the ability to |
| 33 | + * send `execute_message` to peers. */ |
| 34 | +export class BusNode { |
| 35 | + private server?: Server; |
| 36 | + constructor( |
| 37 | + readonly address: string, |
| 38 | + private readonly onMessage: MessageHandler = () => {}, |
| 39 | + ) {} |
| 40 | + |
| 41 | + /** Start the inbound gRPC server. Resolves once it is listening. */ |
| 42 | + start(): Promise<void> { |
| 43 | + const server = new Server(); |
| 44 | + server.addService(DistributedAlgorithmNodeService, { |
| 45 | + ping: (_call: ServerUnaryCall<Empty, Ack>, cb: sendUnaryData<Ack>) => |
| 46 | + cb(null, { error: false, msg: "pong" }), |
| 47 | + executeMessage: (call: ServerUnaryCall<MessageData, Empty>, cb: sendUnaryData<Empty>) => { |
| 48 | + this.onMessage(call.request); |
| 49 | + cb(null, {}); |
| 50 | + }, |
| 51 | + }); |
| 52 | + this.server = server; |
| 53 | + return new Promise((resolve, reject) => { |
| 54 | + server.bindAsync(this.address, ServerCredentials.createInsecure(), (err) => |
| 55 | + err ? reject(err) : resolve(), |
| 56 | + ); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + /** Send a command message to a peer node and await its acknowledgement. */ |
| 61 | + send(peer: string, message: MessageData): Promise<void> { |
| 62 | + const client = new DistributedAlgorithmNodeClient(peer, credentials.createInsecure()); |
| 63 | + return new Promise((resolve, reject) => { |
| 64 | + client.executeMessage(message, (err) => { |
| 65 | + client.close(); |
| 66 | + if (err) reject(err); |
| 67 | + else resolve(); |
| 68 | + }); |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + /** Ping a peer node; resolves with its `Ack`. */ |
| 73 | + ping(peer: string): Promise<Ack> { |
| 74 | + const client = new DistributedAlgorithmNodeClient(peer, credentials.createInsecure()); |
| 75 | + return new Promise((resolve, reject) => { |
| 76 | + client.ping({}, (err, ack) => { |
| 77 | + client.close(); |
| 78 | + if (err) reject(err); |
| 79 | + else resolve(ack); |
| 80 | + }); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + stop(): Promise<void> { |
| 85 | + return new Promise((resolve) => { |
| 86 | + if (!this.server) return resolve(); |
| 87 | + this.server.tryShutdown(() => resolve()); |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
0 commit comments