fixed more stuff for #5, added logflushd

This commit is contained in:
Izaya 2017-09-04 20:13:31 +10:00
parent caeeb10d25
commit 1e3d4eddac
4 changed files with 62 additions and 25 deletions

View File

@ -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

View File

@ -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)

View File

@ -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()

16
modules/util/logflush.lua Normal file
View File

@ -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)