git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: fe17252ec249e039c0e509ba0d745ca0da354881

Files: fe17252ec249e039c0e509ba0d745ca0da354881 / kernel.js

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

Built with git-ssb-web