Files: dcd4e757706b7946eff933674387a29bc6270077 / actor.js
2799 bytesRaw
1 | const errors = require('./errors.json') |
2 | const nope = () => {} |
3 | |
4 | module.exports = class Actor { |
5 | /** |
6 | * the Actor manages the varous message passing functions and provides |
7 | * an interface for the containers to use |
8 | * @param {Object} opts |
9 | * @param {ID} opts.id - the UUID of the Actor |
10 | * @param {Object} opts.state - the state of the container |
11 | * @param {Object} opts.storage - the actor's persistant storage |
12 | * @param {Object} opts.hypervisor - the instance of the hypervisor |
13 | * @param {Number} opts.nonce |
14 | * @param {Number} opts.type - the container type |
15 | * @param {Function} opts.container - the container constuctor and argments |
16 | */ |
17 | constructor (opts) { |
18 | Object.assign(this, opts) |
19 | |
20 | this.ticks = 0 |
21 | this.running = false |
22 | this.container = new this.Container(this) |
23 | if (!this.hypervisor.meter) { |
24 | this.incrementTicks = nope |
25 | } |
26 | } |
27 | |
28 | /** |
29 | * Runs the shutdown routine for the actor |
30 | */ |
31 | shutdown () { |
32 | // saves the nonce and storage to the state |
33 | this.state.value[1] = this.nonce |
34 | this.state.node[2] = {'/': this.storage} |
35 | } |
36 | |
37 | /** |
38 | * Runs the startup routine for the actor |
39 | */ |
40 | startup () { |
41 | return this.container.onStartup() |
42 | } |
43 | |
44 | /** |
45 | * run the Actor with a given message |
46 | * @param {object} message - the message to run |
47 | * @param {String} method - which method to run |
48 | * @returns {Promise} |
49 | */ |
50 | async runMessage (message) { |
51 | if (message._fromTicks > this.ticks) { |
52 | this.ticks = message._fromTicks |
53 | } |
54 | try { |
55 | this.currentMessage = message |
56 | await this.container.onMessage(message) |
57 | } catch (e) { |
58 | message.emit('execution:error', e) |
59 | } |
60 | message.emit('done', this) |
61 | } |
62 | |
63 | /** |
64 | * updates the number of ticks that the actor has run |
65 | * @param {Number} count - the number of ticks to add |
66 | */ |
67 | incrementTicks (count) { |
68 | this.currentMessage.funcRef.gas -= count |
69 | if (this.currentMessage.funcRef.gas < 0) { |
70 | throw new Error(errors.OUT_OF_GAS) |
71 | } |
72 | this.ticks += count |
73 | } |
74 | |
75 | /** |
76 | * creates an actor |
77 | * @param {Integer} type - the type id for the container |
78 | * @param {Object} message - an intial [message](https://github.com/primea/js-primea-message) to send newly created actor |
79 | */ |
80 | createActor (type, code) { |
81 | const id = this._generateNextId() |
82 | return this.hypervisor.createActor(type, code, id) |
83 | } |
84 | |
85 | _generateNextId () { |
86 | const id = { |
87 | nonce: this.nonce, |
88 | parent: this.id |
89 | } |
90 | |
91 | this.nonce++ |
92 | return id |
93 | } |
94 | |
95 | /** |
96 | * sends a message to a given port |
97 | * @param {Object} portRef - the port |
98 | * @param {Message} message - the message |
99 | */ |
100 | send (message) { |
101 | message._fromTicks = this.ticks |
102 | message._fromId = this.id |
103 | |
104 | this.incrementTicks(message.funcRef.gas) |
105 | this.hypervisor.scheduler.queue([message]) |
106 | } |
107 | } |
108 |
Built with git-ssb-web