git ssb

0+

wanderer🌟 / js-primea-hypervisor



Tree: 2d384c9d532ddc23ea6089a835bb4b338df7bd85

Files: 2d384c9d532ddc23ea6089a835bb4b338df7bd85 / creationService.js

1870 bytesRaw
1const chunk = require('chunk')
2
3const MAX_DATA_BYTES = 65533
4
5module.exports = class CreationService {
6 constructor (opts) {
7 this.hypervisor = opts.hypervisor
8 this.scheduler = this.hypervisor.scheduler
9 }
10
11 queue (port, message) {
12 if (message.data.type) {
13 let id
14 if (message.fromId) {
15 const creator = this.scheduler.getInstance(message.fromId)
16 id = creator.generateNextId()
17 }
18 return this.createInstance(message, id)
19 }
20 }
21
22 getPort () {
23 return {
24 messages: [],
25 destId: 0
26 }
27 }
28
29 // send (port, message) {
30 // message._hops++
31 // message._fromTicks = this.ticks
32 // message.fromId = this.id
33
34 // return this.hypervisor.send(port, message)
35 // }
36
37 /**
38 * creates an new container instances and save it in the state
39 * @returns {Promise}
40 */
41 async createInstance (message, id = {nonce: 0, parent: null}) {
42 const idHash = await this._getHashFromObj(id)
43 const state = {
44 nonce: [0],
45 ports: {},
46 type: message.data.type
47 }
48
49 if (message.data.code && message.data.code.length) {
50 state.code = message.data.code
51 }
52
53 // create the container instance
54 const instance = await this.hypervisor._loadInstance(idHash, state)
55
56 // send the intialization message
57 await instance.create(message)
58
59 if (state.code && state.code.length > MAX_DATA_BYTES) {
60 state.code = chunk(state.code, MAX_DATA_BYTES).map(chk => {
61 return {
62 '/': chk
63 }
64 })
65 }
66 // save the container in the state
67 await this.hypervisor.tree.set(idHash, state)
68
69 if (!Object.keys(instance.ports.ports).length) {
70 this.hypervisor.addNodeToCheck(instance.id)
71 }
72
73 return instance
74 }
75
76 // get a hash from a POJO
77 _getHashFromObj (obj) {
78 return this.hypervisor.graph.flush(obj).then(obj => obj['/'])
79 }
80}
81

Built with git-ssb-web