From bce7d5d30fafd6d79ee3b28e7c2da8a595df33aa Mon Sep 17 00:00:00 2001 From: Izaya Date: Mon, 12 Mar 2018 01:25:50 +1100 Subject: [PATCH] added some embedded stuff, still WIP and probably buggy --- Embedded/microtel-3.lua | 108 ++++++++++++++++++++++++++++++++++++++++ Embedded/minify.sh | 15 ++++++ Embedded/strip.lua | 38 ++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 Embedded/microtel-3.lua create mode 100755 Embedded/minify.sh create mode 100644 Embedded/strip.lua diff --git a/Embedded/microtel-3.lua b/Embedded/microtel-3.lua new file mode 100644 index 0000000..c3ee3dd --- /dev/null +++ b/Embedded/microtel-3.lua @@ -0,0 +1,108 @@ +_G.net = {} +net.port = 4096 +net.hostname = computer.address():sub(1,8) +net.hostname = "micro" +net.debug = false +net.rctime = 30 +net.pctime = 30 +net.retry = 30 + +do + +local rcpe, pcache, rcache, pqueue, modems = computer.pullSignal, {}, {}, {}, {} +--subhere + +for a,t in component.list("modem") do + modems[#modems+1] = component.proxy(a) + modems[#modems].open(net.port) +end + +local function genPacketID() + local npID = "" + for i = 1, 16 do + npID = npID .. string.char(math.random(32,126)) + end + return npID +end + +local function sendPacket(packetID,packetType,dest,sender,vport,data) + if rcache[dest] then + component.invoke(rcache[dest][1],"send",rcache[dest][2],net.port,packetID,packetType,dest,sender,vport,data) + else + for k,v in pairs(modems) do + v.broadcast(net.port,packetID,packetType,dest,sender,vport,data) + end + end +end + +local function pruneCache() + for k,v in pairs(rcache) do + if v[3] < computer.uptime() then + rcache[k] = nil + end + end + for k,v in pairs(pcache) do + if v < computer.uptime() then + pcache[k] = nil + end + end +end + +local function checkPCache(packetID) + for k,v in pairs(pcache) do + if k == packetID then return true end + end + return false +end +local function packetPusher() + for k,v in pairs(pqueue) do + if v[5] < computer.uptime() then + sendPacket(k,v[1],v[2],net.hostname,v[3],v[4]) + if v[1] ~= 1 or v[6] == 255 then + pqueue[k] = nil + else + pqueue[k][5]=computer.uptime()+net.retry + pqueue[k][6]=pqueue[k][6]+1 + end + end + end +end + +function computer.pullSignal(t) + pruneCache() + packetPusher() + local tev = {rcpe(t)} + if tev[1] == "modem_message" and tev[4] == net.port and not checkPCache(tev[6]) then + if tev[8] == net.hostname then + if tev[7] == 1 then + sendPacket(genPacketID(),2,tev[9],net.hostname,tev[10],tev[6]) + end + if tev[7] == 2 then + pqueue[tev[11]] = nil + computer.pushSignal("net_ack",data) + end + if packetType ~= 2 then + computer.pushSignal("net_msg",tev[9],tev[10],tev[11]) + end + else + sendPacket(tev[6],tev[7],tev[8],tev[9],tev[10],tev[11]) + end + if not rcache[tev[9]] then + rcache[tev[9]] = {tev[2],tev[3],computer.uptime()+net.rctime} + end + if not pcache[tev[6]] then + pcache[tev[6]] = computer.uptime()+net.pctime + end + end +end + +function net.send(ptype,to,vport,data,npID) + npID = npID or genPacketID() + pqueue[npID] = {ptype,to,vport,data,0,0} +end + +end + +while true do + computer.pullSignal() +end diff --git a/Embedded/minify.sh b/Embedded/minify.sh new file mode 100755 index 0000000..e407be9 --- /dev/null +++ b/Embedded/minify.sh @@ -0,0 +1,15 @@ +#!/usr/bin/env bash +cp $1 $2 +sed -i -e 's/pcache/PC/g' $2 +sed -i -e 's/rcache/RC/g' $2 +sed -i -e 's/pqueue/pQ/g' $2 +sed -i -e 's/modems/M/g' $2 +sed -i -e 's/genPacketID/gP/g' $2 +sed -i -e 's/sendPacket/sP/g' $2 +sed -i -e 's/pruneCache/pC/g' $2 +sed -i -e 's/checkPCache/cPC/g' $2 +sed -i -e 's/packetPusher/pP/g' $2 +sed -i -e 's/component.invoke/cI/g' $2 +sed -i -e 's/computer.uptime/cU/g' $2 +sed -i -e 's/--subhere/local cI,cU = component.invoke,computer.uptime/g' $2 +lua strip.lua $2 $2 diff --git a/Embedded/strip.lua b/Embedded/strip.lua new file mode 100644 index 0000000..032548c --- /dev/null +++ b/Embedded/strip.lua @@ -0,0 +1,38 @@ +tA={...} +f=io.open(tA[1]) +ss=f:read("*a") +f:close() +print("Optimising source") +sl=tostring(ss:len()) +no=0 +replacements={ +{" "," "}, +{"\n ","\n"}, +{"\n\n","\n"}, +{" == ","=="}, +{" ~= ","~="}, +{" >= ",">="}, +{" <= ","<="}, +{" > ",">"}, +{" < ","<"}, +{" = ","="}, +{", ",","}, +{" %+ ","+"}, +{" %- ","-"}, +{" %/ ","/"}, +{" %* ","*"}, +{" \n","\n"}, +{"%-%-.-\n",""}, +} +for k,v in ipairs(replacements) do + while ss:find(v[1]) ~= nil do + ss=ss:gsub(v[1],v[2]) + io.write(".") + no=no+1 + end +end +print("\nBefore: "..sl.."\nAfter: "..tostring(ss:len()).."\nDelta: "..tostring(sl-ss:len())) + +f=io.open(tA[2],"wb") +f:write(ss) +f:close()