-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
69 lines (59 loc) · 2.43 KB
/
cli.js
File metadata and controls
69 lines (59 loc) · 2.43 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
/*
Copyright (C) 2023 by Eric Chen
This file is part of p2p_share_compute.
p2p_share_compute is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
p2p_share_compute is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with p2p_share_compute. If not, see <https://www.gnu.org/licenses/>.
*/
const Peer = require('./index.js');
const fs = require('fs');
const config = JSON.parse(fs.readFileSync('config.json'));
const peer = new Peer(config);
function saveConfig() {
const peers = Object.keys(peer.peerCache);
config['peerlist'] = peers;
fs.writeFileSync('config.json', JSON.stringify(config));
process.exit();
}
process.on('exit', saveConfig);
process.on('SIGINT', saveConfig);
process.on('SIGUSR1', saveConfig);
process.on('SIGUSR2', saveConfig);
const readline= require('readline');
const { stdin: input, stdout: output } = require('process');
const rl = readline.createInterface({ input, output });
function ask(msg) {
return new Promise((resolve, reject) => {
rl.question(msg, ans => resolve(ans));
});
}
async function question() {
const answer = await ask('Action (0=exit, 1=run): ');
if (answer === 0) {
return;
} else {
const { target, sessId } = await program();
console.log(target, sessId);
const action = await ask('Action (0=exit, 1=send stdin, 2=disconnect): ');
if (action === 2) {
peer.disconnect(target, false);
} else if (action === 1) {
const data = await ask('data: ');
await peer.sendStdin(target, sessId, data);
} else if (action === 0) {
return;
}
}
await question();
}
question();
async function program() {
const flops = await ask('flops >=: ');
const ram = await ask('ram >=: ');
const file = await ask('file (in programs/): ');
const startingStdin = await ask('starting stdin: ');
const code = (await fs.promises.readFile('programs/' + file)).toString();
const target = await peer.findPeer(ram, flops);
const sessId = await peer.run(target, code, [startingStdin], out => console.log(`PROGRAM OUTPUT: ${out}`), err => console.error(`PROGRAM ERROR: ${err}`));
return { target, sessId };
}