Files: 9a207a1ce69e564a67b6833876ce76b4f3258efa / actor.js
4814 bytesRaw
1 | const Buffer = require('safe-buffer').Buffer |
2 | const Pipe = require('buffer-pipe') |
3 | const Cap = require('primea-capability') |
4 | const Message = require('primea-message') |
5 | const leb128 = require('leb128').unsigned |
6 | const LockMap = require('lockmap') |
7 | const Inbox = require('./inbox.js') |
8 | |
9 | module.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 | } |
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 | const type = leb128.read(pipe) |
100 | pipe.read(1) |
101 | const nonce = leb128.read(pipe) |
102 | return { |
103 | nonce, |
104 | type |
105 | } |
106 | } |
107 | |
108 | /** |
109 | * Runs the shutdown routine for the actor |
110 | */ |
111 | shutdown () { |
112 | this.state.root['/'][3] = this.serializeMetaData() |
113 | this.hypervisor.scheduler.done(this.id) |
114 | } |
115 | |
116 | /** |
117 | * Runs the startup routine for the actor |
118 | */ |
119 | startup () { |
120 | return this.container.onStartup() |
121 | } |
122 | |
123 | /** |
124 | * run the Actor with a given message |
125 | * @param {object} message - the message to run |
126 | * @param {String} method - which method to run |
127 | * @returns {Promise} |
128 | */ |
129 | async runMessage (message, method = 'onMessage') { |
130 | let result |
131 | try { |
132 | result = await this.container[method](message) |
133 | } catch (e) { |
134 | message.emit('execution:error', e) |
135 | result = { |
136 | exception: true, |
137 | exceptionError: e |
138 | } |
139 | } |
140 | |
141 | if (message.responseCap) { |
142 | this.send(message.responseCap, new Message({ |
143 | data: result |
144 | })) |
145 | } |
146 | } |
147 | |
148 | /** |
149 | * updates the number of ticks that the actor has run |
150 | * @param {Number} count - the number of ticks to add |
151 | */ |
152 | incrementTicks (count) { |
153 | this.ticks += count |
154 | this.hypervisor.scheduler.update(this) |
155 | } |
156 | |
157 | /** |
158 | * creates an actor |
159 | * @param {Integer} type - the type id for the container |
160 | * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor |
161 | */ |
162 | createActor (type, message) { |
163 | const id = this._generateNextId() |
164 | return this.hypervisor.createActor(type, message, id) |
165 | } |
166 | |
167 | _generateNextId () { |
168 | const id = { |
169 | nonce: this.nonce, |
170 | parent: this.id |
171 | } |
172 | |
173 | this.nonce++ |
174 | return id |
175 | } |
176 | |
177 | /** |
178 | * sends a message to a given port |
179 | * @param {Object} portRef - the port |
180 | * @param {Message} message - the message |
181 | */ |
182 | send (cap, message) { |
183 | const resolve = this._sending.lock(cap) |
184 | message._fromTicks = this.ticks |
185 | message._fromId = this.id |
186 | message.tag = cap.tag |
187 | |
188 | return this.hypervisor.send(cap, message).then(() => resolve(cap)) |
189 | } |
190 | } |
191 |
Built with git-ssb-web