Commit 455f5db59297ab334a58c1589e8743cebdab3817
add caps manager
wanderer committed on 11/21/2017, 7:49:30 PMParent: fe85db02d3b6f1a6ef9490d75086d13614896ba4
Files changed
README.md | changed |
index.js | changed |
scheduler.js | changed |
capsManager.js | added |
README.md | ||
---|---|---|
@@ -19,5 +19,7 @@ | ||
19 | 19 | # API |
20 | 20 | [./docs](./docs/index.md) |
21 | 21 | |
22 | 22 | # LICENSE |
23 | -[MPL-2.0](https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2)) | |
23 | +[MPL-2.0][LICENSE] | |
24 | + | |
25 | +[LICENSE]: https://tldrlegal.com/license/mozilla-public-license-2.0-(mpl-2) |
index.js | ||
---|---|---|
@@ -4,18 +4,23 @@ | ||
4 | 4 | module.exports = class Hypervisor { |
5 | 5 | /** |
6 | 6 | * The Hypervisor manages the container instances by instantiating them and |
7 | 7 | * destorying them when possible. It also facilitates localating Containers |
8 | - * @param {Graph} dag an instance of [ipfs.dag](https://github.com/ipfs/interface-ipfs-core/tree/master/API/dag#dag-api) | |
9 | - * @param {object} state - the starting state | |
8 | + * @param {Tree} tree - a [radix tree](https://github.com/dfinity/js-dfinity-radix-tree) to store the state | |
10 | 9 | */ |
11 | 10 | constructor (tree) { |
12 | 11 | this.tree = tree |
13 | 12 | this.scheduler = new Scheduler() |
14 | 13 | this._containerTypes = {} |
15 | 14 | this.nonce = 0 |
16 | 15 | } |
17 | 16 | |
17 | + /** | |
18 | + * sends a message | |
19 | + * @param {Object} cap - the capabilitly used to send the message | |
20 | + * @param {Object} message - the [message](https://github.com/primea/js-primea-message) to send | |
21 | + * @returns {Promise} a promise that resolves once the receiving container is loaded | |
22 | + */ | |
18 | 23 | async send (cap, message) { |
19 | 24 | const id = cap.destId |
20 | 25 | const instance = await this.getInstance(id) |
21 | 26 | instance.queue(message) |
@@ -58,8 +63,14 @@ | ||
58 | 63 | return instance |
59 | 64 | } |
60 | 65 | } |
61 | 66 | |
67 | + /** | |
68 | + * creates an instance of a container | |
69 | + * @param {Integer} type - the type id for the container | |
70 | + * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created instance | |
71 | + * @param {Object} id - the id for the instance | |
72 | + */ | |
62 | 73 | async createInstance (type, message, id = {nonce: this.nonce, parent: null}) { |
63 | 74 | const encoded = encodedID(id) |
64 | 75 | this.nonce++ |
65 | 76 | const idHash = await this._getHashFromObj(encoded) |
@@ -104,9 +115,9 @@ | ||
104 | 115 | /** |
105 | 116 | * regirsters a container with the hypervisor |
106 | 117 | * @param {Class} Constructor - a Class for instantiating the container |
107 | 118 | * @param {*} args - any args that the contructor takes |
108 | - * @param {interger} typeId - the container's type identification ID | |
119 | + * @param {Interger} typeId - the container's type identification ID | |
109 | 120 | */ |
110 | 121 | registerContainer (Constructor, args, typeId = Constructor.typeId) { |
111 | 122 | this._containerTypes[typeId] = { |
112 | 123 | Constructor: Constructor, |
scheduler.js | ||
---|---|---|
@@ -54,9 +54,9 @@ | ||
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
57 | 57 | * deletes an instance from the scheduler |
58 | - * @param {string} id - the containers id | |
58 | + * @param {String} id - the containers id | |
59 | 59 | */ |
60 | 60 | done (id) { |
61 | 61 | this._running.delete(id) |
62 | 62 | this.instances.delete(id) |
capsManager.js | ||
---|---|---|
@@ -1,0 +1,42 @@ | ||
1 | +module.exports = class CapsManager { | |
2 | + /** | |
3 | + * The port manager manages the the ports. This inculdes creation, deletion | |
4 | + * fetching and waiting on ports | |
5 | + * @param {Object} opts | |
6 | + * @param {Object} opts.state | |
7 | + * @param {Object} opts.hypervisor | |
8 | + * @param {Object} opts.exoInterface | |
9 | + */ | |
10 | + constructor (caps) { | |
11 | + this._storedCaps = caps | |
12 | + this.clist = new Set() | |
13 | + } | |
14 | + | |
15 | + /** | |
16 | + * binds a port to a name | |
17 | + * @param {Object} port - the port to bind | |
18 | + * @param {String} name - the name of the port | |
19 | + */ | |
20 | + store (name, cap) { | |
21 | + // save the port instance | |
22 | + this._storedCaps[name] = cap | |
23 | + } | |
24 | + | |
25 | + /** | |
26 | + * gets a port given it's name | |
27 | + * @param {String} name | |
28 | + * @return {Object} | |
29 | + */ | |
30 | + get (name) { | |
31 | + const cap = this._storedCaps[name] | |
32 | + return cap | |
33 | + } | |
34 | + | |
35 | + /** | |
36 | + * delete an port given the name it is bound to | |
37 | + * @param {string} name | |
38 | + */ | |
39 | + delete (name) { | |
40 | + delete this._storedCaps[name] | |
41 | + } | |
42 | +} |
Built with git-ssb-web