kernel.jsView |
---|
17 | 17 | this.state = opts.state |
18 | 18 | this.hypervisor = opts.hypervisor |
19 | 19 | this.id = opts.id |
20 | 20 | this.container = new opts.container.Constructor(this, opts.container.args) |
| 21 | + this.timeout = 0 |
21 | 22 | |
22 | 23 | this.ticks = 0 |
23 | 24 | this.containerState = 'idle' |
24 | 25 | |
49 | 50 | if (this.containerState !== 'running') { |
50 | 51 | this.containerState = 'running' |
51 | 52 | |
52 | 53 | while (1) { |
53 | | - const message = await this.ports.getNextMessage() |
| 54 | + const message = await this.ports.getNextMessage(this.timeout) |
54 | 55 | if (!message) break |
55 | 56 | |
56 | 57 | |
57 | 58 | message.fromPort.messages.shift() |
63 | 64 | } |
64 | 65 | |
65 | 66 | await this.run(message) |
66 | 67 | } |
67 | | - |
68 | | - this.hypervisor.scheduler.done(this.id) |
| 68 | + |
| 69 | + this.containerState = 'idle' |
| 70 | + this.container.onIdle() |
69 | 71 | } |
70 | 72 | } |
71 | 73 | |
| 74 | + shutdown () { |
| 75 | + this.hypervisor.scheduler.done(this.id) |
| 76 | + } |
| 77 | + |
72 | 78 | |
73 | 79 | * run the kernels code with a given enviroment |
74 | 80 | * @param {object} message - the message to run |
75 | 81 | * @param {boolean} init - whether or not to run the intialization routine |
76 | 82 | * @returns {Promise} |
77 | 83 | */ |
78 | 84 | async run (message, method = 'run') { |
79 | | - let result |
80 | | - |
81 | | - const responsePort = message.responsePort |
82 | | - delete message.responsePort |
83 | | - |
84 | | - this.ports.addReceivedPorts(message) |
85 | | - |
86 | 85 | if (message.constructor === DeleteMessage) { |
87 | 86 | this.ports._delete(message.fromName) |
88 | 87 | } else { |
| 88 | + const responsePort = message.responsePort |
| 89 | + delete message.responsePort |
| 90 | + |
| 91 | + this.ports.addReceivedPorts(message) |
| 92 | + let result |
89 | 93 | try { |
90 | 94 | result = await this.container[method](message) || {} |
91 | 95 | } catch (e) { |
92 | 96 | result = { |
93 | 97 | exception: true, |
94 | 98 | exceptionError: e |
95 | 99 | } |
96 | 100 | } |
97 | | - } |
98 | 101 | |
99 | | - if (responsePort) { |
100 | | - this.send(responsePort, new Message({ |
101 | | - data: result |
102 | | - })) |
| 102 | + if (responsePort) { |
| 103 | + this.send(responsePort, new Message({ |
| 104 | + data: result |
| 105 | + })) |
| 106 | + } |
103 | 107 | } |
104 | 108 | |
105 | 109 | this.ports.clearUnboundedPorts() |
106 | 110 | } |