Files: bc308a00381563261f0f7ecf1d231d19e2dfbc52 / actor.js
5066 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() |
72 | if (!message) break |
73 | |
74 | // if the message we recived had more ticks then we currently have then |
75 | // update it |
76 | if (message._fromTicks > this.ticks) { |
77 | this.ticks = message._fromTicks |
78 | this.hypervisor.scheduler.update(this) |
79 | } |
80 | // run the next message |
81 | await this.runMessage(message) |
82 | // wait for state ops to finish |
83 | await this.state.done() |
84 | } |
85 | |
86 | this.running = false |
87 | this.container.onIdle() |
88 | } |
89 | } |
90 | |
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([0])) |
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 | } |
109 | } |
110 | |
111 | /** |
112 | * Runs the shutdown routine for the actor |
113 | */ |
114 | shutdown () { |
115 | this.state.root['/'][3] = this.serializeMetaData() |
116 | this.hypervisor.scheduler.done(this.id) |
117 | } |
118 | |
119 | /** |
120 | * Runs the startup routine for the actor |
121 | */ |
122 | startup () { |
123 | return this.container.onStartup() |
124 | } |
125 | |
126 | /** |
127 | * run the Actor with a given message |
128 | * @param {object} message - the message to run |
129 | * @param {String} method - which method to run |
130 | * @returns {Promise} |
131 | */ |
132 | async runMessage (message, method = 'onMessage') { |
133 | const responseCap = message.responseCap |
134 | delete message.responseCap |
135 | |
136 | let result |
137 | try { |
138 | result = await this.container[method](message) |
139 | } catch (e) { |
140 | message.emit('execution:error', e) |
141 | result = { |
142 | exception: true, |
143 | exceptionError: e |
144 | } |
145 | } |
146 | |
147 | if (responseCap) { |
148 | this.send(responseCap, new Message({ |
149 | data: result |
150 | })) |
151 | } |
152 | } |
153 | |
154 | /** |
155 | * updates the number of ticks that the actor has run |
156 | * @param {Number} count - the number of ticks to add |
157 | */ |
158 | incrementTicks (count) { |
159 | this.ticks += count |
160 | this.hypervisor.scheduler.update(this) |
161 | } |
162 | |
163 | /** |
164 | * creates an actor |
165 | * @param {Integer} type - the type id for the container |
166 | * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor |
167 | */ |
168 | createActor (type, message) { |
169 | const id = this._generateNextId() |
170 | return this.hypervisor.createActor(type, message, id) |
171 | } |
172 | |
173 | _generateNextId () { |
174 | const id = { |
175 | nonce: this.nonce, |
176 | parent: this.id |
177 | } |
178 | |
179 | this.nonce++ |
180 | return id |
181 | } |
182 | |
183 | /** |
184 | * sends a message to a given port |
185 | * @param {Object} portRef - the port |
186 | * @param {Message} message - the message |
187 | */ |
188 | send (cap, message) { |
189 | const resolve = this._sending.lock(cap) |
190 | message._fromTicks = this.ticks |
191 | message._fromId = this.id |
192 | message.tag = cap.tag |
193 | |
194 | return this.hypervisor.send(cap, message).then(() => resolve(cap)) |
195 | } |
196 | } |
197 |
Built with git-ssb-web