This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathtransport.js
207 lines (177 loc) · 5.32 KB
/
transport.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
'use strict'
const parallel = require('async/parallel')
const once = require('once')
const debug = require('debug')
const log = debug('libp2p:switch:transport')
const LimitDialer = require('./limit-dialer')
// number of concurrent outbound dials to make per peer, same as go-libp2p-swtch
const defaultPerPeerRateLimit = 8
// the amount of time a single dial has to succeed
// TODO this should be exposed as a option
const dialTimeout = 30 * 1000
/**
* Manages the transports for the switch. This simplifies dialing and listening across
* multiple transports.
*/
class TransportManager {
constructor (_switch) {
this.switch = _switch
this.dialer = new LimitDialer(defaultPerPeerRateLimit, dialTimeout)
}
/**
* Adds a `Transport` to the list of transports on the switch, and assigns it to the given key
*
* @param {String} key
* @param {Transport} transport
* @returns {void}
*/
add (key, transport) {
log('adding %s', key)
if (this.switch.transports[key]) {
throw new Error('There is already a transport with this key')
}
this.switch.transports[key] = transport
if (!this.switch.transports[key].listeners) {
this.switch.transports[key].listeners = []
}
}
/**
* Closes connections for the given transport key
* and removes it from the switch.
*
* @param {String} key
* @param {function(Error)} callback
* @returns {void}
*/
remove (key, callback) {
callback = callback || function () {}
if (!this.switch.transports[key]) {
return callback()
}
this.close(key, (err) => {
delete this.switch.transports[key]
callback(err)
})
}
/**
* Calls `remove` on each transport the switch has
*
* @param {function(Error)} callback
* @returns {void}
*/
removeAll (callback) {
const tasks = Object.keys(this.switch.transports).map((key) => {
return (cb) => {
this.remove(key, cb)
}
})
parallel(tasks, callback)
}
/**
* For a given transport `key`, dial to all that transport multiaddrs
*
* @param {String} key Key of the `Transport` to dial
* @param {PeerInfo} peerInfo
* @param {function(Error, Connection)} callback
* @returns {void}
*/
dial (key, peerInfo, callback) {
const transport = this.switch.transports[key]
let multiaddrs = peerInfo.multiaddrs.toArray()
if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
}
// filter the multiaddrs that are actually valid for this transport
multiaddrs = TransportManager.dialables(transport, multiaddrs)
log('dialing %s', key, multiaddrs.map((m) => m.toString()))
// dial each of the multiaddrs with the given transport
this.dialer.dialMany(peerInfo.id, transport, multiaddrs, (err, success) => {
if (err) {
return callback(err)
}
peerInfo.connect(success.multiaddr)
this.switch._peerBook.put(peerInfo)
callback(null, success.conn)
})
}
/**
* For a given Transport `key`, listen on all multiaddrs in the switch's `_peerInfo`.
* If a `handler` is not provided, the Switch's `protocolMuxer` will be used.
*
* @param {String} key
* @param {*} _options Currently ignored
* @param {function(Connection)} handler
* @param {function(Error)} callback
* @returns {void}
*/
listen (key, _options, handler, callback) {
handler = this.switch._connectionHandler(key, handler)
const transport = this.switch.transports[key]
const multiaddrs = TransportManager.dialables(
transport,
this.switch._peerInfo.multiaddrs.distinct()
)
if (!transport.listeners) {
transport.listeners = []
}
let freshMultiaddrs = []
const createListeners = multiaddrs.map((ma) => {
return (cb) => {
const done = once(cb)
const listener = transport.createListener(handler)
listener.once('error', done)
listener.listen(ma, (err) => {
if (err) {
return done(err)
}
listener.removeListener('error', done)
listener.getAddrs((err, addrs) => {
if (err) {
return done(err)
}
freshMultiaddrs = freshMultiaddrs.concat(addrs)
transport.listeners.push(listener)
done()
})
})
}
})
parallel(createListeners, (err) => {
if (err) {
return callback(err)
}
// cause we can listen on port 0 or 0.0.0.0
this.switch._peerInfo.multiaddrs.replace(multiaddrs, freshMultiaddrs)
callback()
})
}
/**
* Closes the transport with the given key, by closing all of its listeners
*
* @param {String} key
* @param {function(Error)} callback
* @returns {void}
*/
close (key, callback) {
const transport = this.switch.transports[key]
if (!transport) {
return callback(new Error(`Trying to close non existing transport: ${key}`))
}
parallel(transport.listeners.map((listener) => {
return (cb) => {
listener.close(cb)
}
}), callback)
}
/**
* For a given transport, return its multiaddrs that match the given multiaddrs
*
* @param {Transport} transport
* @param {Array<Multiaddr>} multiaddrs
* @returns {Array<Multiaddr>}
*/
static dialables (transport, multiaddrs) {
return transport.filter(multiaddrs)
}
}
module.exports = TransportManager