Files: e7eb20431b18e511b25b50b4c4d332767e25bc3d / kernel.js
4784 bytesRaw
1 | const Message = require('primea-message') |
2 | const BN = require('bn.js') |
3 | const PortManager = require('./portManager.js') |
4 | const DeleteMessage = require('./deleteMessage') |
5 | |
6 | module.exports = class Kernel { |
7 | /** |
8 | * the Kernel manages the varous message passing functions and provides |
9 | * an interface for the containers to use |
10 | * @param {Object} opts |
11 | * @param {Object} opts.id - the UUID of the Kernel |
12 | * @param {Object} opts.state - the state of the container |
13 | * @param {Object} opts.hypervisor |
14 | * @param {Object} opts.container - the container constuctor and argments |
15 | */ |
16 | constructor (opts) { |
17 | this.state = opts.state |
18 | this.code = opts.code |
19 | this.hypervisor = opts.hypervisor |
20 | this.id = opts.id |
21 | this.container = new opts.container.Constructor(this, opts.container.args) |
22 | this.timeout = 0 |
23 | |
24 | this.ticks = 0 |
25 | this.containerState = 'idle' |
26 | |
27 | // create the port manager |
28 | this.ports = new PortManager(Object.assign({ |
29 | kernel: this |
30 | }, opts)) |
31 | } |
32 | |
33 | /** |
34 | * adds a message to this containers message queue |
35 | * @param {string} portName |
36 | * @param {object} message |
37 | */ |
38 | queue (portName, message) { |
39 | this.ports.queue(portName, message) |
40 | return this._startMessageLoop() |
41 | } |
42 | |
43 | async initialize (message) { |
44 | await this.run(message, 'initialize') |
45 | return this._startMessageLoop() |
46 | } |
47 | |
48 | // waits for the next message |
49 | async _startMessageLoop () { |
50 | // this ensure we only every have one loop running at a time |
51 | if (this.containerState !== 'running') { |
52 | this.containerState = 'running' |
53 | |
54 | while (1) { |
55 | const message = await this.ports.getNextMessage(this.timeout) |
56 | if (!message) break |
57 | |
58 | // dequqe message |
59 | message.fromPort.messages.shift() |
60 | // if the message we recived had more ticks then we currently have the |
61 | // update it |
62 | if (message._fromTicks > this.ticks) { |
63 | this.ticks = message._fromTicks |
64 | this.hypervisor.scheduler.update(this) |
65 | } |
66 | // run the next message |
67 | await this.run(message) |
68 | } |
69 | |
70 | this.containerState = 'idle' |
71 | this.container.onIdle() |
72 | } |
73 | } |
74 | |
75 | shutdown () { |
76 | this.hypervisor.scheduler.done(this.id) |
77 | } |
78 | |
79 | /** |
80 | * run the kernels code with a given enviroment |
81 | * @param {object} message - the message to run |
82 | * @param {boolean} init - whether or not to run the intialization routine |
83 | * @returns {Promise} |
84 | */ |
85 | async run (message, method = 'run') { |
86 | if (message.constructor === DeleteMessage) { |
87 | this.ports._delete(message.fromName) |
88 | } else { |
89 | const responsePort = message.responsePort |
90 | delete message.responsePort |
91 | |
92 | this.ports.addReceivedPorts(message) |
93 | let result |
94 | try { |
95 | result = await this.container[method](message) |
96 | } catch (e) { |
97 | result = { |
98 | exception: true, |
99 | exceptionError: e |
100 | } |
101 | } |
102 | |
103 | if (responsePort) { |
104 | this.send(responsePort, new Message({ |
105 | data: result |
106 | })) |
107 | } |
108 | } |
109 | |
110 | this.ports.clearUnboundedPorts() |
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 | /** |
144 | * creates a new container. Returning a port to it. |
145 | * @param {String} type |
146 | * @param {*} data - the data to populate the initail state with |
147 | * @returns {Object} |
148 | */ |
149 | createInstance (type, message) { |
150 | let nonce = this.state.nonce |
151 | |
152 | const id = { |
153 | nonce: nonce, |
154 | parent: this.id |
155 | } |
156 | |
157 | // incerment the nonce |
158 | nonce = new BN(nonce) |
159 | nonce.iaddn(1) |
160 | this.state.nonce = nonce.toArray() |
161 | this.ports.removeSentPorts(message) |
162 | |
163 | return this.hypervisor.createInstance(type, message, id) |
164 | } |
165 | |
166 | /** |
167 | * sends a message to a given port |
168 | * @param {Object} portRef - the port |
169 | * @param {Message} message - the message |
170 | */ |
171 | send (port, message) { |
172 | message._hops++ |
173 | // set the port that the message came from |
174 | message._fromTicks = this.ticks |
175 | this.ports.removeSentPorts(message) |
176 | |
177 | // if (this.currentMessage !== message && !message.responsePort) { |
178 | // this.currentMessage._addSubMessage(message) |
179 | // } |
180 | return this.hypervisor.send(port, message) |
181 | } |
182 | } |
183 |
Built with git-ssb-web