git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: aebb51fa47b38ca825a49a00b5a83afca13ba3e3

Files: aebb51fa47b38ca825a49a00b5a83afca13ba3e3 / examples / index.js

3618 bytesRaw
1const IPFS = require('ipfs')
2const Hypervisor = require('../')
3
4// the hypervisor store all of it's state using ipfs.dag api
5// https://github.com/ipfs/interface-ipfs-core/tree/master/API/dag
6const node = new IPFS({
7 start: false
8})
9
10// the Hypervisor starts an manages "containers"
11class ExampleContainer {
12 // the constructor is given an instance of the kernel
13 // https://github.com/primea/js-primea-hypervisor/blob/master/docs/kernel.md
14 constructor (kernel) {
15 this.kernel = kernel
16 }
17
18 // this method runs once when the container is intailly created. It is given
19 // a message with a single port, which is a channel to its parent with the
20 // exception of the root container (the container that is intailial created)
21 initialize (message) {
22 const port = message.ports[0]
23 // the root container doesn't get a port
24 if (port) {
25 this.kernel.ports.bind('parent', port)
26 }
27 }
28
29 // the function is called for each message that a container gets
30 run (message) {
31 if (message.data === 'bindPort') {
32 this.kernel.ports.bind('channel', message.ports[0])
33 }
34 }
35
36 onIdle () {
37 this.kernel.shutdown()
38 }
39}
40
41// wait untill the ipfs node is ready
42node.on('ready', async() => {
43 // create a new hypervisor instance
44 const hypervisor = new Hypervisor(node.dag)
45 hypervisor.registerContainer('example', ExampleContainer)
46
47 // create a root instance of the example container
48 const root = await hypervisor.createInstance('example')
49
50 // create two channels
51 const [portRef1, portRef2] = root.ports.createChannel()
52 const [portRef3, portRef4] = root.ports.createChannel()
53
54 // create two instances of the example container. These containers wiil be
55 // given channels to the parent container
56 root.createInstance('example', root.createMessage({
57 ports: [portRef2]
58 }))
59
60 root.createInstance('example', root.createMessage({
61 ports: [portRef4]
62 }))
63
64 // bind the ports of the channels to the newly created containers. Binding
65 // ports allows the root container tt receieve messages from the containers.
66 // If no other container bound these ports then the corrisponding containers
67 // would be garbage collected
68 root.ports.bind('one', portRef1)
69 root.ports.bind('two', portRef3)
70
71 // create a new channel. Each channel has two corrisponding ports that
72 // containers can communicate over
73 const [chanRef1, chanRef2] = root.ports.createChannel()
74
75 // send the newly created ports to each of the containers. Once both the
76 // recieving containers bind the ports they will be able to communicate
77 // directly with each other over them
78 await root.send(portRef1, root.createMessage({
79 data: 'bindPort',
80 ports: [chanRef1]
81 }))
82
83 await root.send(portRef3, root.createMessage({
84 data: 'bindPort',
85 ports: [chanRef2]
86 }))
87
88 // after the recieving containers bind the ports in the messages the channel
89 // topology will look like this. Where "[]" are the containers, "*" are the
90 // ports that the container have and "(name)" is the port name.
91 //
92 // root container
93 // [ ]
94 // (one) * * (two)
95 // / \
96 // / \
97 // / \
98 // (parent)* * (parent)
99 // [ ]*--------*[ ]
100 // (channel) (channel)
101
102 // create a new state root. The state root is not created untill the
103 // hypervisor has finished all of it's work
104 const stateRoot = await hypervisor.createStateRoot()
105 console.log(stateRoot)
106 console.log('--------full state dump---------')
107 await hypervisor.graph.tree(stateRoot, Infinity)
108 console.log(JSON.stringify(stateRoot, null, 2))
109})
110

Built with git-ssb-web