git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 3146cb3249c2ab000ed44d4712910ff42fd0d5c1

Files: 3146cb3249c2ab000ed44d4712910ff42fd0d5c1 / actor.js

4757 bytesRaw
1const Buffer = require('safe-buffer').Buffer
2const Pipe = require('buffer-pipe')
3const Cap = require('primea-capability')
4const Message = require('primea-message')
5const leb128 = require('leb128').unsigned
6const LockMap = require('lockmap')
7const Inbox = require('./inbox.js')
8
9module.exports = class Actor {
10 /**
11 * the Actor manages the varous message passing functions and provides
12 * an interface for the containers to use
13 * @param {Object} opts
14 * @param {Object} opts.id - the UUID of the Actor
15 * @param {Object} opts.state - the state of the container
16 * @param {Object} opts.hypervisor - the instance of the hypervisor
17 * @param {Object} opts.container - the container constuctor and argments
18 */
19 constructor (opts) {
20 Object.assign(this, opts)
21
22 this.container = new opts.container.Constructor(this, opts.container.args)
23 this.inbox = new Inbox({
24 actor: this,
25 hypervisor: opts.hypervisor
26 })
27
28 this.ticks = 0
29 this.running = false
30 this._sending = new LockMap()
31 }
32
33 /**
34 * Mints a new capabilitly with a given tag
35 * @param {*} tag - a tag which can be used to identify caps
36 * @return {Object}
37 */
38 mintCap (tag = 0, funcIndex = 0) {
39 return new Cap(this.id, tag, funcIndex)
40 }
41
42 /**
43 * adds a message to this actor's message queue
44 * @param {string} portName
45 * @param {object} message
46 */
47 queue (message) {
48 this.inbox.queue(message)
49 this._startMessageLoop()
50 }
51
52 /**
53 * runs the creation routine for the actor
54 * @param {object} message
55 * @returns {Promise}
56 */
57 create (message) {
58 // start loop before running intializtion message so the the container state
59 // will be "running" incase the actor recievse a message will running
60 // creation code
61 this._startMessageLoop()
62 return this.runMessage(message, 'onCreation')
63 }
64
65 // waits for the next message
66 async _startMessageLoop () {
67 // this ensure we only every have one loop running at a time
68 if (!this.running) {
69 this.running = true
70 while (1) {
71 const message = await this.inbox.nextMessage()
72 if (!message) break
73
74 // run the next message
75 await this.runMessage(message)
76 // wait for state ops to finish
77 await this.state.done()
78 }
79
80 this.running = false
81 this.container.onIdle()
82 }
83 }
84
85 serializeMetaData () {
86 return Actor.serializeMetaData(this.type, this.transparent, this.nonce)
87 }
88
89 static serializeMetaData (type, transparent = 0, nonce = 0) {
90 const p = new Pipe()
91 leb128.write(type, p)
92 p.write(Buffer.from([0]))
93 leb128.write(nonce, p)
94 return p.buffer
95 }
96
97 static deserializeMetaData (buffer) {
98 const pipe = new Pipe(buffer)
99 return {
100 type: leb128.read(pipe),
101 nonce: leb128.read(pipe)
102 }
103 }
104
105 /**
106 * Runs the shutdown routine for the actor
107 */
108 shutdown () {
109 this.state.root['/'][3] = this.serializeMetaData()
110 this.hypervisor.scheduler.done(this.id)
111 }
112
113 /**
114 * Runs the startup routine for the actor
115 */
116 startup () {
117 return this.container.onStartup()
118 }
119
120 /**
121 * run the Actor with a given message
122 * @param {object} message - the message to run
123 * @param {String} method - which method to run
124 * @returns {Promise}
125 */
126 async runMessage (message, method = 'onMessage') {
127 let result
128 try {
129 result = await this.container[method](message)
130 } catch (e) {
131 message.emit('execution:error', e)
132 result = {
133 exception: true,
134 exceptionError: e
135 }
136 }
137
138 if (message.responseCap) {
139 this.send(message.responseCap, new Message({
140 data: result
141 }))
142 }
143 }
144
145 /**
146 * updates the number of ticks that the actor has run
147 * @param {Number} count - the number of ticks to add
148 */
149 incrementTicks (count) {
150 this.ticks += count
151 this.hypervisor.scheduler.update(this)
152 }
153
154 /**
155 * creates an actor
156 * @param {Integer} type - the type id for the container
157 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
158 */
159 createActor (type, message) {
160 const id = this._generateNextId()
161 return this.hypervisor.createActor(type, message, id)
162 }
163
164 _generateNextId () {
165 const id = {
166 nonce: this.nonce,
167 parent: this.id
168 }
169
170 this.nonce++
171 return id
172 }
173
174 /**
175 * sends a message to a given port
176 * @param {Object} portRef - the port
177 * @param {Message} message - the message
178 */
179 send (cap, message) {
180 const resolve = this._sending.lock(cap)
181 message._fromTicks = this.ticks
182 message._fromId = this.id
183 message.tag = cap.tag
184
185 return this.hypervisor.send(cap, message).then(() => resolve(cap))
186 }
187}
188

Built with git-ssb-web