Files: ded42bab9d3ec663f4308b94ae68a926196845cb / kernel.js
4821 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 | |
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 (portName, message) { |
38 | this.ports.queue(portName, 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 | |
53 | while (1) { |
54 | const message = await this.ports.getNextMessage() |
55 | if (!message) break |
56 | |
57 | // dequqe message |
58 | message.fromPort.messages.shift() |
59 | // if the message we recived had more ticks then we currently have then |
60 | // update it |
61 | if (message._fromTicks > this.ticks) { |
62 | this.ticks = message._fromTicks |
63 | this.hypervisor.scheduler.update(this) |
64 | } |
65 | // run the next message |
66 | await this.message(message) |
67 | } |
68 | |
69 | this.containerState = 'idle' |
70 | this.container.onIdle() |
71 | } |
72 | } |
73 | |
74 | shutdown () { |
75 | this.hypervisor.scheduler.done(this.id) |
76 | } |
77 | |
78 | startup () { |
79 | return this.container.onStartup() |
80 | } |
81 | |
82 | /** |
83 | * run the kernels code with a given enviroment |
84 | * @param {object} message - the message to run |
85 | * @param {boolean} init - whether or not to run the intialization routine |
86 | * @returns {Promise} |
87 | */ |
88 | async message (message, method = 'onMessage') { |
89 | if (message.constructor === DeleteMessage) { |
90 | this.ports._delete(message.fromName) |
91 | } else { |
92 | const responsePort = message.responsePort |
93 | delete message.responsePort |
94 | |
95 | this.ports.addReceivedPorts(message) |
96 | let result |
97 | try { |
98 | result = await this.container[method](message) |
99 | } catch (e) { |
100 | console.log(e) |
101 | result = { |
102 | exception: true, |
103 | exceptionError: e |
104 | } |
105 | } |
106 | |
107 | if (responsePort) { |
108 | this.send(responsePort, new Message({ |
109 | data: result |
110 | })) |
111 | } |
112 | await this.ports.clearUnboundedPorts() |
113 | } |
114 | } |
115 | |
116 | getResponsePort (message) { |
117 | if (message.responsePort) { |
118 | return message.responsePort.destPort |
119 | } else { |
120 | const [portRef1, portRef2] = this.ports.createChannel() |
121 | message.responsePort = portRef2 |
122 | this.ports._unboundPorts.delete(portRef2) |
123 | return portRef1 |
124 | } |
125 | } |
126 | |
127 | /** |
128 | * updates the number of ticks that the container has run |
129 | * @param {Number} count - the number of ticks to add |
130 | */ |
131 | incrementTicks (count) { |
132 | this.ticks += count |
133 | this.hypervisor.scheduler.update(this) |
134 | } |
135 | |
136 | /** |
137 | * creates a new message |
138 | * @param {*} data |
139 | */ |
140 | createMessage (opts) { |
141 | const message = new Message(opts) |
142 | this.ports.checkSendingPorts(message) |
143 | return message |
144 | } |
145 | |
146 | /** |
147 | * creates a new container. Returning a port to it. |
148 | * @param {String} type |
149 | * @param {*} data - the data to populate the initail state with |
150 | * @returns {Object} |
151 | */ |
152 | createInstance (type, message) { |
153 | let nonce = this.state.nonce |
154 | |
155 | const id = { |
156 | nonce: nonce, |
157 | parent: this.id |
158 | } |
159 | |
160 | // incerment the nonce |
161 | nonce = new BN(nonce) |
162 | nonce.iaddn(1) |
163 | this.state.nonce = nonce.toArray() |
164 | |
165 | if (message) { |
166 | this.ports.removeSentPorts(message) |
167 | } |
168 | |
169 | return this.hypervisor.createInstance(type, message, id) |
170 | } |
171 | |
172 | /** |
173 | * sends a message to a given port |
174 | * @param {Object} portRef - the port |
175 | * @param {Message} message - the message |
176 | */ |
177 | send (port, message) { |
178 | message._hops++ |
179 | message._fromTicks = this.ticks |
180 | this.ports.removeSentPorts(message) |
181 | |
182 | // if (this.currentMessage !== message && !message.responsePort) { |
183 | // this.currentMessage._addSubMessage(message) |
184 | // } |
185 | return this.hypervisor.send(port, message) |
186 | } |
187 | } |
188 |
Built with git-ssb-web