git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 3d251f593ad3a3a4e9b46eaf068ca9dc8ec39bfa

added polling message

wanderer committed on 4/18/2017, 8:17:33 AM
Parent: 1635a21816ba2086ab136257355c7a5550d7a7fd

Files changed

index.jschanged
kernel.jschanged
port.jschanged
portManager.jschanged
pollMessage.jsadded
tests/index.jsadded
index.jsView
@@ -1,5 +1,6 @@
11 const Kernel = require('./index.js')
2+const PollMessage = require('./pollMessage.js')
23
34 module.exports = class Hypervisor {
45 constructor (opts) {
56 this._opts = {
@@ -34,11 +35,11 @@
3435 }
3536
3637 // given a port, wait untill its source contract has reached the threshold
3738 // tick count
38- async waitOnPort (port, ticks) {
39- let kernel = this.getVMFromPort(port)
40- const tickCount = await kernel.wait(ticks)
41- port.ticks = tickCount
42- return tickCount
39+ waitOnPort (port, ticks) {
40+ const kernel = this.getVMFromPort(port)
41+ const message = new PollMessage(ticks)
42+ kernel.queue(message)
43+ return message.response()
4344 }
4445 }
kernel.jsView
@@ -17,9 +17,8 @@
1717 this.ports = new PortManager(this)
1818 this._waitingQueue = new PriorityQueue((a, b) => {
1919 return a.threshold > b.threshold
2020 })
21-
2221 this.on('result', this._runNextMessage)
2322 }
2423
2524 _updateState (message) {
@@ -33,9 +32,12 @@
3332 }
3433
3534 queue (message) {
3635 this.portManager.queue(message)
37- if (this.state === 'idle') {
36+ // handle system messages
37+ if (message.isPoll) {
38+ message.respond(this.wait(message.threshold))
39+ } else if (this.state === 'idle') {
3840 this._runNextMessage()
3941 }
4042 }
4143
port.jsView
@@ -6,9 +6,11 @@
66 }
77
88 queue (message) {
99 this.ticks = message.ticks
10- if (this._resolve) {
10+ if (message.isSystem) {
11+ return
12+ } if (this._resolve) {
1113 return this._resolve(message)
1214 } else {
1315 this.queue.push(message)
1416 }
portManager.jsView
@@ -16,8 +16,10 @@
1616 }
1717 })
1818 }
1919
20+ // temporaly queue message untill the ports have been mapped. Mapping the
21+ // ports is async since the ports could just be merkle links
2022 queue (message) {
2123 this._tempQueue.push(message)
2224 }
2325
@@ -55,18 +57,21 @@
5557 return this._portMap.get(id)
5658 }
5759
5860 // waits till all ports have reached a threshold tick count
59- async wait (ticks) {
61+ async wait (threshold) {
62+ // find the ports that have a smaller tick count then the threshold tick count
6063 const unkownPorts = [...this._ports].filter((id, port) => {
6164 const message = port.peek()
62- return !message || message.ticks < ticks
65+ return !message || message.ticks < threshold
6366 })
6467
65- const promises = []
66- for (const id in unkownPorts) {
67- promises.push(this.hypervisor.waitOnVM(id, ticks))
68- }
68+ const promises = unkownPorts.map(port => {
69+ this.hypervisor.waitOnVM(port, threshold).then(ticks => {
70+ // update the port's tick count
71+ port.ticks = ticks
72+ })
73+ })
6974 await Promise.all(promises)
7075 }
7176
7277 async getNextMessage (ticks) {
pollMessage.jsView
@@ -1,0 +1,26 @@
1+const Message = require('./')
2+module.exports = class PollMessage extends Message {
3+ constructor (threshold) {
4+ super()
5+ this.threshold = threshold
6+ this.promise = new Promise((resolve, reject) => {
7+ this._resolve = resolve
8+ })
9+ }
10+
11+ get isSystem () {
12+ return true
13+ }
14+
15+ get isPoll () {
16+ return true
17+ }
18+
19+ response () {
20+ return this.promise
21+ }
22+
23+ respond (tickPromise) {
24+ this._resolve(tickPromise)
25+ }
26+}
tests/index.jsView

Built with git-ssb-web