-
Notifications
You must be signed in to change notification settings - Fork 5
/
belnetRpcCall.ts
152 lines (134 loc) · 3.94 KB
/
belnetRpcCall.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { ipcMain } from 'electron';
import * as _zmq from 'zeromq/';
import * as zmq from 'zeromq/v5-compat';
_zmq.context.blocky = false;
import { eventsByJobId } from './ipcNode';
import { IPC_CHANNEL_KEY } from './sharedIpc';
const RPC_BOUND_PORT = 1190;
const RPC_BOUND_IP = '127.0.0.1';
const RPC_ZMQ_ADDRESS = `tcp://${RPC_BOUND_IP}:${RPC_BOUND_PORT}`;
let dealer: any; // ZeroMqDealer
let isRunning = false;
const request = async (
cmd: string,
reply_tag: string,
args: string
): Promise<void> => {
if (!cmd) {
throw new Error(`Missing cmd`);
}
if (!reply_tag) {
throw new Error(`You must use a reply tag for cmd ${cmd}`);
}
await dealer.send([cmd, reply_tag, args]);
};
// BelnetApiClient::invoke
const invoke = async (
endpoint: string,
reply_tag: string,
args: Record<string, unknown>
) => {
const argsStringified = JSON.stringify(args);
await request(endpoint, reply_tag, argsStringified);
};
export const getSummaryStatus = async (reply_tag: string): Promise<void> => {
await invoke('llarp.get_status', reply_tag, {});
};
export const addExit = async (
reply_tag: string,
exitAddress: string,
exitToken?: string
): Promise<void> => {
if (exitToken) {
await invoke('llarp.exit', reply_tag, {
exit: exitAddress,
token: exitToken
});
} else {
await invoke('llarp.exit', reply_tag, { exit: exitAddress });
}
};
export const deleteExit = async (reply_tag: string): Promise<void> => {
await invoke('llarp.exit', reply_tag, { unmap: true });
};
export const setConfig = async (
reply_tag: string,
section: string,
key: string,
value: string
): Promise<void> => {
const obj: { [k: string]: any } = {};
const config: { [k: string]: string } = {};
config[key] = value;
obj[section] = config;
await invoke('llarp.config', reply_tag, { override: obj, reload: true });
};
export const closeRpcConnection = (): void => {
isRunning = false;
console.info('stopping rpc dealer');
if (dealer) {
try {
dealer.close();
dealer = null;
} catch (e) {
console.info(e);
}
}
};
const loopDealerReceiving = async (): Promise<void> => {
// TODO handle exception in the loop so the loop is not exited if the error is not that bad
try {
dealer.connect(RPC_ZMQ_ADDRESS);
console.log(`Connected to port ${RPC_BOUND_PORT}`);
isRunning = true;
while (isRunning) {
const reply = await dealer.receive();
const replyLength = reply.length;
if (replyLength === 3) {
const replyType = reply[0].toString('utf8');
if (replyType === 'REPLY') {
const jobId = reply[1].toString('utf8');
const content = reply[2].toString('utf8');
const event = eventsByJobId[jobId];
if (!event) {
throw new Error(`Could not find the event for jobId ${jobId}`);
}
event.sender.send(`${IPC_CHANNEL_KEY}-done`, jobId, null, content);
delete eventsByJobId[jobId];
} else {
// delete eventsByJobId[jobId];
//FIXME TODO
throw new Error('To handle');
}
} else {
throw new Error('To handle');
console.warn('Got an invalid reply of length', replyLength);
if (replyLength === 2) {
const jobId = reply[1].toString('utf8');
ipcMain.emit(
`${IPC_CHANNEL_KEY}-done`,
jobId,
'Got an invalid reply of length'
);
}
}
}
} catch (e) {
if (isRunning) {
console.error(
`Got an exception while trying to bind to ${RPC_ZMQ_ADDRESS}:`,
e
);
}
}
};
export const initialBelnetRpcDealer = async (): Promise<void> => {
if (dealer) {
throw new Error('RPC Channel is already init.');
}
dealer = new _zmq.Dealer({ sendTimeout: 10000 });
// just trigger the loop, non blocking
void loopDealerReceiving();
};