OC-PsychOS/modules/net/copper.0.lua

46 lines
1.1 KiB
Lua
Raw Normal View History

net = {}
net.id = computer.address():sub(1,8)
net.np = 4957
net.tm = {}
function net.send(id,po,msg) -- id, port, message
event.push("sendmsg",id,po,msg)
end
if cdlib then
spawn("copper.0 daemon",function() print(pcall(function ()
local pt = {}
for a,t in component.list("modem") do
table.insert(net.tm,component.proxy(a))
component.proxy(a).open(net.np)
end
while true do
local ev = {event.pull()}
if ev[1] == "rsendmsg" then
dst,data = ev[2],ev[3]
for k,v in ipairs(net.tm) do
v.broadcast(net.np,"copper",cdlib.encode(0,net.id,dst,data))
end
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)
else
for k,v in ipairs(net.tm) do
v.broadcast(net.np,cdlib.encode(hops+1,src,dst,data))
end
end
end
end
if #pt > 63 then
local cot = os.time()
for k,v in pairs(pt) do
if v < cot-5 then
pt[k] = nil
end
end
end
end
end)) end)
end