git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 0b60f6ffa1bef52d1491a87e1d7aa604092d0af2

Files: 0b60f6ffa1bef52d1491a87e1d7aa604092d0af2 / actor.js

3713 bytesRaw
1const Message = require('primea-message')
2const CapsManager = require('./capsManager.js')
3const Inbox = require('./inbox.js')
4
5module.exports = class Kernel {
6 /**
7 * the Kernel manages the varous message passing functions and provides
8 * an interface for the containers to use
9 * @param {Object} opts
10 * @param {Object} opts.id - the UUID of the Kernel
11 * @param {Object} opts.state - the state of the container
12 * @param {Object} opts.hypervisor
13 * @param {Object} opts.container - the container constuctor and argments
14 */
15 constructor (opts) {
16 this.state = opts.state
17 this.code = opts.code
18 this.treeNode = opts.treeNode
19 this.hypervisor = opts.hypervisor
20 this.id = opts.id
21 this.container = new opts.container.Constructor(this, opts.container.args)
22 this.inbox = new Inbox(Object.assign({
23 actor: this
24 }, opts))
25
26 this.ticks = 0
27 this.containerState = 'idle'
28
29 // create the port manager
30 this.caps = new CapsManager(opts)
31 }
32
33 mintCap (tag = 0) {
34 return {
35 destId: this.id,
36 tag: tag
37 }
38 }
39
40 /**
41 * adds a message to this containers 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 async create (message) {
51 await this.message(message, 'onCreation')
52 this._startMessageLoop()
53 }
54
55 // waits for the next message
56 async _startMessageLoop () {
57 // this ensure we only every have one loop running at a time
58 if (this.containerState !== 'running') {
59 this.hypervisor.scheduler.update(this)
60 this.containerState = 'running'
61 while (1) {
62 const message = await this.inbox.getNextMessage()
63 if (!message) break
64
65 // if the message we recived had more ticks then we currently have then
66 // update it
67 if (message._fromTicks > this.ticks) {
68 this.ticks = message._fromTicks
69 this.hypervisor.scheduler.update(this)
70 }
71 // run the next message
72 await this.message(message)
73 }
74
75 this.containerState = 'idle'
76 this.container.onIdle()
77 }
78 }
79
80 shutdown () {
81 this.hypervisor.scheduler.done(this.id)
82 }
83
84 startup () {
85 return this.container.onStartup()
86 }
87
88 /**
89 * run the kernels code with a given enviroment
90 * @param {object} message - the message to run
91 * @param {boolean} init - whether or not to run the intialization routine
92 * @returns {Promise}
93 */
94 async message (message, method = 'onMessage') {
95 const responseCap = message.responseCap
96 delete message.responseCap
97
98 let result
99 try {
100 result = await this.container[method](message)
101 } catch (e) {
102 result = {
103 exception: true,
104 exceptionError: e
105 }
106 }
107
108 if (responseCap) {
109 this.send(responseCap, new Message({
110 data: result
111 }))
112 }
113 this.caps.clist.clear()
114 }
115
116 /**
117 * updates the number of ticks that the container has run
118 * @param {Number} count - the number of ticks to add
119 */
120 incrementTicks (count) {
121 this.ticks += count
122 this.hypervisor.scheduler.update(this)
123 }
124
125 createInstance (type, message) {
126 const id = this.generateNextId()
127 return this.hypervisor.createInstance(type, message, id)
128 }
129
130 generateNextId () {
131 const id = {
132 nonce: this.state.nonce,
133 parent: this.id
134 }
135
136 this.state.nonce++
137 return id
138 }
139
140 /**
141 * sends a message to a given port
142 * @param {Object} portRef - the port
143 * @param {Message} message - the message
144 */
145 send (cap, message) {
146 message._fromTicks = this.ticks
147 message.tag = cap.tag
148
149 return this.hypervisor.send(cap, message)
150 }
151}
152

Built with git-ssb-web