git ssb

0+

wanderer🌟 / js-primea-hypervisor



Commit 218dd59e9632521763daa53b40550901c8c2dcf0

Merge pull request #118 from primea/unboundPorts

adds more port binding functionality
wanderer authored on 5/29/2017, 5:04:31 PM
GitHub committed on 5/29/2017, 5:04:31 PM
Parent: 1df41b95ebf09568a8c125df38f2bcedfb9ad8b5
Parent: c8d99be502a496e485cae703eed1e18dc149870b

Files changed

exoInterface.jschanged
portManager.jschanged
tests/index.jschanged
exoInterface.jsView
@@ -146,10 +146,16 @@
146146 /**
147147 * creates a new message
148148 * @param {*} data
149149 */
150- createMessage (data) {
151- return new Message(data)
150+ createMessage (opts) {
151+ const message = new Message(opts)
152+ for (const port of message.ports) {
153+ if (this.ports.isBound(port)) {
154+ throw new Error('message must not contain bound ports')
155+ }
156+ }
157+ return message
152158 }
153159
154160 /**
155161 * sends a message to a given port
portManager.jsView
@@ -40,8 +40,9 @@
4040 */
4141 constructor (opts) {
4242 Object.assign(this, opts)
4343 this._portMap = new Map()
44+ this._unboundPort = new WeakSet()
4445 }
4546
4647 /**
4748 * starts the port manager. This fetchs the ports from the state and maps
@@ -72,39 +73,34 @@
7273 * @param {Object} port - the port to bind
7374 * @param {String} name - the name of the port
7475 */
7576 bind (port, name) {
77+ if (this.isBound(port)) {
78+ throw new Error('cannot bind a port that is already bound')
79+ }
7680 // save the port instance
7781 this.ports[name] = port
7882 this._bindRef(port, name)
7983 }
8084
8185 /**
82- * queues a message on a port
83- * @param {Message} message
84- */
85- queue (message) {
86- this._portMap.get(message.fromPort).queue(message)
87- }
88-
89- /**
90- * gets a port given it's name
86+ * unbinds a port given its name
9187 * @param {String} name
92- * @return {Object}
88+ * @returns {boolean} whether or not the port was deleted
9389 */
94- get (name) {
95- return this.ports[name]
90+ unbind (name) {
91+ const port = this.ports[name]
92+ delete this.ports[name]
93+ this._portMap.delete(port)
94+ return port
9695 }
9796
9897 /**
99- * deletes a port given its name
100- * @param {String} name
101- * @returns {boolean} whether or not the port was deleted
98+ * get the port name given its referance
99+ * @return {string}
102100 */
103- delete (name) {
104- const port = this.ports[name]
105- delete this.ports[name]
106- return this._portMap.delete(port)
101+ getBoundName (portRef) {
102+ return this._portMap.get(portRef).name
107103 }
108104
109105 /**
110106 * check if a port object is still valid
@@ -114,8 +110,25 @@
114110 isBound (port) {
115111 return this._portMap.has(port)
116112 }
117113
114+ /**
115+ * queues a message on a port
116+ * @param {Message} message
117+ */
118+ queue (message) {
119+ this._portMap.get(message.fromPort).queue(message)
120+ }
121+
122+ /**
123+ * gets a port given it's name
124+ * @param {String} name
125+ * @return {Object}
126+ */
127+ get (name) {
128+ return this.ports[name]
129+ }
130+
118131 _createPortObject (type, link) {
119132 const parentId = this.entryPort ? this.entryPort.id : null
120133 let nonce = this.state['/'].nonce
121134
@@ -134,8 +147,9 @@
134147 // incerment the nonce
135148 nonce = new BN(nonce)
136149 nonce.iaddn(1)
137150 this.state['/'].nonce = nonce.toArray()
151+ this._unboundPort.add(portRef)
138152 return portRef
139153 }
140154
141155 /**
tests/index.jsView
@@ -234,9 +234,9 @@
234234 class Root extends BaseContainer {
235235 async run (m) {
236236 const port = this.kernel.ports.create('root')
237237 this.kernel.ports.bind(port, 'three')
238- this.kernel.ports.delete('three')
238+ this.kernel.ports.unbind('three')
239239 try {
240240 await this.kernel.send(port, this.kernel.createMessage())
241241 } catch (e) {
242242 t.pass()
@@ -679,5 +679,35 @@
679679 let port2 = root.ports.copy(port)
680680
681681 t.equals(port2.type, port.type, 'should copy port type')
682682 })
683+
684+ tape('checking ports', async t => {
685+ t.plan(4)
686+ const hypervisor = new Hypervisor(node.dag)
687+ hypervisor.registerContainer('base', BaseContainer)
688+
689+ const root = await hypervisor.createInstance('base')
690+ let port = root.ports.create('base')
691+ root.ports.bind(port, 'test')
692+
693+ t.equals(root.ports.getBoundName(port), 'test', 'should get the ports name')
694+
695+ try {
696+ root.createMessage({
697+ ports: [port]
698+ })
699+ } catch (e) {
700+ t.pass('should thow if sending a port that is bound')
701+ }
702+
703+ try {
704+ root.ports.bind(port, 'test')
705+ } catch (e) {
706+ t.pass('should thow if binding an already bound port')
707+ }
708+
709+ root.ports.unbind('test')
710+ const message = root.createMessage({ports: [port]})
711+ t.equals(message.ports[0], port, 'should create a message if the port is unbound')
712+ })
683713 })

Built with git-ssb-web