git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 0872f0354cc0cdb87c143bd22c75598169e7c206

Files: 0872f0354cc0cdb87c143bd22c75598169e7c206 / kernel.js

4179 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 const portRef = this.hypervisor.getResponsePort(message)
117 this.ports._unboundPorts.add(portRef)
118 return portRef
119 }
120
121 /**
122 * updates the number of ticks that the container has run
123 * @param {Number} count - the number of ticks to add
124 */
125 incrementTicks (count) {
126 this.ticks += count
127 this.hypervisor.scheduler.update(this)
128 }
129
130 /**
131 * creates a new message
132 * @param {*} data
133 */
134 createMessage (opts) {
135 const message = new Message(opts)
136 this.ports.checkSendingPorts(message)
137 return message
138 }
139
140 generateNextId () {
141 const id = {
142 nonce: this.state.nonce,
143 parent: this.id
144 }
145
146 this.state.nonce++
147 return id
148 }
149
150 /**
151 * sends a message to a given port
152 * @param {Object} portRef - the port
153 * @param {Message} message - the message
154 */
155 send (port, message) {
156 message._hops++
157 message._fromTicks = this.ticks
158 message.fromId = this.id
159 this.ports.removeSentPorts(message)
160
161 return this.hypervisor.send(port, message)
162 }
163}
164

Built with git-ssb-web