git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 755514bf413e3af2218088969d281e9b6e56e8d7

added methods for actor metadata serailization

wanderer committed on 12/7/2017, 11:58:01 PM
Parent: 9934d9a6a446f5a6218a27df8b509155c7bc57cc

Files changed

actor.jschanged
index.jschanged
actor.jsView
@@ -1,5 +1,6 @@
11 const Buffer = require('safe-buffer').Buffer
2+const Pipe = require('buffer-pipe')
23 const Cap = require('primea-capability')
34 const Message = require('primea-message')
45 const leb128 = require('leb128').unsigned
56 const LockMap = require('lockmap')
@@ -15,12 +16,10 @@
1516 * @param {Object} opts.hypervisor - the instance of the hypervisor
1617 * @param {Object} opts.container - the container constuctor and argments
1718 */
1819 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+
2322 this.container = new opts.container.Constructor(this, opts.container.args)
2423 this.inbox = new Inbox({
2524 actor: this,
2625 hypervisor: opts.hypervisor
@@ -88,15 +87,34 @@
8887 this.container.onIdle()
8988 }
9089 }
9190
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+
92112 /**
93113 * Runs the shutdown routine for the actor
94114 */
95115 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()
99117 this.hypervisor.scheduler.done(this.id)
100118 }
101119
102120 /**
index.jsView
@@ -1,7 +1,6 @@
11 const Actor = require('./actor.js')
22 const Scheduler = require('./scheduler.js')
3-const leb128 = require('leb128').unsigned
43
54 module.exports = class Hypervisor {
65 /**
76 * The Hypervisor manages the container instances by instantiating them and
@@ -29,17 +28,20 @@
2928
3029 // loads an instance of a container from the state
3130 async _loadActor (id) {
3231 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])
3433 const container = this._containerTypes[type]
3534
3635 // create a new actor instance
3736 const actor = new Actor({
3837 hypervisor: this,
3938 state,
4039 container,
41- id
40+ id,
41+ nonce,
42+ transparent,
43+ type
4244 })
4345
4446 // save the newly created instance
4547 this.scheduler.update(actor)
@@ -52,17 +54,15 @@
5254 * @returns {Promise}
5355 */
5456 async getActor (id) {
5557 let actor = this.scheduler.getInstance(id)
56- if (actor) {
57- return actor
58- } else {
58+ if (!actor) {
5959 const resolve = this.scheduler.lock(id)
60- const actor = await this._loadActor(id)
60+ actor = await this._loadActor(id)
6161 await actor.startup()
6262 resolve(actor)
63- return actor
6463 }
64+ return actor
6565 }
6666
6767 /**
6868 * creates an instance of an Actor
@@ -72,12 +72,12 @@
7272 */
7373 async createActor (type, message, id = {nonce: this.nonce++, parent: null}) {
7474 const encoded = encodedID(id)
7575 const idHash = await this._getHashFromObj(encoded)
76- const state = Buffer.concat([leb128.encode(type), Buffer.from([0, 0])])
76+ const metaData = Actor.serializeMetaData(type)
7777
7878 // save the container in the state
79- this.tree.set(idHash, state)
79+ this.tree.set(idHash, metaData)
8080
8181 // create the container instance
8282 const instance = await this._loadActor(idHash)
8383
@@ -109,10 +109,10 @@
109109 * @param {Integer} typeId - the container's type identification ID
110110 */
111111 registerContainer (Constructor, args, typeId = Constructor.typeId) {
112112 this._containerTypes[typeId] = {
113- Constructor: Constructor,
114- args: args
113+ Constructor,
114+ args
115115 }
116116 }
117117 }
118118

Built with git-ssb-web