-
-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbroadcastPersistence.js
More file actions
156 lines (134 loc) · 3.62 KB
/
broadcastPersistence.js
File metadata and controls
156 lines (134 loc) · 3.62 KB
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
'use strict'
const Packet = require('aedes-packet')
// System topics for subscription management
const TOPIC_ADD_SUBSCRIPTION = '$SYS/sub/add'
const TOPIC_REMOVE_SUBSCRIPTION = '$SYS/sub/rm'
const TOPIC_SUBSCRIPTION_PATTERN = '$SYS/sub/+'
// Constants to increase readability
const SUBSCRIBE = true
const UNSUBSCRIBE = false
function getKey (clientId, isSub, topic) {
return `${clientId}-${isSub ? 'sub_' : 'unsub_'}${topic || ''}`
}
function brokerPublish (broker, topic, clientId, subs, cb) {
const encoded = JSON.stringify({ clientId, subs })
const packet = new Packet({
topic,
payload: encoded
})
broker.publish(packet, cb)
}
class BroadcastPersistence {
// private members start with #
#waiting
#broker
#trie
#onSubMessage
constructor (broker, trie) {
this.#waiting = new Map()
this.#broker = broker
this.#trie = trie
this.#onSubMessage = this.#onMessage.bind(this)
}
#onMessage (packet, cb) {
const decoded = JSON.parse(packet.payload)
const clientId = decoded.clientId
const isAddSubscription = packet.topic === TOPIC_ADD_SUBSCRIPTION
for (let i = 0; i < decoded.subs.length; i++) {
const sub = decoded.subs[i]
sub.clientId = clientId
if (isAddSubscription) {
if (sub.qos > 0) {
this.#trie.add(sub.topic, sub)
} else {
this.#trie.remove(sub.topic, sub)
}
} else if (packet.topic === TOPIC_REMOVE_SUBSCRIPTION) {
this.#trie.remove(sub.topic, sub)
}
}
if (decoded.subs.length > 0) {
const key = getKey(clientId, isAddSubscription, decoded.subs[0].topic)
const waiting = this.#waiting.get(key)
if (waiting) {
this.#waiting.delete(key)
process.nextTick(waiting)
}
}
cb()
}
#waitFor (client, isSub, topic, cb) {
this.#waiting.set(getKey(client.id, isSub, topic), cb)
}
async addedSubscriptions (client, subs) {
if (subs.length === 0) {
return client
}
return new Promise((resolve, reject) => {
let errored = false
this.#waitFor(client, SUBSCRIBE, subs[0].topic, (err) => {
if (!errored && err) {
return reject(err)
}
if (!errored) {
resolve(client)
}
})
brokerPublish(this.#broker, TOPIC_ADD_SUBSCRIPTION, client.id, subs, (err) => {
if (err) {
errored = true
reject(err)
}
})
})
}
async removedSubscriptions (client, subs) {
let key = subs
if (subs.length > 0) {
key = subs[0]
}
return new Promise((resolve, reject) => {
let errored = false
this.#waitFor(client, UNSUBSCRIBE, key, (err) => {
if (!errored && err) {
return reject(err)
}
if (!errored) {
resolve(client)
}
})
const mappedSubs = subs.map(sub => { return { topic: sub } })
brokerPublish(this.#broker, TOPIC_REMOVE_SUBSCRIPTION, client.id, mappedSubs, (err) => {
if (err) {
errored = true
reject(err)
}
})
})
}
async brokerSubscribe () {
return new Promise((resolve, reject) => {
this.#broker.subscribe(
TOPIC_SUBSCRIPTION_PATTERN,
this.#onSubMessage,
(err) => {
if (err) return reject(err)
resolve()
}
)
})
}
async brokerUnsubscribe () {
return new Promise((resolve, reject) => {
this.#broker.unsubscribe(
TOPIC_SUBSCRIPTION_PATTERN,
this.#onSubMessage,
(err) => {
if (err) return reject(err)
resolve()
}
)
})
}
}
module.exports = BroadcastPersistence