From fb0853bb0dd245bc2997e127dff48bc67249d2d1 Mon Sep 17 00:00:00 2001
From: Jake <jake.read@cba.mit.edu>
Date: Mon, 29 Apr 2019 12:10:38 -0400
Subject: [PATCH] can serialize, deserialize hunk
---
hunks/hunks.js | 12 +++----
hunks/link.js | 4 +--
hunks/manager.js | 81 ++++++++++++++++++++++++++++++++++--------------
hunks/view.js | 77 +++++++++++++++++++++++++++++++++++++++++++--
typeset.js | 34 +++++++++++++-------
5 files changed, 163 insertions(+), 45 deletions(-)
diff --git a/hunks/hunks.js b/hunks/hunks.js
index 9d127b6..14ccdc9 100644
--- a/hunks/hunks.js
+++ b/hunks/hunks.js
@@ -238,12 +238,12 @@ function deepCopy(obj) {
}
function isOKType(type){
- // etc,
- return (type === 'Number'
- || type === 'Boolean'
- || type === 'String'
- || type === 'Object'
- || type === 'Any'
+ // etc, lowecase to match with typeof ~ queeeries ~
+ return (type === 'number'
+ || type === 'boolean'
+ || type === 'string'
+ || type === 'object'
+ || type === 'any'
|| type === 'byte'
|| type === 'byteArray'
|| type === 'uint8'
diff --git a/hunks/link.js b/hunks/link.js
index 399f9a6..642d95c 100644
--- a/hunks/link.js
+++ b/hunks/link.js
@@ -25,8 +25,8 @@ function Link() {
this.outputs.push(dtout)
// default messages -> manager, besides also data link
- let inputList = new State('String', 'inputList', "mgrMsgs (byteArray)")
- let outputList = new State('String', 'outputList', "mgrMsgs (byteArray)")
+ let inputList = new State('string', 'inputList', "mgrMsgs (byteArray)")
+ let outputList = new State('string', 'outputList', "mgrMsgs (byteArray)")
this.state.push(inputList, outputList)
/* --------------------------- ---------------------------- */
diff --git a/hunks/manager.js b/hunks/manager.js
index fa8cad8..0839b29 100644
--- a/hunks/manager.js
+++ b/hunks/manager.js
@@ -8,6 +8,7 @@ import {
import {
TSET,
MK,
+ HK,
MSGS
} from '../typeset.js'
@@ -43,7 +44,7 @@ function Manager() {
let msg = new Array()
msg.push(MK.ERR)
MSGS.writeTo(msg, message, 'string')
- writemessage(msg)
+ writeMessage(msg)
}
/* --------------------------- ---------------------------- */
@@ -74,10 +75,41 @@ function Manager() {
this.sendHunkAsDef = (hunk) => {
let def = writeDefinition(hunk)
if (msgverbose) this.log('msg: putdef', def)
- writemessage('putdef', def)
+ writeMessage('putdef', def)
}
let serializeHunk = (hunk, bytes) => {
+ // write the hunk into this array
+ bytes.push(HK.ID)
+ MSGS.writeTo(bytes, hunk.id, 'string')
+ bytes.push(HK.NAME)
+ MSGS.writeTo(bytes, hunk.name, 'string')
+ // write inputs,
+ if(hunk.inputs.length > 0){
+ for(let ip of hunk.inputs){
+ bytes.push(HK.INPUT)
+ MSGS.writeTo(bytes, ip.name, 'string')
+ MSGS.writeTo(bytes, ip.type, 'string')
+ }
+ }
+ // write outputs,
+ if(hunk.outputs.length > 0){
+ for(let op of hunk.outputs){
+ bytes.push(HK.OUTPUT)
+ MSGS.writeTo(bytes, op.name, 'string')
+ MSGS.writeTo(bytes, op.type, 'string')
+ }
+ }
+ // write state,
+ if(hunk.state.length > 0){
+ for(let st of hunk.state){
+ bytes.push(HK.STATE)
+ MSGS.writeTo(bytes, st.name, 'string')
+ MSGS.writeTo(bytes, st.type, 'string')
+ // yarr, last is a debug flag
+ MSGS.writeTo(bytes, st.value, st.type, true)
+ }
+ }
// HERE you r at this ... name / id / inputs / outputs / state
}
@@ -214,7 +246,7 @@ function Manager() {
for (let st in hunk.state) {
hunk.state[st].hookup = (value) => {
// console.log('MGR -> VIEW STATECHANGE')
- writemessage('putstate', {
+ writeMessage('putstate', {
id: hunk.id,
name: hunk.state[st].name,
value: value
@@ -239,7 +271,7 @@ function Manager() {
//console.log('SETTING', state[sn])
so.value = state[sn]
} else {
- writemessage('error', 'problematic state content in program, object not found')
+ writeMessage('error', 'problematic state content in program, object not found')
}
}
}
@@ -321,7 +353,7 @@ function Manager() {
if (verbose) this.log(`hooking ${outHunk.id}, ${otp.name} to ${inHunk.id}, ${inp.name}`)
otp.attach(inp, inHunk.id)
// should be verbatim, so
- writemessage('putlink', {
+ writeMessage('putlink', {
outId: outHunkId,
outName: outputName,
inId: inHunkId,
@@ -358,7 +390,7 @@ function Manager() {
} else {
this.log(`unhooking ${outHunk.id} to ${inHunk.id}`)
if (outHunk.outputs[outputName].remove(inHunk.inputs[inputName], inHunk.id)) {
- writemessage('removelink', {
+ writeMessage('removelink', {
outId: outHunkId,
outName: outputName,
inId: inHunkId,
@@ -478,19 +510,19 @@ function Manager() {
if (msgsin.io) {
let msg = msgsin.get()
if (msgverbose) this.log(`gets msg ${header}`)
- if (!Array.isArray(msg)) throw new Error('hello pls messages by array only now')
+ if (!Array.isArray(msg)) throw new Error(`manager throwing object message, having header ${msg.header}`)
// once
let resp = new Array()
// rules: bytes in this switch, objects elsewhere ?
switch (msg[0]) {
case MK.HELLO:
- writemessage([MK.HELLORESPONSE])
+ writeMessage([MK.HELLORESPONSE])
break
case MK.DESCRIBESELF:
resp.push(MK.BRIEF)
MSGS.writeTo(resp, this.id, 'string')
MSGS.writeTo(resp, GG.interpretername, 'string')
- writemessage(resp)
+ writeMessage(resp)
break
case MK.REQLISTAVAIL:
// allow for error path,
@@ -501,7 +533,7 @@ function Manager() {
// console.log('writing item', item)
MSGS.writeTo(resp, item, 'string')
}
- writemessage(resp)
+ writeMessage(resp)
// for item in list add to resp as string,
// what of top level designation as list-of-strings ? lengths!
}).catch((err) => {
@@ -516,13 +548,16 @@ function Manager() {
// then do,
// a meta comment is that this add promise doesn't fail for
// things like path-loading errors, that would be inside of gogetter.js
+ // i.e. pulling a hunk loads it into that import script hack, that just
+ // gets evaluated into the dom ...
console.log('manager to add', strname)
this.addHunk(strname).then((hunk) => {
console.log('successfully added hunk', hunk)
// serialize
resp.push(MK.HUNKALIVE)
serializeHunk(hunk, resp)
- console.log('TERM serialization', resp)
+ // ok then,
+ writeMessage(resp)
}).catch((err) => {
console.log('addhunk err', err)
})
@@ -536,7 +571,7 @@ function Manager() {
switch (false) {
case 'hello':
let options = this.getHelloResponses()
- writemessage('putcontextoptions', options)
+ writeMessage('putcontextoptions', options)
break
case 'listhunks':
getListOfAvailableComponents().then((list) => {
@@ -550,9 +585,9 @@ function Manager() {
}
options.push(option)
}
- writemessage('putcontextoptions', options)
+ writeMessage('putcontextoptions', options)
}).catch((err) => {
- writemessage('error', 'ERR while getting list of available' + err)
+ writeMessage('error', 'ERR while getting list of available' + err)
})
break
case 'addprogram':
@@ -562,7 +597,7 @@ function Manager() {
// newhunks are automatically reported on msgs, and so are new links
// so we just want a catch block here
}).catch((err) => {
- writemessage('error', 'ERR while adding program: ' + err)
+ writeMessage('error', 'ERR while adding program: ' + err)
})
break
case 'addhunk':
@@ -572,28 +607,28 @@ function Manager() {
this.log('added hunk with id', hunk.id)
}).catch((err) => {
this.log('ERR while adding hunk', err)
- writemessage('error', 'ERR on adding hunk: ' + err)
+ writeMessage('error', 'ERR on adding hunk: ' + err)
})
break
case 'removehunk':
if (this.removeHunk(content)) {
- writemessage('removedef', content.id)
+ writeMessage('removedef', content.id)
} else {
- writemessage('error', 'could not remove hunk as requested')
+ writeMessage('error', 'could not remove hunk as requested')
}
break
case 'addlink':
if (this.addLink(content.outId, content.outName, content.inId, content.inName)) {
// gr8 success, and addlink automatically updates with a message to the view
} else {
- writemessage('error', 'could not add link as requested')
+ writeMessage('error', 'could not add link as requested')
}
break
case 'removelink':
if (this.removeLink(content.outId, content.outName, content.inId, content.inName)) {
// gr8 success, continue
} else {
- writemessage('error', 'cannot remove link as requested')
+ writeMessage('error', 'cannot remove link as requested')
}
break
case 'statechange':
@@ -601,12 +636,12 @@ function Manager() {
// no response for this, message should pass from hunk
}).catch((err) => {
this.log('ERR at statechange', err)
- writemessage('error', 'could not change state as requested')
+ writeMessage('error', 'could not change state as requested')
})
break
default:
- writemessage('error', 'manager receives message with no switch!')
+ writeMessage('error', 'manager receives message with no switch!')
this.log('MGR: msg with no switch')
break
}
@@ -693,7 +728,7 @@ function Manager() {
let outmsgbuffer = new Array()
- let writemessage = (bytes) => {
+ let writeMessage = (bytes) => {
if (!msgsout.io && outmsgbuffer.length < 1) {
// str8 shooters
if (msgverbose) this.log('msg out', bytes)
diff --git a/hunks/view.js b/hunks/view.js
index 0be6a41..7af3876 100644
--- a/hunks/view.js
+++ b/hunks/view.js
@@ -22,6 +22,7 @@ import {
import {
TSET, // typset
MK, // manager keys,
+ HK, // hunk keys,
MSGS // messaging
} from '../typeset.js'
@@ -573,6 +574,73 @@ function View() {
/* -------------------- DEFS in the DOM ---------------------- */
/* --------------------------- ---------------------------- */
+ let deserializeNameType = (arr, bytes, i) => {
+ let nameresp = MSGS.readFrom(bytes, i, 'string')
+ let typeresp = MSGS.readFrom(bytes, i + nameresp.increment, 'string')
+ arr.push({
+ name: nameresp.item,
+ type: typeresp.item
+ })
+ return nameresp.increment + typeresp.increment
+ }
+
+ let defBySerial = (bytes) => {
+ // deserialize
+ let def = {}
+ // inputs, outputs, state
+ def.inputs = new Array()
+ def.outputs = new Array()
+ def.state = new Array()
+ // hold,
+ let temp
+ // starting at 2, msgs[0] is 'hnkalive'
+ let i = 1
+ // ripperoni,
+ outer: while(i < bytes.length){
+ switch (bytes[i]){
+ case HK.ID:
+ i += 1
+ temp = MSGS.readFrom(bytes, i, 'string')
+ def.id = temp.item
+ i += temp.increment
+ break
+ case HK.NAME:
+ i += 1
+ temp = MSGS.readFrom(bytes, i, 'string')
+ def.name = temp.item
+ i += temp.increment
+ break
+ case HK.INPUT:
+ i += 1
+ // expecting two strings here, name and then type
+ i += deserializeNameType(def.inputs, bytes, i)
+ break
+ case HK.OUTPUT:
+ i += 1
+ i += deserializeNameType(def.outputs, bytes, i)
+ break
+ case HK.STATE:
+ i += 1
+ i += deserializeNameType(def.state, bytes, i)
+ // ok, and the value should trail
+ // we don't *need* to know the type, could just read by the key, but,
+ temp = MSGS.readFrom(bytes, i, def.state[def.state.length - 1].type)
+ def.state[def.state.length - 1].value = temp.item
+ i += temp.increment
+ break
+ default:
+ throw new Error(`unexpected key encountered in hunk deserialization at ${i}: ${bytes[i]}`)
+ break outer
+ }
+ }
+ // a check,
+ console.log(`broke outer, len at ${bytes.length}, i at ${i}`)
+ // we should be able to kind of pick-thru and append based on,
+ console.log('def apres deserialize', def)
+ // we gucc ?
+ return def
+ }
+
let putDef = (def) => {
// hmmm ok,
if (verbose) console.log('ready write', def.id)
@@ -629,7 +697,7 @@ function View() {
writeToMessageBox(`native hunk attach fails ${err}`)
}
}
-
+ //
writeTransform(de, mt)
// rm menu if it's around
if (menu !== undefined) $(menu).remove()
@@ -1007,7 +1075,7 @@ function View() {
let msg = msgsin.get()
// at view, read in and deserialize list
if(msgverbose) console.log('VIEW MSG:', msg)
- if(!Array.isArray(msg)) throw new Error('hello pls msgs by array only now')
+ if(!Array.isArray(msg)) throw new Error(`view throwing object message, having header ${msg.header}`)
// ok,
switch(msg[0]) {
@@ -1019,6 +1087,11 @@ function View() {
let stringlist = MSGS.readListFrom(msg, 1, 'string')
addContextOptions(stringlist)
break
+ case MK.HUNKALIVE:
+ console.log('hunk alive, going to deserialize')
+ let def = defBySerial(msg)
+ putDef(def)
+ break
default:
throw new Error(`view receives message with no switch: ${msg[0]}`)
}
diff --git a/typeset.js b/typeset.js
index 9d173f6..8ee267c 100644
--- a/typeset.js
+++ b/typeset.js
@@ -117,7 +117,7 @@ const writeLenBytes = (len) => {
}
if(i > 2) throw new Error('suspiciously long len cast', bts)
}
- console.log(`for len ${len}, wrote bytes`, bts)
+ //console.log(`for len ${len}, wrote bytes`, bts)
return bts
}
@@ -151,7 +151,7 @@ const MSGS = {
writeTo: function(bytes, thing, type, debug){
let phy = findPhy(type)
let block = phy.write(thing)
- if(debug) console.log('writing block in:', block)
+ if(debug) console.log(`writing for type ${type} and thing '${thing}' the following block of bytes`, block)
// write-in to msg like this
block.forEach((byte) => {
bytes.push(byte)
@@ -193,25 +193,35 @@ const MK = {
HELLO: 253, // (eom)
HELLORESPONSE: 252, // (eom) ?
// request a top-level description
- DESCRIBESELF: 251, // (eom)
- BRIEF: 250, // (str) name of interpreter, # hunks, # links (and then begin firing list back)
+ //DESCRIBESELF: 251, // (eom)
+ //BRIEF: 250, // (str) name of interpreter, # hunks, # links (and then begin firing list back)
// please show what is available
REQLISTAVAIL: 249, // (eom)
LISTOFAVAIL: 248, // (list)(str) names 'dirs/like/this' (includes programs ?) (this might be multiple packets?)
- // business,
+ // business ... we should be able to centralize all control w/i view.js if we can write these
REQADDHUNK: 247, // (str) name
HUNKALIVE: 246, // (hunkdescription): name, id, inputlist, outputlist, statelist
- REQRMHUNK: 245, // (str) id
- HUNKREMOVED: 244, // (str) id
- REQADDLINK: 243, // (str) id, (str) outname, (str) id, (str) inname
- LINKALIVE: 242, // (str) id, (str) outname, (str) id, (str) inname
- REQRMLINK: 241, // (str) id, (str) outname, (str) id, (str) inname
- LINKREMOVED: 240, // (str) id, (str) outname, (str) id, (str) inname
+ //REQRMHUNK: 245, // (str) id
+ //HUNKREMOVED: 244, // (str) id
+ //REQADDLINK: 243, // (str) id, (str) outname, (str) id, (str) inname
+ //LINKALIVE: 242, // (str) id, (str) outname, (str) id, (str) inname
+ //REQRMLINK: 241, // (str) id, (str) outname, (str) id, (str) inname
+ //LINKREMOVED: 240, // (str) id, (str) outname, (str) id, (str) inname
+}
+
+// hunk description keys,
+const HK = {
+ NAME: 253,
+ ID: 252,
+ DESCR: 251,
+ INPUT: 249,
+ OUTPUT: 247,
+ STATE: 245,
}
// should write out as list of pairs ?
// or write fn to do key(stringtype)
export {
- TSET, MSGKEY_ACK, MK, MSGS
+ TSET, MSGKEY_ACK, MK, HK, MSGS
}
--
GitLab