-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.js
191 lines (170 loc) · 5.63 KB
/
Network.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
//const Libp2p = require('libp2p')
const pull = require('pull-stream');
const lp = require('pull-length-prefixed')
const ProtocolHandler = require("./ProtocolHandler")
/**
* Standard issue ProtocolMuxer, redirects data based off protocol to proper handler.
* Manages Libp2p.
*/
class Network {
/**
*
* @param {Libp2p} libp2p
*
*/
constructor(libp2p) {
this.libp2p = libp2p;
this.protocolHandlers = {};
this._running = false;
}
get peerInfo() {
return this.libp2p.peerInfo;
}
/**
*
* @param {String} ProtocolName
* @param {ProtocolHandler} handler
*/
registerProtocol(ProtocolName, handler) {
if (this._running) {
this.libp2p.handle(ProtocolName, handler._onConnection);
this.libp2p.on('peer:connect', handler._onPeerConnected);
this.libp2p.on('peer:disconnect', handler._onPeerDisconnected);
this.libp2p.peerBook
.getAllArray()
.filter((peer) => peer.isConnected())
.forEach((peer) => handler._onPeerConnected((peer)));
}
this.protocolHandlers[ProtocolName] = handler;
}
/**
*
* @param {String} ProtocolName
*/
unregisterProtocol(ProtocolName) {
var handler = this.protocolHandlers[ProtocolName];
if (this._running) {
this.libp2p.unhandle(ProtocolName);
this.libp2p.removeListener('peer:connect', handler._onPeerConnected);
this.libp2p.removeListener('peer:disconnect', handler._onPeerDisconnected);
}
delete this.protocolHandlers[ProtocolName];
}
/**
*
* @param {PeerInfo} peer
* @param {Message} msg
* @param {String} protocol
*/
sendMessage(peer, msg, protocol) {
return new Promise((resolve, reject) => {
if (!this._running) { return reject(new Error(`network isn't running`)) }
//const stringId = peer.id.toB58String()
//console.log('sendMessage to %s', stringId, msg)
this._dialPeer(peer, protocol).catch((err) => reject(err))
.then((result) => {
var conn = result[0]
var protocolName = result[1]
let serialized;
switch (protocolName) {
case protocol:
serialized = msg.serialize()
//console.log(protocol)
break
default:
return reject(new Error('Unkown protocol: ' + protocol))
}
//console.log(serialized)
// TODO: why doesn't the error get propageted back??
writeMessage(conn, serialized, (err, val) => {
if (err) {
console.error(err)
}
})
resolve()
})
/*this._dialPeer(peer, (err, conn, protocol) => {
if (err) {
return reject(err)
}
})*/
});
}
/**
*
* @param {PeerInfo} peer
* @returns {Promise}
*/
connectTo(peer) {
return new Promise((resolve, reject) => {
if (!this._running) { return reject(new Error(`network isn't running`)); }
this.libp2p.dial(peer, (err, result) => {
if (err) reject(err);
resolve(result);
});
});
}
/**
* Dial using supplied protocol
* @param {PeerInfo} peer
* @param {String} Protocol
* @returns {Promise}
*/
_dialPeer(peer, Protocol) {
return new Promise((resolve, reject) => {
// Attempt dtube 0.0.1
this.libp2p.dialProtocol(peer, Protocol, (err, conn) => {
if (err) {
reject(err);
return;
}
resolve([conn, Protocol]);
});
});
}
/**
* @returns {Promise}
*/
start() {
return new Promise((resolve, reject) => {
this._running = true;
var protocols = Object.keys(this.protocolHandlers)
for (var protocol in protocols) {
var handler = this.protocolHandlers[protocol];
this.libp2p.handle(ProtocolName, handler._onConnection);
this.libp2p.on('peer:connect', handler._onPeerConnect);
this.libp2p.on('peer:disconnect', handler._onPeerDisconnect);
this.libp2p.peerBook
.getAllArray()
.filter((peer) => peer.isConnected())
.forEach((peer) => handler._onPeerConnect((peer)));
}
resolve();
})
}
/**
* @returns {Promise}
*/
stop() {
return new Promise((resolve, reject) => {
this._running = false;
var protocols = Object.keys(this.protocolHandlers)
for (var protocol in protocols) {
var handler = this.protocolHandlers[protocol];
this.libp2p.unhandle(protocol);
this.libp2p.removeListener('peer:connect', handler._onPeerConnect);
this.libp2p.removeListener('peer:disconnect', handler._onPeerDisconnect);
}
resolve();
});
}
}
function writeMessage(conn, msg, callback) {
pull(
pull.values([msg]),
lp.encode(),
conn,
pull.onEnd(callback)
)
}
exports = module.exports = Network;