-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.ts
More file actions
80 lines (68 loc) · 2.04 KB
/
cli.ts
File metadata and controls
80 lines (68 loc) · 2.04 KB
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
import { parse, serve, respond } from "./deps.ts";
import Address from "./src/interfaces/address.ts";
import Node from "./src/model/node.ts";
import "./src/utils/console.ts";
// Read configuration from program's arguments.
const args = parse(Deno.args);
if ((args.ip && !args.port) || (!args.ip && args.port)) {
console.error("-ip and -port must be both either set or unset");
Deno.exit(1);
}
if ((args.ipTo && !args.portTo) || (!args.ipTo && args.portTo)) {
console.error("-ipTo and -portTo must be both either set or unset");
Deno.exit(1);
}
const config = {
ip: args.ip,
port: parseInt(args.port),
otherIp: args.ipTo,
otherPort: parseInt(args.portTo),
};
// Initialize the node.
const node = await Node.getInstance(config.ip ? config : undefined);
// Run the node's server to listen for JSON remote procedure calls.
const server = serve({
hostname: node.address.hostname,
port: node.address.port,
});
console.info(
`Node accessible through address ${node.address.hostname}:${node.address.port}`
);
const rpcMethods = {
join: (addr: Address) => {
return node.receiver.join(addr);
},
changNNext: (addr: Address) => {
node.receiver.changNNext(addr);
return null;
},
changPrev: (addr: Address) => {
return node.receiver.changPrev(addr);
},
nodeMissing: (addr: Address) => {
node.receiver.nodeMissing(addr);
return null;
},
election: (arg: { id: string }) => {
node.receiver.election(arg);
return null;
},
elected: (arg: { id: string; leaderAddr: Address }) => {
node.receiver.elected(arg);
return null;
},
readVariable: () => {
return node.receiver.readVariable();
},
writeVariable: (arg: { value: any; isBackup: boolean }) => {
node.receiver.writeVariable(arg);
return null;
},
ping: () => {
return node.receiver.ping();
},
};
node.connect();
for await (const req of server) {
respond(req, rpcMethods); // no await, we don't want to block here
}