Files: a2acc6de9878b25ba7ae028fcf33ca086d042fae / kernel.js
4268 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 | |
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 | console.log(e) |
100 | result = { |
101 | exception: true, |
102 | exceptionError: e |
103 | } |
104 | } |
105 | |
106 | if (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