1
0
mirror of https://github.com/ShadowKatStudios/OC-Minitel.git synced 2024-11-23 10:38:05 +11:00

made the minitel daemom use a config file

This commit is contained in:
Izaya 2018-07-27 10:43:56 +10:00
parent b21295f9fb
commit 97734851dc

View File

@ -13,17 +13,20 @@ data: the actual packet data, duh.
local listeners = {}
local timers = {}
local cfg = {}
local event = require "event"
local component = require "component"
local computer = require "computer"
local hostname = computer.address()
local serial = require "serialization"
local hostname = computer.address():sub(1,8)
local listener = false
local dbug = false
cfg.debug = false
local modems = {}
local port = 4096
local retry = 10
local retrycount = 64
local route = true
cfg.port = 4096
cfg.retry = 10
cfg.retrycount = 64
cfg.route = true
--[[
LKR format:
@ -33,9 +36,9 @@ address {
time last received
}
]]--
local sroutes = {}
local rcache = setmetatable({},{__index=sroutes})
local rctime = 15
cfg.sroutes = {}
local rcache = setmetatable({},{__index=cfg.sroutes})
cfg.rctime = 15
--[[
packet queue format:
@ -52,15 +55,36 @@ local pqueue = {}
-- packet cache: [packet ID]=uptime
local pcache = {}
local pctime = 30
cfg.pctime = 30
local function dprint(...)
if dbug then
if cfg.debug then
print(...)
end
end
local function saveconfig()
local f = io.open("/etc/minitel.cfg","wb")
if f then
f:write(serial.serialize(cfg))
f:close()
end
end
local function loadconfig()
local f = io.open("/etc/minitel.cfg","rb")
if f then
local newcfg = serial.unserialize(f:read("*a"))
f:close()
for k,v in pairs(newcfg) do
cfg[k] = v
end
else
saveconfig()
end
end
function start()
loadconfig()
local f=io.open("/etc/hostname","rb")
if f then
hostname = f:read()
@ -73,8 +97,8 @@ function start()
modems[#modems+1] = component.proxy(a)
end
for k,v in ipairs(modems) do
v.open(port)
print("Opened port "..port.." on "..v.address)
v.open(cfg.port)
print("Opened port "..cfg.port.." on "..v.address)
end
for a,t in component.list("tunnel") do
modems[#modems+1] = component.proxy(a)
@ -88,21 +112,21 @@ function start()
return npID
end
local function sendPacket(packetID,packetType,dest,sender,vport,data)
local function sendPacket(packetID,packetType,dest,sender,vPort,data)
if rcache[dest] then
dprint("Cached", rcache[dest][1],"send",rcache[dest][2],port,packetID,packetType,dest,sender,vport,data)
dprint("Cached", rcache[dest][1],"send",rcache[dest][2],cfg.port,packetID,packetType,dest,sender,vPort,data)
if component.type(rcache[dest][1]) == "modem" then
component.invoke(rcache[dest][1],"send",rcache[dest][2],port,packetID,packetType,dest,sender,vport,data)
component.invoke(rcache[dest][1],"send",rcache[dest][2],cfg.port,packetID,packetType,dest,sender,vPort,data)
elseif component.type(rcache[dest][1]) == "tunnel" then
component.invoke(rcache[dest][1],"send",packetID,packetType,dest,sender,vport,data)
component.invoke(rcache[dest][1],"send",packetID,packetType,dest,sender,vPort,data)
end
else
dprint("Not cached", port,packetID,packetType,dest,sender,vport,data)
dprint("Not cached", cfg.port,packetID,packetType,dest,sender,vPort,data)
for k,v in pairs(modems) do
if v.type == "modem" then
v.broadcast(port,packetID,packetType,dest,sender,vport,data)
v.broadcast(cfg.port,packetID,packetType,dest,sender,vPort,data)
elseif v.type == "tunnel" then
v.send(packetID,packetType,dest,sender,vport,data)
v.send(packetID,packetType,dest,sender,vPort,data)
end
end
end
@ -133,14 +157,14 @@ function start()
return false
end
local function processPacket(_,localModem,from,pport,_,packetID,packetType,dest,sender,vport,data)
local function processPacket(_,localModem,from,pport,_,packetID,packetType,dest,sender,vPort,data)
pruneCache()
if pport == port or pport == 0 then -- for linked cards
dprint(port,vport,packetType,dest)
if pport == cfg.port or pport == 0 then -- for linked cards
dprint(cfg.port,vPort,packetType,dest)
if checkPCache(packetID) then return end
if dest == hostname then
if packetType == 1 then
sendPacket(genPacketID(),2,sender,hostname,vport,packetID)
sendPacket(genPacketID(),2,sender,hostname,vPort,packetID)
end
if packetType == 2 then
dprint("Dropping "..data.." from queue")
@ -148,19 +172,19 @@ function start()
computer.pushSignal("net_ack",data)
end
if packetType ~= 2 then
computer.pushSignal("net_msg",sender,vport,data)
computer.pushSignal("net_msg",sender,vPort,data)
end
elseif dest:sub(1,1) == "~" then -- broadcasts start with ~
computer.pushSignal("net_broadcast",sender,vport,data)
elseif route then -- repeat packets if route is enabled
sendPacket(packetID,packetType,dest,sender,vport,data)
computer.pushSignal("net_broadcast",sender,vPort,data)
elseif cfg.route then -- repeat packets if route is enabled
sendPacket(packetID,packetType,dest,sender,vPort,data)
end
if not rcache[sender] then -- add the sender to the rcache
dprint("rcache: "..sender..":", localModem,from,computer.uptime())
rcache[sender] = {localModem,from,computer.uptime()+rctime}
rcache[sender] = {localModem,from,computer.uptime()+cfg.rctime}
end
if not pcache[packetID] then -- add the packet ID to the pcache
pcache[packetID] = computer.uptime()+pctime
pcache[packetID] = computer.uptime()+cfg.pctime
end
end
end
@ -169,14 +193,14 @@ function start()
event.listen("modem_message",processPacket)
print("Started packet listening daemon: "..tostring(processPacket))
local function queuePacket(_,ptype,to,vport,data,npID)
local function queuePacket(_,ptype,to,vPort,data,npID)
npID = npID or genPacketID()
if to == hostname or to == "localhost" then
computer.pushSignal("net_msg",to,vport,data)
computer.pushSignal("net_msg",to,vPort,data)
computer.pushSignal("net_ack",npID)
return
end
pqueue[npID] = {ptype,to,vport,data,0,0}
pqueue[npID] = {ptype,to,vPort,data,0,0}
dprint(npID,table.unpack(pqueue[npID]))
end
@ -189,10 +213,10 @@ function start()
if v[5] < computer.uptime() then
dprint(k,v[1],v[2],hostname,v[3],v[4])
sendPacket(k,v[1],v[2],hostname,v[3],v[4])
if v[1] ~= 1 or v[6] == retrycount then
if v[1] ~= 1 or v[6] == cfg.retrycount then
pqueue[k] = nil
else
pqueue[k][5]=computer.uptime()+retry
pqueue[k][5]=computer.uptime()+cfg.retry
pqueue[k][6]=pqueue[k][6]+1
end
end
@ -218,31 +242,31 @@ function stop()
end
function debug()
dbug = not dbug
cfg.debug = not cfg.debug
end
function set_retry(sn)
retry = tonumber(sn) or 30
print("retry = "..tostring(retry))
cfg.retry = tonumber(sn) or 30
print("retry = "..tostring(cfg.retry))
end
function set_retrycount(sn)
retrycount = tonumber(sn) or 64
print("retrycount = "..tostring(retrycount))
cfg.retrycount = tonumber(sn) or 64
print("retrycount = "..tostring(cfg.retrycount))
end
function set_pctime(sn)
pctime = tonumber(sn) or 30
print("pctime = "..tostring(pctime))
cfg.pctime = tonumber(sn) or 30
print("pctime = "..tostring(cfg.pctime))
end
function set_rctime(sn)
rctime = tonumber(sn) or 30
print("rctime = "..tostring(rctime))
cfg.rctime = tonumber(sn) or 30
print("rctime = "..tostring(cfg.rctime))
end
function set_port(sn)
port = tonumber(sn) or 4096
print("port = "..tostring(port))
cfg.port = tonumber(sn) or 4096
print("port = "..tostring(cfg.port))
end
function set_route(to,laddr,raddr)
sroutes[to] = {laddr,raddr,0}
cfg.sroutes[to] = {laddr,raddr,0}
end
function del_route(to)
sroutes[to] = nil
cfg.sroutes[to] = nil
end