git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 679caf4c89ce01c38751eb18177edee3a845d23e

Files: 679caf4c89ce01c38751eb18177edee3a845d23e / examples / index.js

3528 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
37// wait untill the ipfs node is ready
38node.on('ready', async() => {
39 // create a new hypervisor instance
40 const hypervisor = new Hypervisor(node.dag)
41 hypervisor.registerContainer('example', ExampleContainer)
42
43 // create a root instance of the example container
44 const root = await hypervisor.createInstance('example')
45
46 const [portRef1, portRef2] = root.ports.createChannel()
47 const [portRef3, portRef4] = root.ports.createChannel()
48
49 // create two instances of the example container. These containers wiil be
50 // given channels to the parent container
51 root.ports.create('example', root.createMessage({
52 ports: [portRef2]
53 }))
54
55 root.ports.create('example', root.createMessage({
56 ports: [portRef4]
57 }))
58
59 // bind the ports of the newly created containers. Binding ports allows the
60 // root container tot receieve messages from the containers. If no other
61 // container bound these ports then the corrisponding containers would be
62 // garbage collected
63 root.ports.bind('one', portRef1)
64 root.ports.bind('two', portRef3)
65
66 // create a new channel. Each channel has two corrisponding ports that
67 // containers can communicate over
68 const [chanRef1, chanRef2] = root.ports.createChannel()
69
70 // send the newly created ports to each of the containers. Once both the
71 // recieving containers bind the ports they will be able to communicate
72 // directly with each other over them
73 await root.send(portRef1, root.createMessage({
74 data: 'bindPort',
75 ports: [chanRef1]
76 }))
77
78 await root.send(portRef3, root.createMessage({
79 data: 'bindPort',
80 ports: [chanRef2]
81 }))
82
83 // after the recieving containers bind the ports in the messages the channel
84 // topology will look like this. Where "[]" are the containers, "*" are the
85 // ports that the container have and "(name)" is the port name.
86 //
87 // root container
88 // [ ]
89 // (one) * * (two)
90 // / \
91 // / \
92 // / \
93 // (parent)* * (parent)
94 // [ ]*--------*[ ]
95 // (channel) (channel)
96
97 // create a new state root. The state root is not created untill the
98 // hypervisor has finished all of it's work
99 const stateRoot = await hypervisor.createStateRoot()
100 console.log(stateRoot)
101 console.log('--------full state dump---------')
102 await hypervisor.graph.tree(stateRoot, Infinity)
103 console.log(JSON.stringify(stateRoot, null, 2))
104})
105

Built with git-ssb-web