Files: 420c091edd500452248946f7f519a3f0bd7b3a3e / portManager.js
2676 bytesRaw
1 | const Port = require('./port.js') |
2 | const ENTRY = Symbol('entry') |
3 | |
4 | // decides which message to go firts |
5 | function messageArbiter (pairA, pairB) { |
6 | const a = pairA[1].peek() |
7 | const b = pairB[1].peek() |
8 | |
9 | if (!a) { |
10 | return pairB |
11 | } else if (!b) { |
12 | return pairA |
13 | } |
14 | |
15 | const aGasPrice = a.resources.gasPrice |
16 | const bGasPrice = b.resources.gasPrice |
17 | if (a.ticks !== b.ticks) { |
18 | return a.ticks < b.ticks ? pairA : pairB |
19 | } else if (aGasPrice === bGasPrice) { |
20 | return a.hash() > b.hash() ? pairA : pairB |
21 | } else { |
22 | return aGasPrice > bGasPrice ? pairA : pairB |
23 | } |
24 | } |
25 | |
26 | module.exports = class PortManager { |
27 | constructor (opts) { |
28 | Object.assign(this, opts) |
29 | this._portMap = new Map() |
30 | } |
31 | |
32 | async start () { |
33 | // map ports to thier id's |
34 | this.ports = await this.hypervisor.graph.get(this.state, 'ports') |
35 | const promises = Object.keys(this.ports).map(name => { |
36 | const port = this.ports[name] |
37 | this._mapPort(name, port) |
38 | }) |
39 | |
40 | // create the parent port |
41 | await Promise.all(promises) |
42 | // skip the root, since it doesn't have a parent |
43 | if (this.parentPort !== undefined) { |
44 | const id = await this.hypervisor.generateID(this.parentPort) |
45 | this._portMap.set(id, new Port(ENTRY)) |
46 | } |
47 | } |
48 | |
49 | async _mapPort (name, port) { |
50 | const id = await this.hypervisor.generateID(port) |
51 | port = new Port(name) |
52 | this._portMap.set(id, port) |
53 | } |
54 | |
55 | queue (message) { |
56 | this._portMap.get(message.fromPort).queue(message) |
57 | } |
58 | |
59 | set (name, port) { |
60 | this.ports[name] = port |
61 | return this._mapPort(name, port) |
62 | } |
63 | |
64 | async get (port) { |
65 | const id = await this.hypervisor.generateID(port) |
66 | return this._portMap.get(id) |
67 | } |
68 | |
69 | getRef (key) { |
70 | if (key === ENTRY) { |
71 | return this.entryPort |
72 | } else { |
73 | return this.ports[key] |
74 | } |
75 | } |
76 | |
77 | // waits till all ports have reached a threshold tick count |
78 | async wait (threshold, fromPort) { |
79 | // find the ports that have a smaller tick count then the threshold tick count |
80 | const unkownPorts = [...this._portMap].filter(([id, port]) => { |
81 | const portRef = this.getRef(port.name) |
82 | return port.ticks < threshold && fromPort !== portRef |
83 | }) |
84 | |
85 | const promises = unkownPorts.map(async ([id, port]) => { |
86 | const portObj = port.name === ENTRY ? this.parentPort : this.ports[port.name] |
87 | // update the port's tick count |
88 | port.ticks = await this.hypervisor.wait(portObj, threshold, this.entryPort) |
89 | }) |
90 | return Promise.all(promises) |
91 | } |
92 | |
93 | async getNextMessage () { |
94 | await this.wait(this.kernel.ticks, this.entryPort) |
95 | const portMap = [...this._portMap].reduce(messageArbiter) |
96 | if (portMap) { |
97 | return portMap[1].shift() |
98 | } |
99 | } |
100 | } |
101 |
Built with git-ssb-web