git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: e47dbb585f72b65914431ec10f554e43226dc121

Files: e47dbb585f72b65914431ec10f554e43226dc121 / actor.js

4869 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(0, true)
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 await Promise.all([...this._sending.values()])
79 }
80
81 this.running = false
82 this.container.onIdle()
83 }
84 }
85
86 serializeMetaData () {
87 return Actor.serializeMetaData(this.type, this.transparent, this.nonce)
88 }
89
90 static serializeMetaData (type, transparent = 0, nonce = 0) {
91 const p = new Pipe()
92 leb128.write(type, p)
93 p.write(Buffer.from([0]))
94 leb128.write(nonce, p)
95 return p.buffer
96 }
97
98 static deserializeMetaData (buffer) {
99 const pipe = new Pipe(buffer)
100 const type = leb128.read(pipe)
101 pipe.read(1)
102 const nonce = leb128.read(pipe)
103 return {
104 nonce,
105 type
106 }
107 }
108
109 /**
110 * Runs the shutdown routine for the actor
111 */
112 shutdown () {
113 this.state.root['/'][3] = this.serializeMetaData()
114 this.hypervisor.scheduler.done(this.id)
115 }
116
117 /**
118 * Runs the startup routine for the actor
119 */
120 startup () {
121 return this.container.onStartup()
122 }
123
124 /**
125 * run the Actor with a given message
126 * @param {object} message - the message to run
127 * @param {String} method - which method to run
128 * @returns {Promise}
129 */
130 async runMessage (message, method = 'onMessage') {
131 let result
132 try {
133 result = await this.container[method](message)
134 } catch (e) {
135 message.emit('execution:error', e)
136 result = {
137 exception: true,
138 exceptionError: e
139 }
140 }
141
142 if (message.responseCap) {
143 this.send(message.responseCap, new Message({
144 data: result
145 }))
146 }
147 }
148
149 /**
150 * updates the number of ticks that the actor has run
151 * @param {Number} count - the number of ticks to add
152 */
153 incrementTicks (count) {
154 this.ticks += count
155 this.hypervisor.scheduler.update(this)
156 }
157
158 /**
159 * creates an actor
160 * @param {Integer} type - the type id for the container
161 * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor
162 */
163 createActor (type, message) {
164 const id = this._generateNextId()
165 return this.hypervisor.createActor(type, message, id)
166 }
167
168 _generateNextId () {
169 const id = {
170 nonce: this.nonce,
171 parent: this.id
172 }
173
174 this.nonce++
175 return id
176 }
177
178 /**
179 * sends a message to a given port
180 * @param {Object} portRef - the port
181 * @param {Message} message - the message
182 */
183 send (cap, message) {
184 const resolve = this._sending.lock(cap)
185 message._fromTicks = this.ticks
186 message._fromId = this.id
187 message.tag = cap.tag
188
189 return this.hypervisor.send(cap, message).then(() => resolve(cap))
190 }
191}
192

Built with git-ssb-web