Files: df84d7871808036fce1c56eaa79c0565b9b5ddb6 / inbox.js
4581 bytesRaw
1 | const Buffer = require('safe-buffer').Buffer |
2 | const binarySearchInsert = require('binary-search-insert') |
3 | |
4 | // decides which message to go first |
5 | function messageArbiter (messageA, messageB) { |
6 | // order by number of ticks if messages have different number of ticks |
7 | if (messageA._fromTicks !== messageB._fromTicks) { |
8 | return messageA._fromTicks > messageB._fromTicks |
9 | } else { |
10 | // sender id |
11 | return Buffer.compare(messageA._fromId, messageB._fromId) |
12 | } |
13 | } |
14 | |
15 | module.exports = class Inbox { |
16 | /** |
17 | * The inbox manages and sorts incoming messages and provides functions |
18 | * to wait on messages |
19 | * @param {Object} opts |
20 | * @param {Object} opts.state |
21 | * @param {Object} opts.hypervisor |
22 | */ |
23 | constructor (opts) { |
24 | this.actor = opts.actor |
25 | this.hypervisor = opts.hypervisor |
26 | this._queue = [] |
27 | this._waitingTagsQueue = [] |
28 | this._oldestMessagePromise = new Promise((resolve, reject) => { |
29 | this._oldestMessageResolve = resolve |
30 | }) |
31 | } |
32 | |
33 | get isEmpty () { |
34 | return !this._queue.length |
35 | } |
36 | |
37 | /** |
38 | * queues a message |
39 | * @param {Message} message |
40 | */ |
41 | queue (message) { |
42 | this._queueMessage(message) |
43 | |
44 | const oldestMessage = this._getOldestMessage() |
45 | if (oldestMessage === message) { |
46 | this._oldestMessageResolve(message) |
47 | this._oldestMessagePromise = new Promise((resolve, reject) => { |
48 | this._oldestMessageResolve = resolve |
49 | }) |
50 | } |
51 | } |
52 | |
53 | /** |
54 | * Waits for a message sent with a capablitly that has one of the given tags |
55 | * @param {Array<*>} tags |
56 | * @param {Integer} timeout |
57 | * @returns {Promise} |
58 | */ |
59 | async nextTaggedMessage (tags, timeout) { |
60 | this._waitingTags = new Set(tags) |
61 | this._queue = this._queue.filter(message => !this._queueTaggedMessage(message)) |
62 | |
63 | // todo: add saturation test |
64 | const message = await this.nextMessage(timeout) |
65 | delete this._waitingTags |
66 | this._waitingTagsQueue.forEach(message => this._queueMessage(message)) |
67 | this._waitingTagsQueue = [] |
68 | |
69 | return message |
70 | } |
71 | |
72 | /** |
73 | * Waits for the the next message if any |
74 | * @param {Integer} timeout |
75 | * @returns {Promise} |
76 | */ |
77 | async nextMessage (timeout) { |
78 | if (!this._gettingNextMessage) { |
79 | this._gettingNextMessage = true |
80 | } else { |
81 | throw new Error('already waiting for next message') |
82 | } |
83 | |
84 | let message = this._getOldestMessage() |
85 | if (message) { |
86 | timeout = message._fromTicks |
87 | } else { |
88 | timeout += this.actor.ticks |
89 | } |
90 | |
91 | let oldestTime = this.hypervisor.scheduler.leastNumberOfTicks(this.actor.id) |
92 | |
93 | while (true) { |
94 | if (message) { |
95 | // if there is a message that is "older" then the timeout, the lower |
96 | // the timeout to the oldest message |
97 | if (message._fromTicks < timeout) { |
98 | timeout = message._fromTicks |
99 | } |
100 | } |
101 | |
102 | // if all actors are "older" then the time out then stop waiting for messages |
103 | // since we konw that we can not receive one |
104 | if (oldestTime >= timeout) { |
105 | break |
106 | } |
107 | |
108 | await Promise.race([ |
109 | this.hypervisor.scheduler.wait(timeout, this.actor.id).then(() => { |
110 | message = this._getOldestMessage() |
111 | }), |
112 | this._olderMessage(message).then(m => { |
113 | message = m |
114 | }) |
115 | ]) |
116 | oldestTime = this.hypervisor.scheduler.leastNumberOfTicks(this.actor.id) |
117 | } |
118 | this._gettingNextMessage = false |
119 | // if the message we recived had more ticks then we currently have then |
120 | // update our ticks to it, since we jumped forward in time |
121 | if (message && message._fromTicks > this.actor.ticks) { |
122 | this.actor.ticks = message._fromTicks |
123 | this.hypervisor.scheduler.update(this.actor) |
124 | } |
125 | return this._deQueueMessage() |
126 | } |
127 | |
128 | // returns a promise that resolve when a message older then the given message |
129 | // is recived |
130 | _olderMessage (message) { |
131 | return this._oldestMessagePromise |
132 | } |
133 | |
134 | _getOldestMessage () { |
135 | if (this._waitingTags) { |
136 | return this._waitingTagsQueue[0] |
137 | } else { |
138 | return this._queue[0] |
139 | } |
140 | } |
141 | |
142 | _deQueueMessage () { |
143 | if (this._waitingTags) { |
144 | return this._waitingTagsQueue.shift() |
145 | } else { |
146 | return this._queue.shift() |
147 | } |
148 | } |
149 | |
150 | _queueMessage (message) { |
151 | if (!(this._waitingTags && this._queueTaggedMessage(message))) { |
152 | binarySearchInsert(this._queue, messageArbiter, message) |
153 | } |
154 | } |
155 | |
156 | _queueTaggedMessage (message) { |
157 | if (this._waitingTags.has(message.tag)) { |
158 | this._waitingTags.delete(message.tag) |
159 | binarySearchInsert(this._waitingTagsQueue, messageArbiter, message) |
160 | return true |
161 | } else { |
162 | return false |
163 | } |
164 | } |
165 | } |
166 |
Built with git-ssb-web