This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
peer-pool.js
113 lines (97 loc) · 3.06 KB
/
peer-pool.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
const {Emitter} = require('event-kit')
const PeerConnection = require('./peer-connection')
const PubSubSignalingProvider = require('./pub-sub-signaling-provider')
module.exports =
class PeerPool {
constructor ({peerId, restGateway, pubSubGateway, fragmentSize, testEpoch}) {
this.peerId = peerId
this.restGateway = restGateway
this.pubSubGateway = pubSubGateway
this.fragmentSize = fragmentSize
this.testEpoch = testEpoch
this.emitter = new Emitter()
this.peerConnectionsById = new Map()
}
initialize () {
return Promise.all([
this.fetchICEServers(),
this.waitForIncomingSignals()
])
}
async fetchICEServers () {
this.iceServers = await this.restGateway.get(`/ice-servers`)
}
waitForIncomingSignals () {
return this.pubSubGateway.subscribe(`/peers/${this.peerId}`, 'signal', this.didReceiveSignal.bind(this))
}
connectTo (peerId) {
return this.getPeerConnection(peerId).connect()
}
getConnectedPromise (peerId) {
return this.getPeerConnection(peerId).getConnectedPromise()
}
getDisconnectedPromise (peerId) {
if (this.peerConnectionsById.has(peerId)) {
return this.peerConnectionsById.get(peerId).getDisconnectedPromise()
} else {
return Promise.resolve()
}
}
disconnect () {
this.disconnected = true
this.peerConnectionsById.forEach((peerConnection) => {
peerConnection.disconnect()
})
this.peerConnectionsById.clear()
}
send (peerId, message) {
const peerConnection = this.peerConnectionsById.get(peerId)
if (peerConnection) {
peerConnection.send(message)
} else {
throw new Error('No connection to peer')
}
}
onDisconnection (callback) {
return this.emitter.on('disconnection', callback)
}
onReceive (callback) {
return this.emitter.on('receive', callback)
}
isConnectedToPeer (peerId) {
const peerConnection = this.peerConnectionsById.get(peerId)
return peerConnection ? peerConnection.connected : false
}
didReceiveSignal (message) {
const peerConnection = this.getPeerConnection(message.senderId)
peerConnection.signalingProvider.receiveMessage(message)
}
didDisconnect (peerId) {
this.peerConnectionsById.delete(peerId)
this.emitter.emit('disconnection', {peerId})
}
didReceiveMessage (event) {
this.emitter.emit('receive', event)
}
getPeerConnection (peerId) {
let peerConnection = this.peerConnectionsById.get(peerId)
if (!peerConnection) {
peerConnection = new PeerConnection({
localPeerId: this.peerId,
remotePeerId: peerId,
fragmentSize: this.fragmentSize,
iceServers: this.iceServers,
didReceiveMessage: this.didReceiveMessage.bind(this),
didDisconnect: this.didDisconnect.bind(this),
signalingProvider: new PubSubSignalingProvider({
localPeerId: this.peerId,
remotePeerId: peerId,
restGateway: this.restGateway,
testEpoch: this.testEpoch
})
})
this.peerConnectionsById.set(peerId, peerConnection)
}
return peerConnection
}
}