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