git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 9d0e76ab27a7c7ca239e1ef71f84447d37f4acc4

Files: 9d0e76ab27a7c7ca239e1ef71f84447d37f4acc4 / examples / index.js

3565 bytesRaw
1const IPFS = require('ipfs')
2const Hypervisor = require('../')
3const AbstractContainer = require('primea-abstract-container')
4
5// the hypervisor store all of it's state using ipfs.dag api
6// https://github.com/ipfs/interface-ipfs-core/tree/master/API/dag
7const node = new IPFS({
8 start: false
9})
10
11// the Hypervisor starts an manages "containers"
12class ExampleContainer extends AbstractContainer {
13 // this method runs once when the container is intailly created. It is given
14 // a message with a single port, which is a channel to its parent with the
15 // exception of the root container (the container that is intailial created)
16 async initialize (message) {
17 const port = message.ports[0]
18 // the root container doesn't get a port
19 if (port) {
20 this.kernel.ports.bind('parent', port)
21 } else {
22 super.intialize(message)
23 }
24 }
25
26 // the function is called for each message that a container gets
27 async run (message) {
28 if (message.data === 'bindPort') {
29 this.kernel.ports.bind('channel', message.ports[0])
30 } else {
31 super.run(message)
32 }
33 }
34}
35
36// wait untill the ipfs node is ready
37node.on('ready', async() => {
38 // create a new hypervisor instance
39 const hypervisor = new Hypervisor(node.dag)
40 hypervisor.registerContainer('example', ExampleContainer)
41
42 // create a root instance of the example container
43 const root = await hypervisor.createInstance('example')
44
45 // create two channels
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.createInstance('example', root.createMessage({
52 ports: [portRef2]
53 }))
54
55 root.createInstance('example', root.createMessage({
56 ports: [portRef4]
57 }))
58
59 // bind the ports of the channels to the newly created containers. Binding
60 // ports allows the root container tt receieve messages from the containers.
61 // If no other container bound these ports then the corrisponding containers
62 // would be 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