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