index.jsView |
---|
1 | 1 | const Graph = require('ipld-graph-builder') |
2 | | -const multibase = require('multibase') |
3 | 2 | const Kernel = require('./kernel.js') |
4 | 3 | |
5 | 4 | module.exports = class Hypervisor { |
6 | 5 | constructor (opts) { |
8 | 7 | this._vmInstances = new Map() |
9 | 8 | this._VMs = {} |
10 | 9 | } |
11 | 10 | |
12 | | - async getInstance (port, createIfNotFound, parentPort) { |
13 | | - const id = await this.generateID(port) |
14 | | - let kernel = this._vmInstances.get(id) |
15 | | - if (!kernel && createIfNotFound) { |
16 | | - |
17 | | - await this.graph.tree(port.id, 1) |
18 | | - |
| 11 | + async getInstance (port, parentPort) { |
| 12 | + let kernel = this._vmInstances.get(port) |
| 13 | + if (!kernel) { |
19 | 14 | kernel = await this.createInstance(port.type, port.link, port, parentPort) |
20 | 15 | kernel.on('idle', () => { |
21 | | - this._vmInstances.delete(id) |
| 16 | + this._vmInstances.delete(port) |
22 | 17 | }) |
23 | 18 | } |
24 | 19 | return kernel |
25 | 20 | } |
26 | 21 | |
27 | 22 | |
28 | 23 | |
29 | 24 | async wait (port, threshold, fromPort) { |
30 | | - let kernel = await this.getInstance(port, false) |
| 25 | + let kernel = this._vmInstances.get(port) |
31 | 26 | if (kernel) { |
32 | 27 | return kernel.wait(threshold, fromPort) |
33 | 28 | } else { |
34 | 29 | return threshold |
52 | 47 | state: state, |
53 | 48 | VM: VM |
54 | 49 | }) |
55 | 50 | |
56 | | - const id = await this.generateID(entryPort) |
57 | 51 | |
58 | | - this._vmInstances.set(id, kernel) |
| 52 | + this._vmInstances.set(entryPort, kernel) |
59 | 53 | await kernel.start() |
60 | 54 | return kernel |
61 | 55 | } |
62 | 56 | |
64 | 58 | await container.wait(ticks) |
65 | 59 | return this.graph.flush(container.state) |
66 | 60 | } |
67 | 61 | |
68 | | - async generateID (port) { |
69 | | - if (!port || !port.id) { |
70 | | - return null |
71 | | - } |
72 | | - |
73 | | - let id = Object.assign({}, port.id) |
74 | | - id = await this.graph.flush(id) |
75 | | - id = id['/'] |
76 | | - if (Buffer.isBuffer(id)) { |
77 | | - id = multibase.encode('base58btc', id).toString() |
78 | | - } |
79 | | - return id |
80 | | - } |
81 | | - |
82 | 62 | registerContainer (type, vm) { |
83 | 63 | this._VMs[type] = vm |
84 | 64 | } |
85 | 65 | } |