git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 74ccd6e33a63430ec22a6fcbfd0fd40f10c349c5

added example

wanderer committed on 6/29/2017, 7:18:49 AM
Parent: 317d79e49cb56dd81cb9c94072cd24ad6a825757

Files changed

docs/hypervisor.mdchanged
docs/kernel.mdchanged
docs/portManager.mdchanged
kernel.jschanged
examples/index.jsadded
docs/hypervisor.mdView
@@ -8,13 +8,12 @@
88 - [getInstance](#getinstance)
99 - [createInstance](#createinstance)
1010 - [createStateRoot](#createstateroot)
1111 - [registerContainer](#registercontainer)
12-- [getHashFromObj](#gethashfromobj)
1312
1413 ## constructor
1514
16-[index.js:16-22](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/index.js#L16-L22 "Source code on GitHub")
15+[index.js:16-22](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/index.js#L16-L22 "Source code on GitHub")
1716
1817 The Hypervisor manages the container instances by instantiating them and
1918 destorying them when possible. It also facilitates localating Containers
2019
@@ -24,9 +23,9 @@
2423 - `state` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the starting state (optional, default `{}`)
2524
2625 ## addNodeToCheck
2726
28-[index.js:28-30](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/index.js#L28-L30 "Source code on GitHub")
27+[index.js:28-30](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/index.js#L28-L30 "Source code on GitHub")
2928
3029 add a potaintail node in the state graph to check for garbage collection
3130
3231 **Parameters**
@@ -34,9 +33,9 @@
3433 - `id` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
3534
3635 ## getDestPort
3736
38-[index.js:37-43](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/index.js#L37-L43 "Source code on GitHub")
37+[index.js:37-43](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/index.js#L37-L43 "Source code on GitHub")
3938
4039 given a port, this finds the corridsponeding endpoint port of the channel
4140
4241 **Parameters**
@@ -46,9 +45,9 @@
4645 Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
4746
4847 ## getInstance
4948
50-[index.js:68-78](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/index.js#L68-L78 "Source code on GitHub")
49+[index.js:73-83](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/index.js#L73-L83 "Source code on GitHub")
5150
5251 gets an existsing container instances
5352
5453 **Parameters**
@@ -58,9 +57,9 @@
5857 Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
5958
6059 ## createInstance
6160
62-[index.js:90-114](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/index.js#L90-L114 "Source code on GitHub")
61+[index.js:95-119](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/index.js#L95-L119 "Source code on GitHub")
6362
6463 creates an new container instances and save it in the state
6564
6665 **Parameters**
@@ -75,9 +74,9 @@
7574 Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
7675
7776 ## createStateRoot
7877
79-[index.js:122-129](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/index.js#L122-L129 "Source code on GitHub")
78+[index.js:127-134](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/index.js#L127-L134 "Source code on GitHub")
8079
8180 creates a state root starting from a given container and a given number of
8281 ticks
8382
@@ -88,25 +87,13 @@
8887 Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
8988
9089 ## registerContainer
9190
92-[index.js:137-142](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/index.js#L137-L142 "Source code on GitHub")
91+[index.js:142-147](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/index.js#L142-L147 "Source code on GitHub")
9392
9493 regirsters a container with the hypervisor
9594
9695 **Parameters**
9796
9897 - `type` **[String](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)** the name of the type
9998 - `Constructor` **Class** a Class for instantiating the container
10099 - `args` **any** any args that the contructor takes
101-
102-## getHashFromObj
103-
104-[index.js:149-151](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/index.js#L149-L151 "Source code on GitHub")
105-
106-get a hash from a POJO
107-
108-**Parameters**
109-
110-- `obj` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
111-
112-Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
docs/kernel.mdView
@@ -10,24 +10,24 @@
1010 - [send](#send)
1111
1212 ## constructor
1313
14-[kernel.js:15-28](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/kernel.js#L15-L28 "Source code on GitHub")
14+[kernel.js:15-28](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/kernel.js#L15-L28 "Source code on GitHub")
1515
16-the ExoInterface manages the varous message passing functions and provides
16+the Kernel manages the varous message passing functions and provides
1717 an interface for the containers to use
1818
1919 **Parameters**
2020
2121 - `opts` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
22- - `opts.id` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
23- - `opts.state` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
22+ - `opts.id` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the UUID of the Kernel
23+ - `opts.state` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the state of the container
2424 - `opts.hypervisor` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
25- - `opts.Container` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
25+ - `opts.container` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the container constuctor and argments
2626
2727 ## queue
2828
29-[kernel.js:35-48](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/kernel.js#L35-L48 "Source code on GitHub")
29+[kernel.js:35-48](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/kernel.js#L35-L48 "Source code on GitHub")
3030
3131 adds a message to this containers message queue
3232
3333 **Parameters**
@@ -36,24 +36,22 @@
3636 - `message` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
3737
3838 ## run
3939
40-[kernel.js:78-98](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/kernel.js#L78-L98 "Source code on GitHub")
40+[kernel.js:78-98](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/kernel.js#L78-L98 "Source code on GitHub")
4141
4242 run the kernels code with a given enviroment
43-The Kernel Stores all of its state in the Environment. The Interface is used
44-to by the VM to retrive infromation from the Environment.
4543
4644 **Parameters**
4745
48-- `message`
49-- `init` (optional, default `false`)
46+- `message` **[object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the message to run
47+- `init` **[boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)** whether or not to run the intialization routine (optional, default `false`)
5048
5149 Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
5250
5351 ## incrementTicks
5452
55-[kernel.js:104-107](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/kernel.js#L104-L107 "Source code on GitHub")
53+[kernel.js:104-107](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/kernel.js#L104-L107 "Source code on GitHub")
5654
5755 updates the number of ticks that the container has run
5856
5957 **Parameters**
@@ -61,9 +59,9 @@
6159 - `count` **[Number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** the number of ticks to add
6260
6361 ## createMessage
6462
65-[kernel.js:113-121](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/kernel.js#L113-L121 "Source code on GitHub")
63+[kernel.js:113-121](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/kernel.js#L113-L121 "Source code on GitHub")
6664
6765 creates a new message
6866
6967 **Parameters**
@@ -72,9 +70,9 @@
7270 - `data` **any**
7371
7472 ## send
7573
76-[kernel.js:128-145](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/kernel.js#L128-L145 "Source code on GitHub")
74+[kernel.js:128-145](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/kernel.js#L128-L145 "Source code on GitHub")
7775
7876 sends a message to a given port
7977
8078 **Parameters**
docs/portManager.mdView
@@ -15,9 +15,9 @@
1515 - [getNextMessage](#getnextmessage)
1616
1717 ## constructor
1818
19-[portManager.js:33-44](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L33-L44 "Source code on GitHub")
19+[portManager.js:33-44](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L33-L44 "Source code on GitHub")
2020
2121 The port manager manages the the ports. This inculdes creation, deletion
2222 fetching and waiting on ports
2323
@@ -29,9 +29,9 @@
2929 - `opts.exoInterface` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
3030
3131 ## bind
3232
33-[portManager.js:51-73](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L51-L73 "Source code on GitHub")
33+[portManager.js:51-73](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L51-L73 "Source code on GitHub")
3434
3535 binds a port to a name
3636
3737 **Parameters**
@@ -40,9 +40,9 @@
4040 - `port` **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)** the port to bind
4141
4242 ## unbind
4343
44-[portManager.js:80-92](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L80-L92 "Source code on GitHub")
44+[portManager.js:80-92](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L80-L92 "Source code on GitHub")
4545
4646 unbinds a port given its name
4747
4848 **Parameters**
@@ -52,9 +52,9 @@
5252 Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
5353
5454 ## delete
5555
56-[portManager.js:98-102](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L98-L102 "Source code on GitHub")
56+[portManager.js:98-102](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L98-L102 "Source code on GitHub")
5757
5858 delete an port given the name it is bound to
5959
6060 **Parameters**
@@ -62,15 +62,15 @@
6262 - `name` **[string](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String)**
6363
6464 ## clearUnboundedPorts
6565
66-[portManager.js:112-120](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L112-L120 "Source code on GitHub")
66+[portManager.js:112-120](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L112-L120 "Source code on GitHub")
6767
6868 clears any unbounded ports referances
6969
7070 ## isBound
7171
72-[portManager.js:127-129](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L127-L129 "Source code on GitHub")
72+[portManager.js:127-129](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L127-L129 "Source code on GitHub")
7373
7474 check if a port object is still valid
7575
7676 **Parameters**
@@ -80,9 +80,9 @@
8080 Returns **[Boolean](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean)**
8181
8282 ## queue
8383
84-[portManager.js:135-157](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L135-L157 "Source code on GitHub")
84+[portManager.js:135-157](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L135-L157 "Source code on GitHub")
8585
8686 queues a message on a port
8787
8888 **Parameters**
@@ -91,9 +91,9 @@
9191 - `message` **Message**
9292
9393 ## get
9494
95-[portManager.js:164-166](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L164-L166 "Source code on GitHub")
95+[portManager.js:164-166](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L164-L166 "Source code on GitHub")
9696
9797 gets a port given it's name
9898
9999 **Parameters**
@@ -103,9 +103,9 @@
103103 Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
104104
105105 ## create
106106
107-[portManager.js:174-193](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L174-L193 "Source code on GitHub")
107+[portManager.js:174-193](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L174-L193 "Source code on GitHub")
108108
109109 creates a new container. Returning a port to it.
110110
111111 **Parameters**
@@ -116,17 +116,17 @@
116116 Returns **[Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object)**
117117
118118 ## createChannel
119119
120-[portManager.js:199-213](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L199-L213 "Source code on GitHub")
120+[portManager.js:199-213](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L199-L213 "Source code on GitHub")
121121
122122 creates a channel returns the created ports in an Array
123123
124124 Returns **[array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array)**
125125
126126 ## getNextMessage
127127
128-[portManager.js:229-257](https://github.com/primea/js-primea-hypervisor/blob/b507f49239a73767abbee979b4e637a6ca469764/portManager.js#L229-L257 "Source code on GitHub")
128+[portManager.js:229-257](https://github.com/primea/js-primea-hypervisor/blob/317d79e49cb56dd81cb9c94072cd24ad6a825757/portManager.js#L229-L257 "Source code on GitHub")
129129
130130 Waits for the the next message if any
131131
132132 Returns **[Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise)**
kernel.jsView
@@ -3,15 +3,15 @@
33 const DeleteMessage = require('./deleteMessage')
44
55 module.exports = class Kernel {
66 /**
7- * the ExoInterface manages the varous message passing functions and provides
7+ * the Kernel manages the varous message passing functions and provides
88 * an interface for the containers to use
99 * @param {Object} opts
10- * @param {Object} opts.id
11- * @param {Object} opts.state
10+ * @param {Object} opts.id - the UUID of the Kernel
11+ * @param {Object} opts.state - the state of the container
1212 * @param {Object} opts.hypervisor
13- * @param {Object} opts.Container
13+ * @param {Object} opts.container - the container constuctor and argments
1414 */
1515 constructor (opts) {
1616 this.state = opts.state
1717 this.hypervisor = opts.hypervisor
@@ -70,10 +70,10 @@
7070 }
7171
7272 /**
7373 * run the kernels code with a given enviroment
74- * The Kernel Stores all of its state in the Environment. The Interface is used
75- * to by the VM to retrive infromation from the Environment.
74+ * @param {object} message - the message to run
75+ * @param {boolean} init - whether or not to run the intialization routine
7676 * @returns {Promise}
7777 */
7878 async run (message, init = false) {
7979 let result
examples/index.jsView
@@ -1,0 +1,82 @@
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" each container is
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+ initailize (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+ console.log('message recived!', message.data)
32+ if (message.data === 'bindPort') {
33+ this.kernel.ports.bind('channel', message.ports[0])
34+ }
35+ }
36+}
37+
38+// wait untill the ipfs node is ready
39+node.on('ready', async() => {
40+ // create a new hypervisor instance
41+ const hypervisor = new Hypervisor(node.dag)
42+ hypervisor.registerContainer('example', ExampleContainer)
43+
44+ // create a root instance of the example container
45+ const root = await hypervisor.createInstance('example')
46+
47+ // create two instances of the example container. These containers wiil be
48+ // given channels to the parent container
49+ const portRef1 = root.ports.create('example')
50+ const portRef2 = root.ports.create('example')
51+
52+ // bind the ports of the newly created containers. Binding ports allows the
53+ // root container tot receieve messages from the containers. If no other
54+ // container bound these ports then the corrisponding containers would be
55+ // garbage collected
56+ root.ports.bind('one', portRef1)
57+ root.ports.bind('two', portRef2)
58+
59+ // create a new channel. Each channel has two corrisponding ports that
60+ // containers can communicate over
61+ const [chanRef1, chanRef2] = root.ports.createChannel()
62+ // send the newly created ports to each of the containers. Once both the
63+ // recieving containers bind the ports they will be able to communicate
64+ // directly with each other over them
65+ await root.send(portRef1, root.createMessage({
66+ data: 'bindPort',
67+ ports: [chanRef1]
68+ }))
69+
70+ await root.send(portRef2, root.createMessage({
71+ data: 'bindPort',
72+ ports: [chanRef2]
73+ }))
74+
75+ // create a new state root. The state root is not created untill the
76+ // hypervisor has finished all of it's work
77+ const stateRoot = await hypervisor.createStateRoot()
78+ console.log(stateRoot)
79+ console.log('--------full state dump---------')
80+ await hypervisor.graph.tree(stateRoot, Infinity)
81+ console.log(JSON.stringify(stateRoot, null, 2))
82+})

Built with git-ssb-web