-
Notifications
You must be signed in to change notification settings - Fork 3
/
signaller.js
executable file
·150 lines (133 loc) · 4 KB
/
signaller.js
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
#!/usr/bin/env node
const Chord = require('./lib/chord'),
SocketIO = require('socket.io'),
io = require('socket.io-client'),
co = require('co'),
http = require('http'),
path = require('path'),
url = require('url'),
program = require('commander'),
wrtc = require('electron-webrtc')({ headless: true }),
packageJson = require(path.join(__dirname, 'package.json')),
server = http.Server(),
ioServer = SocketIO(server),
peerOptions = { wrtc },
nodeOptions = {
fingerTableSize: 160,
succListSize: 160,
fixFingerCocurrency: 40,
},
chordOptions = {
peerOptions,
nodeOptions,
minHubConnCount: 5,
maxHubConnCount: 160,
minNodeFingerSize: 20,
maxNodeFingerSize: 160,
},
chord = new Chord(chordOptions, true),
peerHosts = { /* host -> time */ },
time = Date.now()
program
.version(packageJson.version)
.option('-p, --port <port>', 'listen port, default 8088', parseFloat, 8088)
.option('-P, --peer <url>', 'peer websocket url', host => peerHosts[host] = { time })
.parse(process.argv)
chord.getKnownNodes = function() {
var nodes = { }
chord.node.fingerIds
.concat(chord.node.succBackupIds)
.concat(chord.node.predBackupIds)
.concat(chord.node.predecessorId)
.forEach(id => nodes[id] = true)
return Object.keys(nodes)
}
ioServer.on('connection', sock => co(function *() {
try {
yield *chord.connectViaSocketIO(sock)
sock.disconnect()
}
catch (err) {
console.error('[ws]', err)
sock.disconnect()
}
}))
server.addListener('request', (req, res) => {
var reqUrl = req.url.match(/^\w:\/\/.*/) ? 'http://' + req.headers.host + req.url : req.url,
parse = url.parse(reqUrl, true)
if (req.method === 'GET' && parse.path === '/status') {
var id = chord.id,
peers = Object.keys(peerHosts),
predecessorId = chord.node.predecessorId,
fingers = chord.node.fingerIds,
conns = Object.keys(chord.hub.conns)
res.end(JSON.stringify({ id, peers, predecessorId, fingers, conns }))
}
})
chord.on('pub-sig-peer', data => {
var time = Date.now(),
peers = peerHosts
peerHosts = { }
Object.keys(peers)
.filter(host => peers[host].time > time - 5 * 60 * 1000)
.forEach(host => peerHosts[host] = peers[host])
var id = data.id,
host = data.host
if (id !== chord.id && host) {
peerHosts[host] = { id, time }
}
})
chord.on('pub-sig-join', data => {
var shouldJoin = data.nodes.length > chord.getKnownNodes().length
if (shouldJoin) {
co(chord.node.join(data.id))
}
chord.send(data.id, 'pub-sig-join-' + chord.id, shouldJoin)
})
co(function *() {
while (1) {
yield new Promise(resolve => setTimeout(resolve, 10000))
chord.subscribe('sig-peer')
var time = Date.now(),
hosts = Object.keys(peerHosts),
host = hosts[Math.floor(Math.random() * hosts.length)],
id = peerHosts[host] && peerHosts[host].id,
sock = null
if (host && chord.hub.has(id)) {
peerHosts[host] = { id, time }
chord.publish('sig-peer', 'pub-sig-peer', { id, host })
}
else if (host) try {
console.log('[sig] connecting to peer ' + host)
sock = io('ws://' + host, { transports: ['websocket'], reconnection: false })
yield new Promise((resolve, reject) => {
sock.on('connect', resolve)
sock.on('connect_error', reject)
setTimeout(reject, 1000, 'websocket connection timeout')
})
id = yield* chord.connectViaSocketIO(sock, true)
if (id && id !== chord.id) {
chord.send(id, 'pub-sig-join', { id: chord.id, nodes: chord.getKnownNodes() })
var hasJoined = yield new Promise((resolve, reject) => {
setTimeout(reject, 10000, 'join to ' + id + ' timeout')
chord.once('pub-sig-join-' + id, resolve)
})
if (!hasJoined) {
yield *chord.node.join(id)
}
peerHosts[host] = { id, time }
yield chord.publish('sig-peer', 'pub-sig-peer', { id, host })
console.log('[sig] joined to ' + id + ' at ' + host)
}
console.log('[sig] ' + host + ' connected')
sock.disconnect()
}
catch (err) {
console.error('[sig]', err)
sock && sock.disconnect()
}
}
})
server.listen(program.port, _ => {
console.log('listening at port ' + server.address().port)
})