Files: e62cd9ffb288088f86f5a80cfd7f8e5dc58a6b5c / inbox.js
4717 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 | /** |
34 | * queues a message |
35 | * @param {Message} message |
36 | */ |
37 | queue (message) { |
38 | this._queueMessage(message) |
39 | |
40 | const oldestMessage = this._getOldestMessage() |
41 | if (oldestMessage === message) { |
42 | this._oldestMessageResolve(message) |
43 | this._oldestMessagePromise = new Promise((resolve, reject) => { |
44 | this._oldestMessageResolve = resolve |
45 | }) |
46 | } |
47 | } |
48 | |
49 | /** |
50 | * Waits for a message sent with a capablitly that has one of the given tags |
51 | * @param {Array<*>} tags |
52 | * @param {Integer} timeout |
53 | * @returns {Promise} |
54 | */ |
55 | async nextTaggedMessage (tags, timeout) { |
56 | this._waitingTags = new Set(tags) |
57 | this._queue = this._queue.filter(message => !this._queueTaggedMessage(message)) |
58 | |
59 | // todo: add saturation test |
60 | const message = await this.nextMessage(timeout) |
61 | delete this._waitingTags |
62 | this._waitingTagsQueue.forEach(message => this._queueMessage(message)) |
63 | this._waitingTagsQueue = [] |
64 | |
65 | return message |
66 | } |
67 | |
68 | /** |
69 | * Waits for the the next message if any |
70 | * @param {Integer} timeout |
71 | * @returns {Promise} |
72 | */ |
73 | nextMessage (timeout, getCurrent = false) { |
74 | if (!this._gettingNextMessage) { |
75 | this._gettingNextMessage = this._nextMessage(timeout) |
76 | } else if (!getCurrent) { |
77 | throw new Error('already waiting for next message') |
78 | } |
79 | return this._gettingNextMessage |
80 | } |
81 | |
82 | async _nextMessage (timeout) { |
83 | await Promise.all([...this.actor._sending.values()]) |
84 | let message = this._getOldestMessage() |
85 | if (message === undefined && timeout === 0) { |
86 | return |
87 | } |
88 | |
89 | timeout += this.actor.ticks |
90 | let oldestTime = this.hypervisor.scheduler.leastNumberOfTicks(this.actor.id) |
91 | |
92 | while (true) { |
93 | if (message) { |
94 | // if the message we recived had more ticks then we currently have then |
95 | // update our ticks to it, since we jumped forward in time |
96 | if (message._fromTicks > this.actor.ticks) { |
97 | this.actor.ticks = message._fromTicks |
98 | this.hypervisor.scheduler.update(this.actor) |
99 | } |
100 | |
101 | // if there is a message that is "older" then the timeout, the lower |
102 | // the timeout to the oldest message |
103 | if (message._fromTicks < timeout) { |
104 | timeout = message._fromTicks |
105 | } |
106 | } |
107 | |
108 | // if all actors are "older" then the time out then stop waiting for messages |
109 | // since we konw that we can not receive one |
110 | if (oldestTime >= timeout) { |
111 | break |
112 | } |
113 | |
114 | await Promise.race([ |
115 | this.hypervisor.scheduler.wait(timeout, this.actor.id).then(() => { |
116 | message = this._getOldestMessage() |
117 | }), |
118 | this._olderMessage(message).then(m => { |
119 | message = m |
120 | }) |
121 | ]) |
122 | oldestTime = this.hypervisor.scheduler.leastNumberOfTicks(this.actor.id) |
123 | } |
124 | this._gettingNextMessage = false |
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