Files: c369709837d3b46f11f96b69f996371d77256e62 / kernel.js
4764 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.hypervisor = opts.hypervisor |
19 | this.id = opts.id |
20 | this.container = new opts.container.Constructor(this, opts.container.args) |
21 | this.timeout = 0 |
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 | return this._startMessageLoop() |
40 | } |
41 | |
42 | async initialize (message) { |
43 | await this.run(message, 'initialize') |
44 | return 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(this.timeout) |
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 the |
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.run(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 | /** |
79 | * run the kernels code with a given enviroment |
80 | * @param {object} message - the message to run |
81 | * @param {boolean} init - whether or not to run the intialization routine |
82 | * @returns {Promise} |
83 | */ |
84 | async run (message, method = 'run') { |
85 | if (message.constructor === DeleteMessage) { |
86 | this.ports._delete(message.fromName) |
87 | } else { |
88 | const responsePort = message.responsePort |
89 | delete message.responsePort |
90 | |
91 | this.ports.addReceivedPorts(message) |
92 | let result |
93 | try { |
94 | result = await this.container[method](message) || {} |
95 | } catch (e) { |
96 | result = { |
97 | exception: true, |
98 | exceptionError: e |
99 | } |
100 | } |
101 | |
102 | if (responsePort) { |
103 | this.send(responsePort, new Message({ |
104 | data: result |
105 | })) |
106 | } |
107 | } |
108 | |
109 | this.ports.clearUnboundedPorts() |
110 | } |
111 | |
112 | getResponsePort (message) { |
113 | if (message.responsePort) { |
114 | return message.responsePort.destPort |
115 | } else { |
116 | const [portRef1, portRef2] = this.ports.createChannel() |
117 | message.responsePort = portRef2 |
118 | this.ports._unboundPorts.delete(portRef2) |
119 | return portRef1 |
120 | } |
121 | } |
122 | |
123 | /** |
124 | * updates the number of ticks that the container has run |
125 | * @param {Number} count - the number of ticks to add |
126 | */ |
127 | incrementTicks (count) { |
128 | this.ticks += count |
129 | this.hypervisor.scheduler.update(this) |
130 | } |
131 | |
132 | /** |
133 | * creates a new message |
134 | * @param {*} data |
135 | */ |
136 | createMessage (opts) { |
137 | const message = new Message(opts) |
138 | this.ports.checkSendingPorts(message) |
139 | return message |
140 | } |
141 | |
142 | /** |
143 | * creates a new container. Returning a port to it. |
144 | * @param {String} type |
145 | * @param {*} data - the data to populate the initail state with |
146 | * @returns {Object} |
147 | */ |
148 | createInstance (type, message) { |
149 | let nonce = this.state.nonce |
150 | |
151 | const id = { |
152 | nonce: nonce, |
153 | parent: this.id |
154 | } |
155 | |
156 | // incerment the nonce |
157 | nonce = new BN(nonce) |
158 | nonce.iaddn(1) |
159 | this.state.nonce = nonce.toArray() |
160 | this.ports.removeSentPorts(message) |
161 | |
162 | return this.hypervisor.createInstance(type, message, id) |
163 | } |
164 | |
165 | /** |
166 | * sends a message to a given port |
167 | * @param {Object} portRef - the port |
168 | * @param {Message} message - the message |
169 | */ |
170 | send (port, message) { |
171 | message._hops++ |
172 | // set the port that the message came from |
173 | message._fromTicks = this.ticks |
174 | this.ports.removeSentPorts(message) |
175 | |
176 | // if (this.currentMessage !== message && !message.responsePort) { |
177 | // this.currentMessage._addSubMessage(message) |
178 | // } |
179 | return this.hypervisor.send(port, message) |
180 | } |
181 | } |
182 |
Built with git-ssb-web