-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
123 lines (118 loc) · 4.53 KB
/
app.js
File metadata and controls
123 lines (118 loc) · 4.53 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
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
require('dotenv').config()
const {startUser} = require('./folder/user.js')
const {Peer} = require('./folder/peer.js')
const readline = require('readline')
const peer = new Peer(startUser())
process.on('SIGINT', code => {
peer.stopAllApps()
console.log('stopped all apps on since it is exiting')
console.log(code)
console.log('interrupted')
process.exit(0)
})
process.on('exit', code => {
peer.stopAllApps()
console.log('stopped all apps again to make sure')
console.log(code)
console.log('exited')
})
let rl = readline.Interface({input: process.stdin, output: process.stdout})
function commandFunc(){
rl.question('Command And Data: ', answer => {
try {
answer = JSON.parse(answer)
switch (answer.command) {
case 'post':
peer.postTorrent(answer.data)
console.log(answer.command + ' done')
break;
case 'get':
peer.getTorrent(answer.data)
console.log(answer.command + ' done')
break;
// case 'getall':
// peer.getAllTorrents(answer.data)
// console.log(answer.command + ' done')
// break;
case 'delete':
peer.deleteTorrent(answer.data)
console.log(answer.command + ' done')
break;
// case 'deleteall':
// // peer.deleteAllTorrents(answer.data)
// peer.deleteAllTorrents()
// console.log(answer.command + ' done')
// break;
case 'start':
peer.runTorrentApp(answer.data)
console.log(answer.command + ' done')
break;
// case 'startall':
// peer.startAllApps(answer.data)
// console.log(answer.command + ' done')
// break;
case 'stop':
peer.stopTorrentApp(answer.data)
console.log(answer.command + ' done')
break;
// case 'stopall':
// peer.stopAllApps()
// console.log(answer.command + ' done')
// break;
// case 'stopall':
// peer.startUpTorrents()
// console.log(answer.command + ' done')
// break;
case 'lookup':
peer.lookUpHash(answer.data)
console.log(answer.command + ' done')
break;
default:
console.log('not a command')
break;
}
} catch (error) {
console.log('--------------------------------------------------------\n')
console.log(error)
console.log('\n--------------------------------------------------------')
}
// try {
// answer = JSON.parse(answer)
// if(answer.command === 'add'){
// peer.postTorrent(answer.data)
// console.log(answer.command + ' done')
// } else if(answer.command === 'get'){
// peer.getTorrent(answer.data)
// console.log(answer.command + ' done')
// } else if(answer.command === 'delete'){
// peer.deleteTorrent(answer.data)
// console.log(answer.command + ' done')
// } else if(answer.command === 'start'){
// peer.runTorrentApp(answer.data)
// console.log(answer.command + ' done')
// } else if(answer.command === 'stop'){
// peer.stopTorrentApp(answer.data)
// console.log(answer.command + ' done')
// } else if(answer.command === 'lookup'){
// peer.lookUpHash(answer.data)
// console.log(answer.command + ' done')
// } else {
// console.log('not a command')
// }
// } catch (error) {
// console.log('--------------------------------------------------------\n')
// console.log(error)
// console.log('\n--------------------------------------------------------')
// }
commandFunc()
})
}
if(Number(process.env.CLI)){
console.log('started cli')
commandFunc()
}
if(Number(process.env.SERVER)){
console.log('started http and ws')
peer.startHTTP()
peer.startWS()
}