diff --git a/build.cfg b/build.cfg index 2f294b5..ee4e9cc 100755 --- a/build.cfg +++ b/build.cfg @@ -3,6 +3,7 @@ modules/debug/log.lua modules/base/header.lua modules/base/component.lua modules/lib/fs.lua +modules/util/logflush.lua modules/lib/buffer.lua modules/lib/io.lua modules/drivers/vt52.lua diff --git a/modules/lib/relib.lua b/modules/lib/relib.lua index bec6d93..16022f2 100644 --- a/modules/lib/relib.lua +++ b/modules/lib/relib.lua @@ -11,7 +11,7 @@ function relib.encode(p) end function relib.decode(dat) if type(dat) == "string" then - if dat:len() > 7 then + if dat:len() >= 7 then local p = {} p.port = tonumber(string.format("%x%x",string.byte(dat:sub(1,1)),string.byte(dat:sub(2,2))),16) p.pid = dat:sub(3,5) diff --git a/modules/net/copper.lua b/modules/net/copper.lua index 92de0ce..8d91483 100644 --- a/modules/net/copper.lua +++ b/modules/net/copper.lua @@ -9,9 +9,10 @@ function net.send(id,po,msg) -- id, port, message end end if cdlib and relib then -spawn("copperd",function() print(pcall(function () +spawn("copperd",function() print(xpcall(function () local pt = {} local ps = {} + local fcache = {} for a,t in component.list("modem") do table.insert(net.tm,component.proxy(a)) component.proxy(a).open(net.np) @@ -35,29 +36,41 @@ spawn("copperd",function() print(pcall(function () ps[p.pid] = p elseif ev[1] == "modem_message" and ev[4] == net.np and ev[6] == "copper" then local hops,src,dst,data = cdlib.decode(ev[7]) - if not pt[ev[7]:sub(2)] then - pt[ev[7]:sub(2)] = os.time() - if dst == net.id then - event.push("net_rmsg",src,data) - local pd = relib.decode(data) - if pd then - if pd.port and pd.pid and pd.at and pd.pt and pd.msg then - if pd.pt == 1 then - event.push("net_msg",src,pd.port,pd.msg) - pd.pt = 2 - pd.nid = src - pd.lt = 0 - pd.msg = "" - ps[pd.pid] = pd - elseif pd.pt == 2 then - ps[pd.pid] = nil + if hops and src and dst and data then + if not fcache[src] then + log("fcache["..tostring(src).."] = "..tostring(ev[3])) + fcache[src] = {ev[2],ev[3],os.time()} + end + if not pt[ev[7]:sub(2)] then + pt[ev[7]:sub(2)] = os.time() + if dst == net.id then + event.push("net_rmsg",src,data) + local pd = relib.decode(data) + if pd then + if pd.port and pd.pid and pd.at and pd.pt then + if pd.pt == 1 then + event.push("net_msg",src,pd.port,pd.msg) + pd.pt = 2 + pd.nid = src + pd.lt = 0 + pd.msg = "" + ps[pd.pid] = pd + log("[copperd] sent ack to "..src) + elseif pd.pt == 2 then + log("[copperd] received ack from "..src.." for "..pd.pid) + ps[pd.pid] = nil + end end end - end - else - if hops < 255 then - for k,v in ipairs(net.tm) do - v.broadcast(net.np,"copper",cdlib.encode(hops+1,src,dst,data)) + else + if hops < 255 then + if fcache[dst] and _G.LKR_CACHE then + component.invoke(fcache[dst][1],"send",fcache[dst][2],net.np,"copper",cdlib.encode(hops+1,src,dst,data)) + else + for k,v in ipairs(net.tm) do + v.broadcast(net.np,"copper",cdlib.encode(hops+1,src,dst,data)) + end + end end end end @@ -65,8 +78,15 @@ spawn("copperd",function() print(pcall(function () end for k,v in pairs(ps) do if v.lt < os.time()-1 then - for l,m in ipairs(net.tm) do - m.broadcast(net.np,"copper",cdlib.encode(0,net.id,v.nid,relib.encode(v))) + if fcache[v.nid] and _G.LKR_CACHE then + local lma = fcache[v.nid][1] + local rma = fcache[v.nid][2] + print(lma,rma) + component.invoke(lma,"send",rma,net.np,"copper",cdlib.encode(0,net.id,v.nid,relib.encode(v))) + else + for l,m in ipairs(net.tm) do + m.broadcast(net.np,"copper",cdlib.encode(0,net.id,v.nid,relib.encode(v))) + end end v.at = v.at + 1 v.lt = os.time() diff --git a/modules/util/logflush.lua b/modules/util/logflush.lua new file mode 100644 index 0000000..ccfdb69 --- /dev/null +++ b/modules/util/logflush.lua @@ -0,0 +1,16 @@ +spawn("logflushd",function() + local lt = os.time() + while true do + if os.time() > lt + 60 and Log:len() > 0 then + local f = fs.open("/tmp/sys.log","ab") + if f then + for i = 1, #Log, 1024 do + fs.write(f,Log:sub(i,i+1023)) + end + fs.close(f) + Log = "" + end + end + coroutine.yield() + end +end)