git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 783d84ab7ec65979b1dad49a2e84dd41de2a555d

Files: 783d84ab7ec65979b1dad49a2e84dd41de2a555d / actor.js

4444 bytesRaw
1const Pipe = require('buffer-pipe')
2const Cap = require('primea-capability')
3const Message = require('primea-message')
4const leb128 = require('leb128').unsigned
5const Inbox = require('./inbox.js')
6
7module.exports = class Actor {
8 /**
9 * the Actor manages the varous message passing functions and provides
10 * an interface for the containers to use
11 * @param {Object} opts
12 * @param {Object} opts.id - the UUID of the Actor
13 * @param {Object} opts.state - the state of the container
14 * @param {Object} opts.hypervisor - the instance of the hypervisor
15 * @param {Object} opts.container - the container constuctor and argments
16 */
17 constructor (opts) {
18 Object.assign(this, opts)
19
20 this.container = new opts.container.Constructor(this, opts.container.args)
21 this.inbox = new Inbox({
22 actor: this,
23 hypervisor: opts.hypervisor
24 })
25
26 this.ticks = 0
27 this.running = false
28 }
29
30 /**
31 * Mints a new capabilitly with a given tag
32 * @param {*} tag - a tag which can be used to identify caps
33 * @return {Object}
34 */
35 mintCap (tag = 0, funcIndex = 0) {
36 return new Cap(this.id, tag, funcIndex)
37 }
38
39 /**
40 * adds a message to this actor's message queue
41 * @param {string} portName
42 * @param {object} message
43 */
44 queue (message) {
45 this.inbox.queue(message)
46
47 if (!this.running) {
48 this.running = true
49 this._startMessageLoop()
50 }
51 }
52
53 /**
54 * runs the creation routine for the actor
55 * @param {object} message
56 * @returns {Promise}
57 */
58 create (message) {
59 this.running = true
60 this.inbox.currentMessage = message
61 return this.runMessage(message, 'onCreation').then(() => {
62 this._startMessageLoop()
63 })
64 }
65
66 // waits for the next message
67 async _startMessageLoop () {
68 // this ensure we only every have one loop running at a time
69 while (1) {
70 const message = await this.inbox.nextMessage(0, true)
71 if (!message) break
72
73 // run the next message
74 await this.runMessage(message)
75 }
76 this.running = false
77 // wait for state ops to finish
78 await this.state.done()
79 if (!this.running) {
80 this.container.onIdle()
81 }
82 }
83
84 serializeMetaData () {
85 return Actor.serializeMetaData(this.type, this.nonce)
86 }
87
88 static serializeMetaData (type, nonce = 0) {
89 const p = new Pipe()
90 leb128.write(type, p)
91 leb128.write(nonce, p)
92 return p.buffer
93 }
94
95 static deserializeMetaData (buffer) {
96 const pipe = new Pipe(buffer)
97 const type = leb128.read(pipe)
98 const nonce = leb128.read(pipe)
99 return {
100 nonce,
101 type
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 message._fromTicks = this.ticks
181 message._fromId = this.id
182
183 return this.hypervisor.send(cap, message)
184 }
185}
186

Built with git-ssb-web