This repository was archived by the owner on Dec 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathrouter.js
133 lines (110 loc) · 4.31 KB
/
router.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
const {CompositeDisposable, Emitter} = require('event-kit')
const {RouterMessage} = require('./teletype-client_pb')
const convertToProtobufCompatibleBuffer = require('./convert-to-protobuf-compatible-buffer')
module.exports =
class Router {
constructor (network) {
this.network = network
this.emitter = new Emitter()
this.subscriptions = new CompositeDisposable()
this.subscriptions.add(network.onReceive(this.receive.bind(this)))
this.nextRequestId = 0
this.requestPromiseResolveCallbacks = new Map()
this.peerIdsByRequestId = new Map()
this.lastReceivePromise = Promise.resolve()
}
dispose () {
this.subscriptions.dispose()
}
notify ({recipientId, channelId, body}) {
body = convertToProtobufCompatibleBuffer(body)
const notification = new RouterMessage.Notification()
notification.setChannelId(channelId)
if (body != null) notification.setBody(body)
const routerMessage = new RouterMessage()
routerMessage.setNotification(notification)
if (recipientId) {
this.network.unicast(recipientId, routerMessage.serializeBinary())
} else {
this.network.broadcast(routerMessage.serializeBinary())
}
}
request ({recipientId, channelId, body}) {
if (body) body = convertToProtobufCompatibleBuffer(body)
const requestId = this.nextRequestId++
const request = new RouterMessage.Request()
request.setChannelId(channelId)
request.setRequestId(requestId)
if (body) request.setBody(body)
const routerMessage = new RouterMessage()
routerMessage.setRequest(request)
this.network.unicast(recipientId, routerMessage.serializeBinary())
return new Promise((resolve) => {
this.requestPromiseResolveCallbacks.set(requestId, resolve)
})
}
respond ({requestId, ok, body}) {
const recipientId = this.peerIdsByRequestId.get(requestId)
if (!recipientId) throw new Error('Multiple responses to the same request are not allowed')
if (ok == null) ok = true
if (body) body = convertToProtobufCompatibleBuffer(body)
const response = new RouterMessage.Response()
response.setRequestId(requestId)
response.setOk(ok)
response.setBody(body)
const routerMessage = new RouterMessage()
routerMessage.setResponse(response)
this.peerIdsByRequestId.delete(requestId)
this.network.unicast(recipientId, routerMessage.serializeBinary())
}
onNotification (channelId, callback) {
return this.emitter.on('notification:' + channelId, callback)
}
onRequest (channelId, callback) {
return this.emitter.on('request:' + channelId, callback)
}
receive ({senderId, message}) {
const routerMessage = RouterMessage.deserializeBinary(message)
if (routerMessage.hasNotification()) {
this.receiveNotification(senderId, routerMessage.getNotification())
} else if (routerMessage.hasRequest()) {
this.receiveRequest(senderId, routerMessage.getRequest())
} else if (routerMessage.hasResponse()) {
this.receiveResponse(routerMessage.getResponse())
} else {
throw new Error('Unsupported router message variant')
}
}
receiveNotification (senderId, notification) {
this.lastReceivePromise = this.lastReceivePromise.then(async () => {
const channelId = notification.getChannelId()
const body = convertToProtobufCompatibleBuffer(notification.getBody())
await this.emitter.emitAsync(
'notification:' + channelId,
{senderId, body}
)
})
}
receiveRequest (senderId, request) {
this.lastReceivePromise = this.lastReceivePromise.then(async () => {
const channelId = request.getChannelId()
const requestId = request.getRequestId()
const eventName = 'request:' + channelId
const body = convertToProtobufCompatibleBuffer(request.getBody())
this.peerIdsByRequestId.set(requestId, senderId)
if (this.emitter.listenerCountForEventName(eventName) === 0) {
this.respond({requestId, ok: false})
} else {
await this.emitter.emitAsync(eventName, {senderId, requestId, body})
}
})
}
receiveResponse (response) {
const requestId = response.getRequestId()
const requestResolveCallback = this.requestPromiseResolveCallbacks.get(requestId)
requestResolveCallback({
body: convertToProtobufCompatibleBuffer(response.getBody()),
ok: response.getOk()
})
}
}