git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: a0c04bdafa14ebe4eecafb19828b204564ec9a90

Files: a0c04bdafa14ebe4eecafb19828b204564ec9a90 / kernel.js

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

Built with git-ssb-web