git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit a378a2df93c85f8e6fe2dbfe58627b085c7bc7d3

implement port binding

wanderer committed on 5/16/2017, 2:59:11 PM
Parent: 1f8b8cb2aec1488e4f420467a81021af33fbb92b

Files changed

portManager.jschanged
tests/index.jschanged
portManager.jsView
@@ -71,9 +71,15 @@
7171 isValidPort (port) {
7272 return this._portMap.has(port)
7373 }
7474
75- create (type, name) {
75+ bind (port, name) {
76+ // save the port instance
77+ this.ports[name] = port
78+ this._mapPort(name, port)
79+ }
80+
81+ create (type) {
7682 const VM = this.hypervisor._VMs[type]
7783 const parentId = this.entryPort ? this.entryPort.id : null
7884 let nonce = this.state['/'].nonce
7985
@@ -90,12 +96,8 @@
9096 '/': VM.createState()
9197 }
9298 }
9399
94- // save the port instance
95- this.ports[name] = portRef
96- this._mapPort(name, portRef)
97-
98100 // incerment the nonce
99101 nonce = new BN(nonce)
100102 nonce.iaddn(1)
101103 this.state['/'].nonce = nonce.toArray()
tests/index.jsView
@@ -45,9 +45,10 @@
4545 const hypervisor = new Hypervisor({dag: node.dag})
4646 hypervisor.registerContainer('test', testVMContainer)
4747
4848 const rootContainer = await hypervisor.createInstance('test')
49- const port = await rootContainer.ports.create('test', 'first')
49+ const port = rootContainer.ports.create('test')
50+ rootContainer.ports.bind(port, 'first')
5051
5152 await rootContainer.send(port, message)
5253
5354 const stateRoot = await hypervisor.createStateRoot(rootContainer, Infinity)
@@ -76,9 +77,10 @@
7677 }
7778
7879 class testVMContainer extends BaseContainer {
7980 async run (m) {
80- const port = await this.kernel.ports.create('test2', 'child')
81+ const port = this.kernel.ports.create('test2')
82+ this.kernel.ports.bind(port, 'child')
8183 await this.kernel.send(port, m)
8284 this.kernel.incrementTicks(1)
8385 }
8486 }
@@ -87,10 +89,12 @@
8789 hypervisor.registerContainer('test', testVMContainer)
8890 hypervisor.registerContainer('test2', testVMContainer2)
8991
9092 let root = await hypervisor.createInstance('test')
91- let port = await root.ports.create('test', 'first')
93+ let port = root.ports.create('test')
9294
95+ root.ports.bind(port, 'first')
96+
9397 await root.send(port, message)
9498 const stateRoot = await hypervisor.createStateRoot(root, Infinity)
9599 t.true(hasResolved, 'should resolve before generating the state root')
96100 t.deepEquals(stateRoot, expectedState, 'expected state')
@@ -105,19 +109,20 @@
105109 }
106110
107111 hypervisor.registerContainer('test', testVMContainer3)
108112 root = await hypervisor.createInstance('test', stateRoot)
109- port = await root.ports.get('first')
113+ port = root.ports.get('first')
110114
111- await root.send(port, message)
115+ root.send(port, message)
112116 })
113117
114118 tape('ping pong', async t => {
115119 class Ping extends BaseContainer {
116120 async run (m) {
117121 let port = this.kernel.ports.get('child')
118122 if (!port) {
119- port = await this.kernel.ports.create('pong', 'child')
123+ port = this.kernel.ports.create('pong')
124+ this.kernel.ports.bind(port, 'child')
120125 }
121126
122127 if (this.kernel.ticks < 100) {
123128 this.kernel.incrementTicks(1)
@@ -140,9 +145,10 @@
140145
141146 hypervisor.registerContainer('ping', Ping)
142147 hypervisor.registerContainer('pong', Pong)
143148 const root = await hypervisor.createInstance('pong')
144- const port = await root.ports.create('ping', 'child')
149+ const port = root.ports.create('ping')
150+ root.ports.bind(port, 'child')
145151
146152 await root.send(port, new Message())
147153 await hypervisor.createStateRoot(root, Infinity)
148154
@@ -154,12 +160,16 @@
154160 let runs = 0
155161
156162 class Root extends BaseContainer {
157163 async run (m) {
158- const one = this.kernel.ports.create('child', 'one')
159- const two = this.kernel.ports.create('child', 'two')
160- const three = this.kernel.ports.create('child', 'three')
164+ const one = this.kernel.ports.create('child')
165+ const two = this.kernel.ports.create('child')
166+ const three = this.kernel.ports.create('child')
161167
168+ this.kernel.ports.bind(one, 'one')
169+ this.kernel.ports.bind(two, 'two')
170+ this.kernel.ports.bind(three, 'three')
171+
162172 await Promise.all([
163173 this.kernel.send(one, new Message()),
164174 this.kernel.send(two, new Message()),
165175 this.kernel.send(three, new Message())
@@ -188,9 +198,11 @@
188198 hypervisor.registerContainer('root', Root)
189199 hypervisor.registerContainer('child', Child)
190200
191201 const root = await hypervisor.createInstance('root')
192- const port = await root.ports.create('root', 'first')
202+ const port = root.ports.create('root')
203+ root.ports.bind(port, 'first')
204+
193205 await root.send(port, new Message())
194206 await root.wait(Infinity)
195207
196208 t.equals(runs, 3, 'the number of run should be 3')
@@ -201,12 +213,16 @@
201213 tape('traps', async t => {
202214 t.plan(1)
203215 class Root extends BaseContainer {
204216 async run (m) {
205- this.kernel.ports.create('root', 'one')
206- this.kernel.ports.create('root', 'two')
207- this.kernel.ports.create('root', 'three')
217+ const one = this.kernel.ports.create('child')
218+ const two = this.kernel.ports.create('child')
219+ const three = this.kernel.ports.create('child')
208220
221+ this.kernel.ports.bind(one, 'one')
222+ this.kernel.ports.bind(two, 'two')
223+ this.kernel.ports.bind(three, 'three')
224+
209225 throw new Error('it is a trap!!!')
210226 }
211227 }
212228
@@ -229,12 +245,13 @@
229245 tape('invalid port referances', async t => {
230246 t.plan(2)
231247 class Root extends BaseContainer {
232248 async run (m) {
233- const ports = this.kernel.ports.create('root', 'three')
249+ const port = this.kernel.ports.create('root')
250+ this.kernel.ports.bind(port, 'three')
234251 this.kernel.ports.delete('three')
235252 try {
236- await this.kernel.send(ports, new Message())
253+ await this.kernel.send(port, new Message())
237254 } catch (e) {
238255 t.pass()
239256 }
240257 }
@@ -262,11 +279,14 @@
262279 class Root extends BaseContainer {
263280 async run (m) {
264281 if (!this.runs) {
265282 this.runs = 1
266- const one = this.kernel.ports.create('first', 'one')
267- const two = this.kernel.ports.create('second', 'two')
283+ const one = this.kernel.ports.create('first')
284+ const two = this.kernel.ports.create('second')
268285
286+ this.kernel.ports.bind(one, 'one')
287+ this.kernel.ports.bind(two, 'two')
288+
269289 await Promise.all([
270290 this.kernel.send(one, new Message()),
271291 this.kernel.send(two, new Message())
272292 ])
@@ -303,28 +323,31 @@
303323 hypervisor.registerContainer('first', First)
304324 hypervisor.registerContainer('second', Second)
305325
306326 const root = await hypervisor.createInstance('root')
307- const port = await root.ports.create('root', 'first')
308- await root.send(port, new Message())
327+ const port = root.ports.create('root')
328+ root.ports.bind(port, 'first')
329+
330+ root.send(port, new Message())
309331 })
310332
311333 tape('message should arrive in the correct order, even if sent out of order', async t => {
312334 t.plan(2)
313335
314336 class Root extends BaseContainer {
315- async run (m) {
337+ run (m) {
316338 if (!this.runs) {
317339 this.runs = 1
318- const one = this.kernel.ports.create('first', 'one')
319- const two = this.kernel.ports.create('second', 'two')
340+ const one = this.kernel.ports.create('first')
341+ const two = this.kernel.ports.create('second')
320342
321- await Promise.all([
343+ this.kernel.ports.bind(one, 'one')
344+ this.kernel.ports.bind(two, 'two')
345+
346+ return Promise.all([
322347 this.kernel.send(one, new Message()),
323348 this.kernel.send(two, new Message())
324349 ])
325-
326- this.kernel.incrementTicks(6)
327350 } else if (this.runs === 1) {
328351 this.runs++
329352 t.equals(m.data, 'second', 'should recive the first message')
330353 } else if (this.runs === 2) {
@@ -333,18 +356,18 @@
333356 }
334357 }
335358
336359 class First extends BaseContainer {
337- async run (m) {
360+ run (m) {
338361 this.kernel.incrementTicks(2)
339- await this.kernel.send(m.fromPort, new Message({data: 'first'}))
362+ return this.kernel.send(m.fromPort, new Message({data: 'first'}))
340363 }
341364 }
342365
343366 class Second extends BaseContainer {
344- async run (m) {
367+ run (m) {
345368 this.kernel.incrementTicks(1)
346- await this.kernel.send(m.fromPort, new Message({data: 'second'}))
369+ this.kernel.send(m.fromPort, new Message({data: 'second'}))
347370 }
348371 }
349372
350373 const hypervisor = new Hypervisor({
@@ -355,10 +378,12 @@
355378 hypervisor.registerContainer('first', First)
356379 hypervisor.registerContainer('second', Second)
357380
358381 const root = await hypervisor.createInstance('root')
359- const port = await root.ports.create('root', 'first')
360- await root.send(port, new Message())
382+ const port = root.ports.create('root')
383+ root.ports.bind(port, 'first')
384+
385+ root.send(port, new Message())
361386 })
362387
363388 tape('message should arrive in the correct order, even in a tie of ticks', async t => {
364389 t.plan(2)
@@ -366,11 +391,14 @@
366391 class Root extends BaseContainer {
367392 async run (m) {
368393 if (!this.runs) {
369394 this.runs = 1
370- const one = this.kernel.ports.create('first', 'one')
371- const two = this.kernel.ports.create('second', 'two')
395+ const one = this.kernel.ports.create('first')
396+ const two = this.kernel.ports.create('second')
372397
398+ this.kernel.ports.bind(one, 'one')
399+ this.kernel.ports.bind(two, 'two')
400+
373401 await Promise.all([
374402 this.kernel.send(one, new Message()),
375403 this.kernel.send(two, new Message())
376404 ])
@@ -385,18 +413,22 @@
385413 }
386414 }
387415
388416 class First extends BaseContainer {
389- async run (m) {
417+ run (m) {
390418 this.kernel.incrementTicks(2)
391- await this.kernel.send(m.fromPort, new Message({data: 'first'}))
419+ return this.kernel.send(m.fromPort, new Message({
420+ data: 'first'
421+ }))
392422 }
393423 }
394424
395425 class Second extends BaseContainer {
396- async run (m) {
426+ run (m) {
397427 this.kernel.incrementTicks(2)
398- await this.kernel.send(m.fromPort, new Message({data: 'second'}))
428+ return this.kernel.send(m.fromPort, new Message({
429+ data: 'second'
430+ }))
399431 }
400432 }
401433
402434 const hypervisor = new Hypervisor({
@@ -407,28 +439,30 @@
407439 hypervisor.registerContainer('first', First)
408440 hypervisor.registerContainer('second', Second)
409441
410442 const root = await hypervisor.createInstance('root')
411- const port = await root.ports.create('root', 'first')
412- await root.send(port, new Message())
443+ const port = await root.ports.create('root')
444+ root.ports.bind(port, 'first')
445+ root.send(port, new Message())
413446 })
414447
415448 tape('message should arrive in the correct order, even in a tie of ticks', async t => {
416449 t.plan(2)
417450
418451 class Root extends BaseContainer {
419- async run (m) {
452+ run (m) {
420453 if (!this.runs) {
421454 this.runs = 1
422- const two = this.kernel.ports.create('second', 'two')
423- const one = this.kernel.ports.create('first', 'one')
455+ const two = this.kernel.ports.create('second')
456+ const one = this.kernel.ports.create('first')
424457
425- await Promise.all([
458+ this.kernel.ports.bind(two, 'two')
459+ this.kernel.ports.bind(one, 'one')
460+
461+ return Promise.all([
426462 this.kernel.send(two, new Message()),
427463 this.kernel.send(one, new Message())
428464 ])
429-
430- this.kernel.incrementTicks(6)
431465 } else if (this.runs === 1) {
432466 this.runs++
433467 t.equals(m.data, 'first', 'should recived the second message')
434468 } else if (this.runs === 2) {
@@ -437,18 +471,22 @@
437471 }
438472 }
439473
440474 class First extends BaseContainer {
441- async run (m) {
475+ run (m) {
442476 this.kernel.incrementTicks(2)
443- await this.kernel.send(m.fromPort, new Message({data: 'first'}))
477+ return this.kernel.send(m.fromPort, new Message({
478+ data: 'first'
479+ }))
444480 }
445481 }
446482
447483 class Second extends BaseContainer {
448- async run (m) {
484+ run (m) {
449485 this.kernel.incrementTicks(2)
450- await this.kernel.send(m.fromPort, new Message({data: 'second'}))
486+ return this.kernel.send(m.fromPort, new Message({
487+ data: 'second'
488+ }))
451489 }
452490 }
453491
454492 const hypervisor = new Hypervisor({
@@ -459,28 +497,32 @@
459497 hypervisor.registerContainer('first', First)
460498 hypervisor.registerContainer('second', Second)
461499
462500 const root = await hypervisor.createInstance('root')
463- const port = await root.ports.create('root', 'first')
464- await root.send(port, new Message())
501+
502+ const port = root.ports.create('root')
503+ root.ports.bind(port, 'first')
504+
505+ root.send(port, new Message())
465506 })
466507
467508 tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
468509 t.plan(2)
469510
470511 class Root extends BaseContainer {
471- async run (m) {
512+ run (m) {
472513 if (!this.runs) {
473514 this.runs = 1
474- const one = this.kernel.ports.create('first', 'one')
475- const two = this.kernel.ports.create('second', 'two')
515+ const one = this.kernel.ports.create('first')
516+ const two = this.kernel.ports.create('second')
476517
477- await Promise.all([
518+ this.kernel.ports.bind(one, 'one')
519+ this.kernel.ports.bind(two, 'two')
520+
521+ return Promise.all([
478522 this.kernel.send(two, new Message()),
479523 this.kernel.send(one, new Message())
480524 ])
481-
482- this.kernel.incrementTicks(6)
483525 } else if (this.runs === 1) {
484526 this.runs++
485527 t.equals(m.data, 'first', 'should recive the first message')
486528 } else if (this.runs === 2) {
@@ -489,11 +531,11 @@
489531 }
490532 }
491533
492534 class First extends BaseContainer {
493- async run (m) {
535+ run (m) {
494536 this.kernel.incrementTicks(2)
495- await this.kernel.send(m.fromPort, new Message({
537+ return this.kernel.send(m.fromPort, new Message({
496538 resources: {
497539 priority: 100
498540 },
499541 data: 'first'
@@ -501,11 +543,13 @@
501543 }
502544 }
503545
504546 class Second extends BaseContainer {
505- async run (m) {
547+ run (m) {
506548 this.kernel.incrementTicks(2)
507- await this.kernel.send(m.fromPort, new Message({data: 'second'}))
549+ return this.kernel.send(m.fromPort, new Message({
550+ data: 'second'
551+ }))
508552 }
509553 }
510554
511555 const hypervisor = new Hypervisor({
@@ -516,28 +560,31 @@
516560 hypervisor.registerContainer('first', First)
517561 hypervisor.registerContainer('second', Second)
518562
519563 const root = await hypervisor.createInstance('root')
520- const port = await root.ports.create('root', 'first')
521- await root.send(port, new Message())
564+ const port = root.ports.create('root')
565+ root.ports.bind(port, 'first')
566+ root.send(port, new Message())
522567 })
523568
524569 tape('message should arrive in the correct order, with a tie in ticks but with differnt proity', async t => {
525570 t.plan(2)
526571
527572 class Root extends BaseContainer {
528- async run (m) {
573+ run (m) {
529574 if (!this.runs) {
530575 this.runs = 1
531- const one = this.kernel.ports.create('first', 'one')
532- const two = this.kernel.ports.create('second', 'two')
533576
534- await Promise.all([
577+ const one = this.kernel.ports.create('first')
578+ const two = this.kernel.ports.create('second')
579+
580+ this.kernel.ports.bind(one, 'one')
581+ this.kernel.ports.bind(two, 'two')
582+
583+ return Promise.all([
535584 this.kernel.send(two, new Message()),
536585 this.kernel.send(one, new Message())
537586 ])
538-
539- this.kernel.incrementTicks(6)
540587 } else if (this.runs === 1) {
541588 this.runs++
542589 t.equals(m.data, 'second', 'should recive the first message')
543590 } else if (this.runs === 2) {
@@ -546,20 +593,20 @@
546593 }
547594 }
548595
549596 class First extends BaseContainer {
550- async run (m) {
597+ run (m) {
551598 this.kernel.incrementTicks(2)
552- await this.kernel.send(m.fromPort, new Message({
599+ return this.kernel.send(m.fromPort, new Message({
553600 data: 'first'
554601 }))
555602 }
556603 }
557604
558605 class Second extends BaseContainer {
559- async run (m) {
606+ run (m) {
560607 this.kernel.incrementTicks(2)
561- await this.kernel.send(m.fromPort, new Message({
608+ return this.kernel.send(m.fromPort, new Message({
562609 resources: {
563610 priority: 100
564611 },
565612 data: 'second'
@@ -575,21 +622,25 @@
575622 hypervisor.registerContainer('first', First)
576623 hypervisor.registerContainer('second', Second)
577624
578625 const root = await hypervisor.createInstance('root')
579- const port = await root.ports.create('root', 'first')
580- await root.send(port, new Message())
626+ const port = root.ports.create('root')
627+ root.ports.bind(port, 'first')
628+ root.send(port, new Message())
581629 })
582630
583631 tape('should order parent messages correctly', async t => {
584632 t.plan(1)
585633 class Middle extends BaseContainer {
586- async run (m) {
634+ run (m) {
587635 if (!this.runs) {
588636 this.runs = 1
589637 this.kernel.incrementTicks(1)
590- const leaf = this.kernel.ports.create('leaf', 'leaf')
591- await this.kernel.send(leaf, new Message())
638+
639+ const leaf = this.kernel.ports.create('leaf')
640+ this.kernel.ports.bind(leaf, 'leaf')
641+
642+ return this.kernel.send(leaf, new Message())
592643 } else {
593644 ++this.runs
594645 if (this.runs === 3) {
595646 t.equals(m.data, 'first')
@@ -598,11 +649,11 @@
598649 }
599650 }
600651
601652 class Leaf extends BaseContainer {
602- async run (m) {
653+ run (m) {
603654 this.kernel.incrementTicks(2)
604- await this.kernel.send(m.fromPort, new Message({
655+ return this.kernel.send(m.fromPort, new Message({
605656 data: 'first'
606657 }))
607658 }
608659 }
@@ -617,10 +668,11 @@
617668
618669 const root = await hypervisor.createInstance('root')
619670 root.incrementTicks(2)
620671
621- const port = await root.ports.create('middle', 'first')
672+ const port = root.ports.create('middle')
673+ root.ports.bind(port, 'first')
622674
623675 await root.send(port, new Message())
624- await root.send(port, new Message())
676+ root.send(port, new Message())
625677 })
626678 })

Built with git-ssb-web