Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wisp-bus starter #839

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions experiments/puter-net/child/child.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
window.parent.postMessage({$: "wisp-reg"})

window.parent.postMessage({$: "wisp-tx", new Uint8Array()})
Empty file.
Empty file.
22 changes: 22 additions & 0 deletions experiments/puter-net/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class WispBus extends EventEmitter {
constructor(parameters) {

}
}



class puterSocket {

constructor(host, port) {

}
async write(arg[0]) {

if (args[3]) {

}


}
}
13 changes: 13 additions & 0 deletions experiments/puter-net/parent/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="wisplib.js"></script>
<script src="parent.js"></script>
</head>
<body>
<iframe src="../child/index.html" frameborder="0"></iframe>
</body>
</html>
62 changes: 62 additions & 0 deletions experiments/puter-net/parent/parent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@

const streamID = 1;
const streamMapping = new Map();

const ws = new WebSocket("wss://anura.pro/");
ws.binaryType = "arraybuffer";
ws.onmessage = ((ev) => {
const data = wispPacketParser(new Uint8Array(ev.data));
if ( data.streamID !== 0 )
streamMapping.get(data.streamID).rxCallBack(new Uint8Array(ev.data));
});




// Message registerer
window.addEventListener("message", (message) => {
if (message.data.$ !== "wisp-reg")
return;

const app = message.source;
message.source.addEventListener("message", (message) => {
if (message.data.$ === "wisp-tx") {
const unmapping = {};
const messageData = wispPacketParser(message.data.packet);

if (messageData.type === "CONNECT") {
const view = new DataView(message.data.packet.buffer);
streamMapping.set(streamID, {
originalID: messageData.streamID,
rxCallBack: (data) => {
const view = new DataView(data.buffer);
view.setUint32(0, messageData.streamID, true); // replace with old stream ID

app.postMessage({
$: "wisp-rx",
packet: data
});
}
})
unmapping[messageData.streamID] = streamID;

view.setUint32(0, streamID, true); // override the given stream ID with one we made instead to avoid conflicts
ws.send(message.data);
}
if (messageData.type === "DATA") {
const streamID = unmapping[messageData.streamID];
const view = new DataView(message.data.packet.buffer);
view.setUint32(0, streamID, true); // replace with old stream ID
ws.send(message.data);

}
if (messageData.type === "CLOSE") {
const streamID = unmapping[messageData.streamID];
const view = new DataView(message.data.packet.buffer);
view.setUint32(0, streamID, true); // replace with old stream ID
ws.send(message.data);
delete unmapping[messageData.streamID];
}
}
});
})
51 changes: 51 additions & 0 deletions experiments/puter-net/parent/wisplib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
*
* @param {Uint8Array} arr
* @returns {Object}
*/
function wispPacketParser(arr) {
const returnObj = {};
const view = new DataView(arr.buffer);

returnObj.streamID = view.getUint32(0, true);
returnObj.payload = arr.slice(5);

switch (arr[0]) {
case 1: // CONNECT
returnObj.type = "CONNECT";
returnObj.socketType = view.getUint8(5) === 1 ? "TCP": "UDP";
returnObj.port = view.getUint16(6, true) === 1 ? "TCP": "UDP";
returnObj.hostname = returnObj.payload.slice(7);
break;
case 2: // DATA
returnObj.type = "DATA";
break;
case 3: // CONTINUE
returnObj.type = "CONTINUE";
returnObj.bufferRemaining = view.getUint32(5, true)
break;
case 4: // CLOSE
returnObj.type = "CLOSE";
returnObj.reason = view.getUint8(5)
break;
case 5: // PROTOEXT
returnObj.type = "PROTOEXT"
returnObj.wispVer = `${view.getUint8(5)}.${view.getUint8(6)}`
returnObj.extensions = [];
let pointer = 7;
while (true) {
if (pointer >= view.byteLength)
break;
returnObj.extensions.push(
{
ID: view.getUint8(pointer),
metadata: view.slice(pointer + 2, pointer + view.getUint8(pointer + 1))
}
)
pointer += 2 + view.getUint8(pointer + 1);
}
break;

}
return returnObj;
}
2 changes: 1 addition & 1 deletion src/emulator/image/rootfs/bin/puter-save
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ fi
echo -n $$ > /var/puter-save
PUTERUSERNAME=$( cat /proc/cmdline | sed -n 's/.*puterusername=\(.*\)/\1/p' )
lbu package - > /puter/$PUTERUSERNAME/emulatorstate.apkovl.tar.gz
rm /var/puter-save
rm /var/puter-save
11 changes: 8 additions & 3 deletions src/emulator/image/rootfs/etc/apk/commit_hooks.d/puter.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#!/bin/bash
#!/bin/sh

echo saving state to puter...
puter-save
if [ "$1" = "pre-commit" ]; then
echo Running with puter integration
elif [ "$1" = "post-commit" ]; then
echo Queuing puter save...
puter-save
fi
exit 0