Files: e301c1ce5f932e363af5cfff39807eef96443627 / kernel.js
4244 bytesRaw
1 | const Message = require('primea-message') |
2 | const PortManager = require('./portManager.js') |
3 | const DeleteMessage = require('./deleteMessage') |
4 | |
5 | module.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 | result = { |
99 | exception: true, |
100 | exceptionError: e |
101 | } |
102 | } |
103 | |
104 | if (responsePort) { |
105 | this.send(responsePort, new Message({ |
106 | data: result |
107 | })) |
108 | } |
109 | await this.ports.clearUnboundedPorts() |
110 | } |
111 | } |
112 | |
113 | getResponsePort (message) { |
114 | if (message.responsePort) { |
115 | return message.responsePort.destPort |
116 | } else { |
117 | const [portRef1, portRef2] = this.ports.createChannel() |
118 | message.responsePort = portRef2 |
119 | this.ports._unboundPorts.delete(portRef2) |
120 | return portRef1 |
121 | } |
122 | } |
123 | |
124 | /** |
125 | * updates the number of ticks that the container has run |
126 | * @param {Number} count - the number of ticks to add |
127 | */ |
128 | incrementTicks (count) { |
129 | this.ticks += count |
130 | this.hypervisor.scheduler.update(this) |
131 | } |
132 | |
133 | /** |
134 | * creates a new message |
135 | * @param {*} data |
136 | */ |
137 | createMessage (opts) { |
138 | const message = new Message(opts) |
139 | this.ports.checkSendingPorts(message) |
140 | return message |
141 | } |
142 | |
143 | generateNextId () { |
144 | const id = { |
145 | nonce: this.state.nonce, |
146 | parent: this.id |
147 | } |
148 | |
149 | this.state.nonce++ |
150 | return id |
151 | } |
152 | |
153 | /** |
154 | * sends a message to a given port |
155 | * @param {Object} portRef - the port |
156 | * @param {Message} message - the message |
157 | */ |
158 | send (port, message) { |
159 | message._hops++ |
160 | message._fromTicks = this.ticks |
161 | message.fromId = this.id |
162 | this.ports.removeSentPorts(message) |
163 | |
164 | return this.hypervisor.send(port, message) |
165 | } |
166 | } |
167 |
Built with git-ssb-web