git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: d4cfbd48b828917f70d5680efb0a6567637efcdb

Files: d4cfbd48b828917f70d5680efb0a6567637efcdb / kernel.js

4826 bytesRaw
1const Message = require('primea-message')
2const BN = require('bn.js')
3const PortManager = require('./portManager.js')
4const DeleteMessage = require('./deleteMessage')
5
6module.exports = class Kernel {
7 /**
8 * the Kernel manages the varous message passing functions and provides
9 * an interface for the containers to use
10 * @param {Object} opts
11 * @param {Object} opts.id - the UUID of the Kernel
12 * @param {Object} opts.state - the state of the container
13 * @param {Object} opts.hypervisor
14 * @param {Object} opts.container - the container constuctor and argments
15 */
16 constructor (opts) {
17 this.state = opts.state
18 this.code = opts.code
19 this.hypervisor = opts.hypervisor
20 this.id = opts.id
21 this.container = new opts.container.Constructor(this, opts.container.args)
22 this.timeout = 0
23
24 this.ticks = 0
25 this.containerState = 'idle'
26
27 // create the port manager
28 this.ports = new PortManager(Object.assign({
29 kernel: this
30 }, opts))
31 }
32
33 /**
34 * adds a message to this containers message queue
35 * @param {string} portName
36 * @param {object} message
37 */
38 queue (portName, message) {
39 this.ports.queue(portName, message)
40 this._startMessageLoop()
41 }
42
43 async create (message) {
44 await this.message(message, 'onCreation')
45 this._startMessageLoop()
46 }
47
48 // waits for the next message
49 async _startMessageLoop () {
50 // this ensure we only every have one loop running at a time
51 if (this.containerState !== 'running') {
52 this.containerState = 'running'
53
54 while (1) {
55 const message = await this.ports.getNextMessage(this.timeout)
56 if (!message) break
57
58 // dequqe message
59 message.fromPort.messages.shift()
60 // if the message we recived had more ticks then we currently have then
61 // update it
62 if (message._fromTicks > this.ticks) {
63 this.ticks = message._fromTicks
64 this.hypervisor.scheduler.update(this)
65 }
66 // run the next message
67 await this.message(message)
68 }
69
70 this.containerState = 'idle'
71 this.container.onIdle()
72 }
73 }
74
75 shutdown () {
76 this.hypervisor.scheduler.done(this.id)
77 }
78
79 startup () {
80 return this.container.onStartup()
81 }
82
83 /**
84 * run the kernels code with a given enviroment
85 * @param {object} message - the message to run
86 * @param {boolean} init - whether or not to run the intialization routine
87 * @returns {Promise}
88 */
89 async message (message, method = 'onMessage') {
90 if (message.constructor === DeleteMessage) {
91 this.ports._delete(message.fromName)
92 } else {
93 const responsePort = message.responsePort
94 delete message.responsePort
95
96 this.ports.addReceivedPorts(message)
97 let result
98 try {
99 result = await this.container[method](message)
100 } catch (e) {
101 console.log(e)
102 result = {
103 exception: true,
104 exceptionError: e
105 }
106 }
107
108 if (responsePort) {
109 this.send(responsePort, new Message({
110 data: result
111 }))
112 }
113 await this.ports.clearUnboundedPorts()
114 }
115 }
116
117 getResponsePort (message) {
118 if (message.responsePort) {
119 return message.responsePort.destPort
120 } else {
121 const [portRef1, portRef2] = this.ports.createChannel()
122 message.responsePort = portRef2
123 this.ports._unboundPorts.delete(portRef2)
124 return portRef1
125 }
126 }
127
128 /**
129 * updates the number of ticks that the container has run
130 * @param {Number} count - the number of ticks to add
131 */
132 incrementTicks (count) {
133 this.ticks += count
134 this.hypervisor.scheduler.update(this)
135 }
136
137 /**
138 * creates a new message
139 * @param {*} data
140 */
141 createMessage (opts) {
142 const message = new Message(opts)
143 this.ports.checkSendingPorts(message)
144 return message
145 }
146
147 /**
148 * creates a new container. Returning a port to it.
149 * @param {String} type
150 * @param {*} data - the data to populate the initail state with
151 * @returns {Object}
152 */
153 createInstance (type, message) {
154 let nonce = this.state.nonce
155
156 const id = {
157 nonce: nonce,
158 parent: this.id
159 }
160
161 // incerment the nonce
162 nonce = new BN(nonce)
163 nonce.iaddn(1)
164 this.state.nonce = nonce.toArray()
165 this.ports.removeSentPorts(message)
166
167 return this.hypervisor.createInstance(type, message, id)
168 }
169
170 /**
171 * sends a message to a given port
172 * @param {Object} portRef - the port
173 * @param {Message} message - the message
174 */
175 send (port, message) {
176 message._hops++
177 message._fromTicks = this.ticks
178 this.ports.removeSentPorts(message)
179
180 // if (this.currentMessage !== message && !message.responsePort) {
181 // this.currentMessage._addSubMessage(message)
182 // }
183 return this.hypervisor.send(port, message)
184 }
185}
186

Built with git-ssb-web