git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 9b7a855ce9b773c9162179ef6cdb3c70739711f0

wrote docs

wanderer committed on 5/23/2017, 12:29:00 AM
Parent: a728a4416ce9e9ab6ba38dc79dde2c7a294feb7e

Files changed

exoInterface.jschanged
index.jschanged
port.jschanged
portManager.jschanged
exoInterface.jsView
@@ -82,8 +82,9 @@
8282 /**
8383 * run the kernels code with a given enviroment
8484 * The Kernel Stores all of its state in the Environment. The Interface is used
8585 * to by the VM to retrive infromation from the Environment.
86+ * @returns {Promise}
8687 */
8788 async run (message) {
8889 const oldState = clone(this.state, false, 3)
8990 let result
@@ -105,9 +106,10 @@
105106 }
106107
107108 /**
108109 * returns a promise that resolves once the kernel hits the threshould tick count
109- *
110+ * @param {Number} threshould - the number of ticks to wait
111+ * @returns {Promise}
110112 */
111113 wait (threshold, fromPort) {
112114 if (threshold <= this.ticks) {
113115 return this.ticks
@@ -123,8 +125,12 @@
123125 })
124126 }
125127 }
126128
129+ /**
130+ * updates the number of ticks that the container has run
131+ * @param {Number} count - the number of ticks to add
132+ */
127133 incrementTicks (count) {
128134 this.ticks += count
129135 for (const [fromPort, waiter] of this._waitingMap) {
130136 if (waiter.threshold < this.ticks) {
@@ -133,8 +139,13 @@
133139 }
134140 }
135141 }
136142
143+ /**
144+ * sends a message to a given port
145+ * @param {Object} portRef - the port
146+ * @param {Message} message - the message
147+ */
137148 async send (portRef, message) {
138149 if (!this.ports.isValidPort(portRef)) {
139150 throw new Error('invalid port referance')
140151 }
@@ -146,14 +157,20 @@
146157 const container = await this.getInstance(portRef)
147158 container.queue(message)
148159
149160 const waiter = this._waitingMap.get(portRef)
161+ // if the was a wait on this port the resolve it
150162 if (waiter) {
151163 waiter.resolve(this.ticks)
152164 this._waitingMap.delete(portRef)
153165 }
154166 }
155167
168+ /**
169+ * gets a container instance given a port
170+ * @param {Object} portRef - the port
171+ * @returns {Object}
172+ */
156173 getInstance (portRef) {
157174 return this.hypervisor.getInstanceByPort(portRef, this.entryPort)
158175 }
159176 }
index.jsView
@@ -98,8 +98,9 @@
9898 * creates a state root starting from a given container and a given number of
9999 * ticks
100100 * @param {Container} container an container instance
101101 * @param {Number} ticks the number of ticks at which to create the state root
102+ * @returns {Promise}
102103 */
103104 async createStateRoot (container, ticks) {
104105 await container.wait(ticks)
105106 return this.graph.flush(container.state)
port.jsView
@@ -1,24 +1,44 @@
11 module.exports = class Port {
2- constructor (name) {
3- this.name = name
2+ /**
3+ * a simple repsentation of a port
4+ * @property {Interger} ticks - the last know number of ticks the
5+ * corrisponding container is at
6+ */
7+ constructor () {
48 this._queue = []
59 this.ticks = 0
610 }
711
12+ /**
13+ * queues a message on the port
14+ * @param {Message}
15+ */
816 queue (message) {
917 this.ticks = message._fromPortTicks
1018 this._queue.push(message)
1119 }
1220
21+ /**
22+ * returns the message at the front of the queue
23+ * @returns {Message}
24+ */
1325 peek () {
1426 return this._queue[0]
1527 }
1628
17- shift () {
29+ /**
30+ * dequeue a message
31+ * @returns {Message}
32+ */
33+ dequeue () {
1834 return this._queue.shift()
1935 }
2036
37+ /**
38+ * returns the size of the queue
39+ * @returns {Integer}
40+ */
2141 get size () {
2242 return this._queue.length
2343 }
2444 }
portManager.jsView
@@ -27,13 +27,28 @@
2727 }
2828 }
2929
3030 module.exports = class PortManager {
31+ /**
32+ * The port manager manages the the ports. This inculdes creation, deletion
33+ * fetching and waiting on ports
34+ * @param {Object} opts
35+ * @param {Object} opts.state
36+ * @param {Object} opts.entryPort
37+ * @param {Object} opts.parentPort
38+ * @param {Object} opts.hypervisor
39+ * @param {Object} opts.exoInterface
40+ */
3141 constructor (opts) {
3242 Object.assign(this, opts)
3343 this._portMap = new Map()
3444 }
3545
46+ /**
47+ * starts the port manager. This fetchs the ports from the state and maps
48+ * them to thier names
49+ * @returns {Promise}
50+ */
3651 async start () {
3752 // skip the root, since it doesn't have a parent
3853 if (this.parentPort !== undefined) {
3954 this._bindRef(this.parentPort, ENTRY)
@@ -47,36 +62,64 @@
4762 })
4863 }
4964
5065 _bindRef (portRef, name) {
51- const port = new Port(name)
66+ const port = new Port()
5267 this._portMap.set(portRef, port)
5368 }
5469
70+ /**
71+ * binds a port to a name
72+ * @param {Object} port - the port to bind
73+ * @param {String} name - the name of the port
74+ */
5575 bind (port, name) {
5676 // save the port instance
5777 this.ports[name] = port
5878 this._bindRef(port, name)
5979 }
6080
81+ /**
82+ * queues a message on a port
83+ * @param {Message} message
84+ */
6185 queue (message) {
6286 this._portMap.get(message.fromPort).queue(message)
6387 }
6488
65- get (key) {
66- return this.ports[key]
89+ /**
90+ * gets a port given it's name
91+ * @param {String} name
92+ * @return {Object}
93+ */
94+ get (name) {
95+ return this.ports[name]
6796 }
6897
69- delete (key) {
70- const port = this.ports[key]
71- delete this.ports[key]
98+ /**
99+ * deletes a port given its name
100+ * @param {String} name
101+ */
102+ delete (name) {
103+ const port = this.ports[name]
104+ delete this.ports[name]
72105 this._portMap.delete(port)
73106 }
74107
108+ /**
109+ * check if a port object is still valid
110+ * @param {Object} port
111+ * @return {Boolean}
112+ */
75113 isValidPort (port) {
76114 return this._portMap.has(port)
77115 }
78116
117+ /**
118+ * creates a new Port given the container type
119+ * @param {String} type
120+ * @returns {Object} the newly created port
121+ */
79122 create (type) {
80123 const Container = this.hypervisor._containerTypes[type]
81124 const parentId = this.entryPort ? this.entryPort.id : null
82125 let nonce = this.state['/'].nonce
@@ -101,12 +144,18 @@
101144 this.state['/'].nonce = nonce.toArray()
102145 return portRef
103146 }
104147
105- // waits till all ports have reached a threshold tick count
106- wait (threshold, fromPort = this.entryPort, ports = this._portMap) {
148+ /**
149+ * waits till all ports have reached a threshold tick count
150+ * @param {Integer} threshold - the number of ticks to wait
151+ * @param {Object} fromPort - the port requesting the wait
152+ * @param {Array} ports - the ports to wait on
153+ * @returns {Promise}
154+ */
155+ wait (threshold, fromPort = this.entryPort, ports = [...this._portMap]) {
107156 // find the ports that have a smaller tick count then the threshold tick count
108- const unkownPorts = [...ports].filter(([portRef, port]) => {
157+ const unkownPorts = ports.filter(([portRef, port]) => {
109158 return port.ticks < threshold && fromPort !== portRef
110159 })
111160
112161 const promises = unkownPorts.map(async([portRef, port]) => {
@@ -116,19 +165,24 @@
116165
117166 return Promise.all(promises)
118167 }
119168
120- async getNextMessage () {
169+ /**
170+ * gets the next canonical message given the an array of ports to choose from
171+ * @param {Array} ports
172+ * @returns {Promise}
173+ */
174+ async getNextMessage (ports = [...this._portMap]) {
121175 if (this._portMap.size) {
122176 // find the oldest message
123- const ticks = [...this._portMap].map(([name, port]) => {
177+ const ticks = ports.map(([name, port]) => {
124178 return port.size ? port.ticks : this.exoInterface.ticks
125179 }).reduce((ticksA, ticksB) => {
126180 return ticksA < ticksB ? ticksA : ticksB
127181 })
128182
129183 await this.wait(ticks)
130- const portMap = [...this._portMap].reduce(messageArbiter)
131- return portMap[1].shift()
184+ const portMap = ports.reduce(messageArbiter)
185+ return portMap[1].dequeue()
132186 }
133187 }
134188 }

Built with git-ssb-web