Commit 755514bf413e3af2218088969d281e9b6e56e8d7
added methods for actor metadata serailization
wanderer committed on 12/7/2017, 11:58:01 PMParent: 9934d9a6a446f5a6218a27df8b509155c7bc57cc
Files changed
actor.js | changed |
index.js | changed |
actor.js | ||
---|---|---|
@@ -1,5 +1,6 @@ | ||
1 | 1 | const Buffer = require('safe-buffer').Buffer |
2 | +const Pipe = require('buffer-pipe') | |
2 | 3 | const Cap = require('primea-capability') |
3 | 4 | const Message = require('primea-message') |
4 | 5 | const leb128 = require('leb128').unsigned |
5 | 6 | const LockMap = require('lockmap') |
@@ -15,12 +16,10 @@ | ||
15 | 16 | * @param {Object} opts.hypervisor - the instance of the hypervisor |
16 | 17 | * @param {Object} opts.container - the container constuctor and argments |
17 | 18 | */ |
18 | 19 | constructor (opts) { |
19 | - this.state = opts.state | |
20 | - this.nonce = leb128.decode(opts.state.root['/'][3].subarray(2)) | |
21 | - this.hypervisor = opts.hypervisor | |
22 | - this.id = opts.id | |
20 | + Object.assign(this, opts) | |
21 | + | |
23 | 22 | this.container = new opts.container.Constructor(this, opts.container.args) |
24 | 23 | this.inbox = new Inbox({ |
25 | 24 | actor: this, |
26 | 25 | hypervisor: opts.hypervisor |
@@ -88,15 +87,34 @@ | ||
88 | 87 | this.container.onIdle() |
89 | 88 | } |
90 | 89 | } |
91 | 90 | |
91 | + serializeMetaData () { | |
92 | + return Actor.serializeMetaData(this.type, this.transparent, this.nonce) | |
93 | + } | |
94 | + | |
95 | + static serializeMetaData (type, transparent = 0, nonce = 0) { | |
96 | + const p = new Pipe() | |
97 | + leb128.write(type, p) | |
98 | + p.write(Buffer.from([transparent])) | |
99 | + leb128.write(nonce, p) | |
100 | + return p.buffer | |
101 | + } | |
102 | + | |
103 | + static deserializeMetaData (buffer) { | |
104 | + const pipe = new Pipe(buffer) | |
105 | + return { | |
106 | + type: leb128.read(pipe), | |
107 | + nonce: leb128.read(pipe), | |
108 | + transparent: pipe.read(1)[0] | |
109 | + } | |
110 | + } | |
111 | + | |
92 | 112 | /** |
93 | 113 | * Runs the shutdown routine for the actor |
94 | 114 | */ |
95 | 115 | shutdown () { |
96 | - // save the nonce | |
97 | - let state = this.state.root['/'][3].subarray(0, 2) | |
98 | - this.state.root['/'][3] = Buffer.concat([state, leb128.encode(this.nonce)]) | |
116 | + this.state.root['/'][3] = this.serializeMetaData() | |
99 | 117 | this.hypervisor.scheduler.done(this.id) |
100 | 118 | } |
101 | 119 | |
102 | 120 | /** |
index.js | ||
---|---|---|
@@ -1,7 +1,6 @@ | ||
1 | 1 | const Actor = require('./actor.js') |
2 | 2 | const Scheduler = require('./scheduler.js') |
3 | -const leb128 = require('leb128').unsigned | |
4 | 3 | |
5 | 4 | module.exports = class Hypervisor { |
6 | 5 | /** |
7 | 6 | * The Hypervisor manages the container instances by instantiating them and |
@@ -29,17 +28,20 @@ | ||
29 | 28 | |
30 | 29 | // loads an instance of a container from the state |
31 | 30 | async _loadActor (id) { |
32 | 31 | const state = await this.tree.getSubTree(id) |
33 | - const type = leb128.decode(state.root['/'][3]) | |
32 | + const {type, nonce, transparent} = Actor.deserializeMetaData(state.root['/'][3]) | |
34 | 33 | const container = this._containerTypes[type] |
35 | 34 | |
36 | 35 | // create a new actor instance |
37 | 36 | const actor = new Actor({ |
38 | 37 | hypervisor: this, |
39 | 38 | state, |
40 | 39 | container, |
41 | - id | |
40 | + id, | |
41 | + nonce, | |
42 | + transparent, | |
43 | + type | |
42 | 44 | }) |
43 | 45 | |
44 | 46 | // save the newly created instance |
45 | 47 | this.scheduler.update(actor) |
@@ -52,17 +54,15 @@ | ||
52 | 54 | * @returns {Promise} |
53 | 55 | */ |
54 | 56 | async getActor (id) { |
55 | 57 | let actor = this.scheduler.getInstance(id) |
56 | - if (actor) { | |
57 | - return actor | |
58 | - } else { | |
58 | + if (!actor) { | |
59 | 59 | const resolve = this.scheduler.lock(id) |
60 | - const actor = await this._loadActor(id) | |
60 | + actor = await this._loadActor(id) | |
61 | 61 | await actor.startup() |
62 | 62 | resolve(actor) |
63 | - return actor | |
64 | 63 | } |
64 | + return actor | |
65 | 65 | } |
66 | 66 | |
67 | 67 | /** |
68 | 68 | * creates an instance of an Actor |
@@ -72,12 +72,12 @@ | ||
72 | 72 | */ |
73 | 73 | async createActor (type, message, id = {nonce: this.nonce++, parent: null}) { |
74 | 74 | const encoded = encodedID(id) |
75 | 75 | const idHash = await this._getHashFromObj(encoded) |
76 | - const state = Buffer.concat([leb128.encode(type), Buffer.from([0, 0])]) | |
76 | + const metaData = Actor.serializeMetaData(type) | |
77 | 77 | |
78 | 78 | // save the container in the state |
79 | - this.tree.set(idHash, state) | |
79 | + this.tree.set(idHash, metaData) | |
80 | 80 | |
81 | 81 | // create the container instance |
82 | 82 | const instance = await this._loadActor(idHash) |
83 | 83 | |
@@ -109,10 +109,10 @@ | ||
109 | 109 | * @param {Integer} typeId - the container's type identification ID |
110 | 110 | */ |
111 | 111 | registerContainer (Constructor, args, typeId = Constructor.typeId) { |
112 | 112 | this._containerTypes[typeId] = { |
113 | - Constructor: Constructor, | |
114 | - args: args | |
113 | + Constructor, | |
114 | + args | |
115 | 115 | } |
116 | 116 | } |
117 | 117 | } |
118 | 118 |
Built with git-ssb-web