git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 61eb05a360ac5413a3b250bbbfdf7b0a33560d93

add copy functio to creation service

Signed-off-by: wanderer <mjbecze@gmail.com>
wanderer committed on 9/10/2017, 4:41:34 AM
Parent: 5d73da450ff3f8fae6bf5844ed02da668a06e011

Files changed

creationService.jschanged
portManager.jschanged
tests/index.jschanged
creationService.jsView
@@ -1,5 +1,7 @@
11 const chunk = require('chunk')
2+const Message = require('primea-message')
3+const DeleteMessage = require('./deleteMessage.js')
24
35 const MAX_DATA_BYTES = 65533
46
57 module.exports = class CreationService {
@@ -15,8 +17,12 @@
1517 const creator = this.scheduler.getInstance(message.fromId)
1618 id = creator.generateNextId()
1719 }
1820 return this.createInstance(message, id)
21+ } else if (message.responsePort && !(message instanceof DeleteMessage)) {
22+ this.send(message.responsePort, new Message({
23+ ports: [this.getPort()]
24+ }))
1925 }
2026 }
2127
2228 getPort () {
@@ -25,15 +31,15 @@
2531 destId: 0
2632 }
2733 }
2834
29- // send (port, message) {
30- // message._hops++
31- // message._fromTicks = this.ticks
32- // message.fromId = this.id
35+ send (port, message) {
36+ message._hops++
37+ message._fromTicks = this.ticks
38+ message.fromId = this.id
3339
34- // return this.hypervisor.send(port, message)
35- // }
40+ return this.hypervisor.send(port, message)
41+ }
3642
3743 /**
3844 * creates an new container instances and save it in the state
3945 * @returns {Promise}
portManager.jsView
@@ -1,26 +1,7 @@
11 const DeleteMessage = require('./deleteMessage')
22
3-// decides which message to go first
4-function messageArbiter (portA, portB) {
5- const a = portA.messages[0]
6- const b = portB.messages[0]
73
8- if (!a) {
9- return portB
10- } else if (!b) {
11- return portA
12- }
13-
14- // order by number of ticks if messages have different number of ticks
15- if (a._fromTicks !== b._fromTicks) {
16- return a._fromTicks < b._fromTicks ? portA : portB
17- } else {
18- // insertion order
19- return portA
20- }
21-}
22-
234 module.exports = class PortManager {
245 /**
256 * The port manager manages the the ports. This inculdes creation, deletion
267 * fetching and waiting on ports
@@ -31,8 +12,10 @@
3112 */
3213 constructor (opts) {
3314 Object.assign(this, opts)
3415 this.ports = this.state.ports
16+
17+ this._waitingPorts = {}
3518 // tracks unbounded ports that we have
3619 this._unboundPorts = new Set()
3720 this._saturationPromise = new Promise((resolve, reject) => {
3821 this._saturationResolve = resolve
@@ -142,9 +125,9 @@
142125
143126 const numOfMsg = port.messages.push(message)
144127
145128 if (numOfMsg === 1) {
146- if (this._isSaturated(this.ports)) {
129+ if (isSaturated(this._waitingPorts)) {
147130 this._saturationResolve()
148131 this._saturationPromise = new Promise((resolve, reject) => {
149132 this._saturationResolve = resolve
150133 })
@@ -195,8 +178,10 @@
195178 let message = this._peekNextMessage(ports)
196179 let oldestTime = this.hypervisor.scheduler.leastNumberOfTicks()
197180 let saturated = false
198181
182+ this._waitingPorts = ports
183+
199184 const findOldestMessage = async () => {
200185 while (// end if we have a message older then slowest containers
201186 !((message && oldestTime >= message._fromTicks) ||
202187 // end if there are no messages and this container is the oldest contaner
@@ -218,9 +203,9 @@
218203 }
219204 }
220205
221206 await Promise.race([
222- this._whenSaturated().then(() => {
207+ this._whenSaturated(ports).then(() => {
223208 message = this._peekNextMessage(ports)
224209 saturated = true
225210 }),
226211 findOldestMessage()
@@ -228,17 +213,11 @@
228213
229214 return message
230215 }
231216
232- // tests wether or not all the ports have a message
233- _isSaturated (ports) {
234- const values = Object.values(ports)
235- return values.length === 0 ? true : values.every(port => port.messages.length !== 0)
236- }
237-
238217 // returns a promise that resolve when the ports are saturated
239- _whenSaturated () {
240- if (this._isSaturated(this.ports)) {
218+ _whenSaturated (ports) {
219+ if (isSaturated(ports)) {
241220 return Promise.resolve()
242221 } else {
243222 return this._saturationPromise
244223 }
@@ -266,4 +245,30 @@
266245 }
267246 }
268247 }
269248 }
249+
250+// tests wether or not all the ports have a message
251+function isSaturated (ports) {
252+ const values = Object.values(ports)
253+ return values.length ? values.every(port => port.messages.length) : true
254+}
255+
256+// decides which message to go first
257+function messageArbiter (portA, portB) {
258+ const a = portA.messages[0]
259+ const b = portB.messages[0]
260+
261+ if (!a) {
262+ return portB
263+ } else if (!b) {
264+ return portA
265+ }
266+
267+ // order by number of ticks if messages have different number of ticks
268+ if (a._fromTicks !== b._fromTicks) {
269+ return a._fromTicks < b._fromTicks ? portA : portB
270+ } else {
271+ // insertion order
272+ return portA
273+ }
274+}
tests/index.jsView
@@ -1147,5 +1147,40 @@
11471147 '/': 'zdpuAonuhk7ZhdghJh4saaUCskY5mXZ6M9BcV9iAhCanAQx9i'
11481148 }
11491149 t.deepEquals(stateRoot, expectedSR)
11501150 })
1151+
1152+ tape('creation service - port copy', async t => {
1153+ t.plan(2)
1154+ class TestVMContainer extends BaseContainer {
1155+ onCreation (m) {
1156+ const creationPort = m.ports[0]
1157+
1158+ const message = this.kernel.createMessage()
1159+ const responePort = this.kernel.getResponsePort(message)
1160+
1161+ return Promise.all([
1162+ this.kernel.ports.bind('response', responePort),
1163+ this.kernel.send(creationPort, message)
1164+ ])
1165+ }
1166+ onMessage (m) {
1167+ t.equal(m.fromName, 'response')
1168+ t.equal(m.ports.length, 1)
1169+ }
1170+ }
1171+
1172+ const hypervisor = new Hypervisor(node.dag)
1173+ hypervisor.registerContainer(TestVMContainer)
1174+
1175+ const port = hypervisor.creationService.getPort()
1176+
1177+ const root = await hypervisor.send(port, new Message({
1178+ data: {
1179+ type: TestVMContainer.typeId
1180+ },
1181+ ports: [port]
1182+ }))
1183+
1184+ hypervisor.pin(root)
1185+ })
11511186 })

Built with git-ssb-web