Files: 33a6e566ff3440039cadbd025088e2f289f3c59d / inbox.js
3938 bytesRaw
1 | const binarySearchInsert = require('binary-search-insert') |
2 | const Buffer = require('safe-buffer').Buffer |
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 | async nextMessage (timeout = 0) { |
74 | if (this._gettingNextMessage) { |
75 | throw new Error('already getting next message') |
76 | } else { |
77 | this._gettingNextMessage = true |
78 | } |
79 | |
80 | let message = this._getOldestMessage() |
81 | if (message === undefined && timeout === 0) { |
82 | return |
83 | } |
84 | |
85 | timeout += this.actor.ticks |
86 | let oldestTime = this.hypervisor.scheduler.leastNumberOfTicks(this.actor.id) |
87 | |
88 | while (true) { |
89 | if (message && message._fromTicks < timeout) { |
90 | timeout = message._fromTicks |
91 | } |
92 | |
93 | if (oldestTime >= timeout) { |
94 | break |
95 | } |
96 | |
97 | await Promise.race([ |
98 | this.hypervisor.scheduler.wait(timeout, this.actor.id).then(() => { |
99 | message = this._getOldestMessage() |
100 | }), |
101 | this._olderMessage(message).then(m => { |
102 | message = m |
103 | }) |
104 | ]) |
105 | oldestTime = this.hypervisor.scheduler.leastNumberOfTicks(this.actor.id) |
106 | } |
107 | this._gettingNextMessage = false |
108 | return this._deQueueMessage() |
109 | } |
110 | |
111 | // returns a promise that resolve when a message older then the given message |
112 | // is recived |
113 | _olderMessage (message) { |
114 | return this._oldestMessagePromise |
115 | } |
116 | |
117 | _getOldestMessage () { |
118 | if (this._waitingTags) { |
119 | return this._waitingTagsQueue[0] |
120 | } else { |
121 | return this._queue[0] |
122 | } |
123 | } |
124 | |
125 | _deQueueMessage () { |
126 | if (this._waitingTags) { |
127 | return this._waitingTagsQueue.shift() |
128 | } else { |
129 | return this._queue.shift() |
130 | } |
131 | } |
132 | |
133 | _queueMessage (message) { |
134 | if (!(this._waitingTags && this._queueTaggedMessage(message))) { |
135 | binarySearchInsert(this._queue, messageArbiter, message) |
136 | } |
137 | } |
138 | |
139 | _queueTaggedMessage (message) { |
140 | if (this._waitingTags.has(message.tag)) { |
141 | this._waitingTags.delete(message.tag) |
142 | binarySearchInsert(this._waitingTagsQueue, messageArbiter, message) |
143 | return true |
144 | } else { |
145 | return false |
146 | } |
147 | } |
148 | } |
149 |
Built with git-ssb-web