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