-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebFed.js
379 lines (315 loc) · 15.7 KB
/
webFed.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// import gunDB from './gunWrapper.js'
import p2p from './peerJsWrapper.js'
const configuration = { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }] }
const GUN_TLN_KEY = "federatedTest"
const GUN_KEYS_SPEC = {
config: 'config',
users: 'users',
fedDetails: 'federationDetails',
modelUpdates: 'modelUpdates'
}
const webFed = {}
webFed.currentFederation = {
id: '',
clients: {}
}
webFed.allFederationIds = []
webFed.isFederation = (federationId=webFed.currentFederation.id) => webFed.allFederationIds.includes(federationId)
webFed.getClientInfo = async (federationId=webFed.currentFederation.id, clientId) => {
if (webFed.isFederation(federationId)) {
return await gunDB.getObject([GUN_TLN_KEY, federationId, clientId])
}
}
webFed.isClientInFederation = async (federationId=webFed.currentFederation.id, clientId) => {
return !!(await webFed.getClientInfo(federationId, clientId))
}
webFed.updateAllClientsList = async (federationId=webFed.currentFederation.id, selfClientId) => {
if (webFed.isFederation(federationId)) {
const allClients = await gunDB.getObject([GUN_TLN_KEY,federationId, GUN_KEYS_SPEC['users']])
if (allClients) {
Object.keys(allClients).filter(clientId => !webFed.currentFederation?.clients?.[clientId]).forEach(clientId => {
webFed.currentFederation.clients[clientId] = {
'id': clientId,
'connectionState': clientId !== selfClientId ? "disconnected" : ""
}
})
}
}
}
webFed.getAllPeers = (federationId=webFed.currentFederation.id, selfClientId) => Object.keys(webFed.currentFederation.clients).reduce((peers, clientId) => {
if (clientId !== selfClientId) {
peers[clientId] = webFed.currentFederation.clients[clientId]
}
return peers
}, {})
webFed.getConnectedPeers = async(federationId=webFed.currentFederation.id, selfClientId) => {
const peers = await webFed.getAllPeers(federationId, selfClientId)
const connectedPeers = Object.keys(peers).reduce((newPeerIds, peerId) => {
if (peers[peerId].connectionState === "connected") {
newPeerIds.push(peerId)
}
return newPeerIds
}, [])
return connectedPeers
}
webFed.getDisconnectedPeers = async (federationId=webFed.currentFederation.id, selfClientId) => {
const peers = await webFed.getAllPeers(federationId, selfClientId)
const disconnectedPeers = Object.keys(peers).reduce((newPeerIds, peerId) => {
if (peers[peerId].connectionState === "disconnected") {
newPeerIds.push(peerId)
}
return newPeerIds
}, [])
return disconnectedPeers
}
webFed.getAllFederationIds = () => {
console.warn("Multiple federations not yet supported")
return webFed.allFederationIds
}
webFed.getFederation = async (federationId) => {
console.warn("Multiple federations not yet supported")
return await gunDB.getObject([GUN_TLN_KEY, GUN_KEYS_SPEC.fedDetails, federationId])
}
webFed.createFederation = async (newFederationId, description='') => {
const federationId = newFederationId || crypto.randomUUID()
if (webFed.allFederationIds.includes(federationId)) {
console.error("Federation already exists!")
return
} else {
const federationObject = {
federationId,
description
}
try {
await gunDB.createObject([GUN_TLN_KEY, GUN_KEYS_SPEC['fedDetails']], federationId, federationObject) // Create a key to maintain federation specifics/details
await gunDB.createObject([GUN_TLN_KEY], federationId) // Create a key to add users into the federation
webFed.allFederationIds.push(federationId)
console.log(`Federation created with ID: ${federationId}`)
return federationId
} catch(e) {
console.error("Error occurred while creating federation:", e)
return undefined
}
}
}
webFed.joinFederation = async (federationId, clientId) => {
if (!federationId || !webFed.isFederation(federationId)) {
const errorMsg = "Parameter federationId missing or incorrect!"
console.error(errorMsg)
return errorMsg
}
if (!clientId) {
const errorMsg = "Parameter clientId missing or incorrect!"
console.error(errorMsg)
return errorMsg
}
const selfJoinedTime = Date.now()
if (!(await webFed.isClientInFederation(federationId, clientId))) {
const clientIdRecord = await gunDB.createObject([GUN_TLN_KEY, federationId], clientId, clientId)
const clientJoinedTimeRecord = await gunDB.createObject([GUN_TLN_KEY, federationId, clientId], "joinedTime", selfJoinedTime)
const clientFederationId = await gunDB.createObject([GUN_TLN_KEY, federationId, clientId], "federationId", federationId)
}
const clientRecordInUsers = await gunDB.createObject([GUN_TLN_KEY, federationId, GUN_KEYS_SPEC.users], clientId, selfJoinedTime)
if (webFed.currentFederation?.id) {
await gunDB.untrackObject([GUN_TLN_KEY, webFed.currentFederation.id, GUN_KEYS_SPEC['users']])
}
webFed.currentFederation.id = federationId
const refreshPeerConnectionsFromDB = (newClients) => {
if (newClients) {
const {_, ...newPeers} = newClients
const peersToBeConnectedWith = Object.keys(newPeers).filter(peerId => peerId !== clientId && !webFed.currentFederation.clients[peerId])
if (peersToBeConnectedWith.length > 0) {
peersToBeConnectedWith.forEach(async (peerId) => {
await webFed.updateAllClientsList(federationId, clientId)
webFed.connectToPeer(webFed.currentFederation.id, clientId, peerId)
})
}
}
}
await gunDB.trackObject([GUN_TLN_KEY, webFed.currentFederation.id, GUN_KEYS_SPEC['users']], refreshPeerConnectionsFromDB, true)
await webFed.connectToPeers(federationId, clientId)
return federationId
}
webFed.connectToPeers = async (federationId=webFed.currentFederation.id, selfClientId) => {
await webFed.updateAllClientsList(federationId, selfClientId)
const newPeers = await webFed.getDisconnectedPeers(federationId, selfClientId)
newPeers.forEach(async (peerId) => {
webFed.connectToPeer(federationId, selfClientId, peerId)
})
}
webFed.connectToPeer = async (federationId=webFed.currentFederation.id, selfClientId, peerId, peerProperties = undefined) => {
const createOffer = async (peerId) => {
console.log("Creating Offer")
webFed.currentFederation.clients[peerId].localConnection = new RTCPeerConnection(configuration)
webFed.currentFederation.clients[peerId].localConnection.onsignalingstatechange = (e) => {
console.log("Local signaling state change:\n", e.currentTarget.signalingState)
}
webFed.currentFederation.clients[peerId].localConnection.onicegatheringstatechange = (e) => {
console.log("Local ICE gathering state change:\n", e.currentTarget.iceGatheringState)
}
webFed.currentFederation.clients[peerId].localConnection.oniceconnectionstatechange = (e) => {
console.log("Local signaling state change:\n", e.currentTarget.iceConnectionState)
}
webFed.currentFederation.clients[peerId].localConnection.onicecandidate = (e) => console.log("New ICE Candidate", e.candidate)
// webFed.currentFederation.clients[peerId].localConnection.onicecandidateerror = console.error
webFed.currentFederation.clients[peerId].dataChannel = webFed.currentFederation.clients[peerId].localConnection.createDataChannel("channel");
await new Promise(resolve => setTimeout(resolve, 1000))
const offer = await webFed.currentFederation.clients[peerId].localConnection.createOffer();
await webFed.currentFederation.clients[peerId].localConnection.setLocalDescription(offer);
// webFed.currentFederation.clients[peerId].localConnection = localConnection
// webFed.currentFederation.clients[peerId].dataChannel = dataChannel
return JSON.parse(JSON.stringify(webFed.currentFederation.clients[peerId].localConnection.localDescription))
}
const createAnswer = async (offer, peerId) => {
let answer = undefined
console.log("Creating Answer")
webFed.currentFederation.clients[peerId].remoteConnection = new RTCPeerConnection(configuration)
webFed.currentFederation.clients[peerId].remoteConnection.onsignalingstatechange = (e) => console.log("Remote signaling state change:\n", e.currentTarget.signalingState)
webFed.currentFederation.clients[peerId].remoteConnection.onicegatheringstatechange = (e) => {
console.log("Remote ICE gathering state change:\n", e.currentTarget.iceGatheringState)
}
webFed.currentFederation.clients[peerId].remoteConnection.oniceconnectionstatechange = (e) => {
console.log("Remote signaling state change:\n", e.currentTarget.iceConnectionState)
}
webFed.currentFederation.clients[peerId].remoteConnection.onicecandidate = (e) => console.log("New ICE Candidate", e.candidate)
if (!webFed.currentFederation.clients[peerId].remoteConnection.remoteDescription) {
await webFed.currentFederation.clients[peerId].remoteConnection.setRemoteDescription(offer)
await new Promise(resolve => setTimeout(resolve, 1000))
answer = await webFed.currentFederation.clients[peerId].remoteConnection.createAnswer()
await webFed.currentFederation.clients[peerId].remoteConnection.setLocalDescription(answer)
await new Promise(resolve => setTimeout(resolve, 1000))
// webFed.currentFederation.clients[peerId].remoteConnection = remoteConnection
}
return JSON.parse(JSON.stringify(webFed.currentFederation.clients[peerId].remoteConnection.localDescription))
}
const selfJoinedTime = await gunDB.getObject([GUN_TLN_KEY, federationId, selfClientId, "joinedTime"])
let peerJoinedTime = await gunDB.getObject([GUN_TLN_KEY, federationId, peerId, "joinedTime"])
// HACK: peerJoinedTime sometimes doesn't get returned, no idea why. For now, get it from the users key as a workaround.
if (!peerJoinedTime) {
peerJoinedTime = await gunDB.getObject([GUN_TLN_KEY, federationId, GUN_KEYS_SPEC['users'], peerId])
}
if (webFed.currentFederation.clients[peerId].connectionState === "disconnected") {
webFed.currentFederation.clients[peerId].connectionState = "attempting"
document.dispatchEvent(new CustomEvent('newPeer', {
detail: {
peerId
}
}))
if (selfJoinedTime < peerJoinedTime) {
// If peer joined later, create an offer and wait to receive an answer.
gunDB.trackObject([GUN_TLN_KEY, federationId, peerId, "answers", selfClientId], (answer) => {
// const answer = await mnist.getFromObjectProps(peerId, `answer/${localStorage.clientId}`)
if (answer && webFed.currentFederation.clients[peerId].connectionState !== "receiving_answer") {
webFed.currentFederation.clients[peerId].connectionState = "receiving_answer"
webFed.currentFederation.clients[peerId].dataChannel.onopen = (e) => {
webFed.currentFederation.clients[peerId].connectionState = "connected"
document.dispatchEvent(new CustomEvent('peerConnected', {
detail: {
peerId
}
}))
gunDB.untrackObject([GUN_TLN_KEY, federationId, peerId, "answers", selfClientId])
gunDB.updateObject([GUN_TLN_KEY, federationId, selfClientId, "offers", peerId], null)
}
// webFed.currentFederation.clients[peerId].dataChannel.onmessage = (msg) => console.log("Received data from peer:\n", msg.data)
if (!webFed.currentFederation.clients[peerId].localConnection.remoteDescription) {
setTimeout(() => webFed.currentFederation.clients[peerId].localConnection.setRemoteDescription(answer), 1000)
console.log("Remote description set")
}
}
}, true)
const offer = await createOffer(peerId)
webFed.currentFederation.clients[peerId].connectionState = "offer"
// const offerObj = gun.get(peerId).put(offer)
await gunDB.createObject([GUN_TLN_KEY, federationId, selfClientId, "offers"], peerId, offer)
} else {
gunDB.trackObject([GUN_TLN_KEY, federationId, peerId, "offers", selfClientId], async (offer) => {
// const offer = await mnist.getFromObjectProps(peerId, `offer/${localStorage.clientId}`)
if (offer && webFed.currentFederation.clients[peerId].connectionState != "answering") {
webFed.currentFederation.clients[peerId].connectionState = "answering"
const answer = await createAnswer(offer, peerId)
webFed.currentFederation.clients[peerId].remoteConnection.ondatachannel = (e) => {
webFed.currentFederation.clients[peerId].dataChannel = e.channel
webFed.currentFederation.clients[peerId].dataChannel.onopen = (e) => {
webFed.currentFederation.clients[peerId].connectionState = "connected"
document.dispatchEvent(new CustomEvent('peerConnected', {
detail: {
peerId
}
}))
gunDB.untrackObject([GUN_TLN_KEY, federationId, peerId, "offers", selfClientId])
gunDB.updateObject([GUN_TLN_KEY, federationId, selfClientId, "answers", peerId], null)
}
// webFed.currentFederation.clients[peerId].dataChannel.onmessage = (msg) => {
// console.log("msg from peer:", msg)
// }
}
gunDB.createObject([GUN_TLN_KEY, federationId, selfClientId, "answers"], peerId, answer)
}
}, true)
}
}
}
webFed.sendDataToPeer = (peerId, data) => {
if (typeof(data) === 'object') {
data = JSON.stringify(data)
}
webFed.currentFederation.clients[peerId].dataChannel.send(data)
}
webFed.broadcastToAllPeers = async (federationId, clientId, data) => {
if (typeof(data) === 'object') {
data = JSON.stringify(data)
}
const peersCommunicated = []
const allPeers = await webFed.getAllPeers(federationId, clientId)
Object.values(allPeers).forEach(peer => {
if (peer.dataChannel && peer.dataChannel.readyState === 'open') {
peer.dataChannel.send(data)
peersCommunicated.push(peer.id)
}
})
return peersCommunicated
}
webFed.sendDataToPeer = (peerId, data) => {
p2p.sendDataToPeer(peerId, data)
}
webFed.broadcastData = (data) => {
p2p.broadcastData(data)
}
webFed.listenForMessageFromPeer = (peerId, cb, once=true) =>
webFed.currentFederation.clients[peerId].dataChannel.addEventListener("message", cb, {
once
})
webFed.initializeFederation = async (initOptions) => {
const { clientId: connectedClientId, federationId: connectedFederationId } = await p2p.initializeFederation(initOptions)
webFed.group = p2p.group
// if (!gunServerPath) {
// console.error("Path to Gun server required")
// }
// if (!clientId) {
// clientId = crypto.randomUUID()
// }
// gunDB.initialize(gunServerPath)
// const refreshFederationsFromDB = async () => {
// const allFederations = await gunDB.getObject([GUN_TLN_KEY, GUN_KEYS_SPEC['fedDetails']])
// if (allFederations && !Object.keys(allFederations).every((fed, ind) => fed === webFed.allFederationIds[ind])) {
// webFed.allFederationIds = Object.keys(allFederations)
// if (currentFederationId) {
// await webFed.updateAllClientsList(currentFederationId, clientId)
// if (webFed.currentFederation.clients?.[clientId] && webFed.currentFederation.id !== currentFederationId) {
// if (webFed.currentFederation.id) {
// gunDB.untrackObject([GUN_TLN_KEY, webFed.currentFederation.id, GUN_KEYS_SPEC['users']])
// }
// webFed.currentFederation.id = currentFederationId
// } else {
// currentFederationId = undefined
// }
// }
// document.dispatchEvent(new CustomEvent('federationsChanged'))
// }
// }
// await refreshFederationsFromDB()
// gunDB.trackObject([GUN_TLN_KEY, GUN_KEYS_SPEC['fedDetails']], refreshFederationsFromDB)
return { connectedClientId, connectedFederationId }
}
export default webFed