| 29 | } |
| 30 | |
| 31 | class WshRouter { |
| 32 | routeMap: Map<string, AbstractWshClient>; // routeid -> client |
| 33 | upstreamClient: AbstractWshClient; |
| 34 | rpcMap: Map<string, RouteInfo>; // rpcid -> routeinfo |
| 35 | |
| 36 | constructor(upstreamClient: AbstractWshClient) { |
| 37 | this.routeMap = new Map(); |
| 38 | this.rpcMap = new Map(); |
| 39 | if (upstreamClient == null) { |
| 40 | throw new Error("upstream client cannot be null"); |
| 41 | } |
| 42 | this.upstreamClient = upstreamClient; |
| 43 | } |
| 44 | |
| 45 | reannounceRoutes() { |
| 46 | for (const [routeId, client] of this.routeMap) { |
| 47 | const announceMsg: RpcMessage = { |
| 48 | command: "routeannounce", |
| 49 | data: routeId, |
| 50 | source: routeId, |
| 51 | route: ControlRouteName, |
| 52 | }; |
| 53 | this.upstreamClient.recvRpcMessage(announceMsg); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // returns true if the message was sent |
| 58 | _sendRoutedMessage(msg: RpcMessage, destRouteId: string) { |
| 59 | const client = this.routeMap.get(destRouteId); |
| 60 | if (client) { |
| 61 | client.recvRpcMessage(msg); |
| 62 | return; |
| 63 | } |
| 64 | // there should always an upstream client |
| 65 | if (!this.upstreamClient) { |
| 66 | throw new Error(`no upstream client for message: ${msg}`); |
| 67 | } |
| 68 | this.upstreamClient?.recvRpcMessage(msg); |
| 69 | } |
| 70 | |
| 71 | _registerRouteInfo(reqid: string, sourceRouteId: string, destRouteId: string) { |
| 72 | dlog("registering route info", reqid, sourceRouteId, destRouteId); |
| 73 | if (util.isBlank(reqid)) { |
| 74 | return; |
| 75 | } |
| 76 | const routeInfo: RouteInfo = { |
| 77 | rpcId: reqid, |
| 78 | sourceRouteId: sourceRouteId, |
| 79 | destRouteId: destRouteId, |
| 80 | }; |
| 81 | this.rpcMap.set(reqid, routeInfo); |
| 82 | } |
| 83 | |
| 84 | recvRpcMessage(msg: RpcMessage) { |
| 85 | dlog("router received message", msg); |
| 86 | // we are a terminal node by definition, so we don't need to process with announce/unannounce messages |
| 87 | if (msg.command == "routeannounce" || msg.command == "routeunannounce") { |
| 88 | return; |
nothing calls this directly
no outgoing calls
no test coverage detected