forked from izaya/OC-PsychOS2
replaced the minitel implementation with the OpenOS version, mostly
This commit is contained in:
parent
4606620583
commit
3c1e2d31ae
@ -10,36 +10,27 @@ sender: original sender of packet
|
|||||||
data: the actual packet data, duh.
|
data: the actual packet data, duh.
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
local listeners,timers,processes,modems = {},{},{},{}
|
local listeners = {}
|
||||||
local hostname = os.getenv("HOSTNAME")
|
local timers = {}
|
||||||
|
|
||||||
local cfg = {}
|
local cfg = {}
|
||||||
|
|
||||||
|
local event = require "event"
|
||||||
|
local component = require "component"
|
||||||
|
local computer = require "computer"
|
||||||
|
local serial = require "serialization"
|
||||||
|
|
||||||
|
local hostname = computer.address():sub(1,8)
|
||||||
|
local modems = {}
|
||||||
|
|
||||||
|
local pid = nil
|
||||||
|
|
||||||
cfg.debug = false
|
cfg.debug = false
|
||||||
cfg.port = 4096
|
cfg.port = 4096
|
||||||
cfg.retry = 10
|
cfg.retry = 10
|
||||||
cfg.retrycount = 64
|
cfg.retrycount = 3
|
||||||
cfg.route = true
|
cfg.route = true
|
||||||
|
|
||||||
local event, component, computer, serial = event, component, computer, serial
|
|
||||||
local hnpath, cfgpath = "", ""
|
|
||||||
|
|
||||||
OPENOS, PSYCHOS, KITTENOS = false, false, false
|
|
||||||
if _OSVERSION:sub(1,6) == "OpenOS" then
|
|
||||||
OPENOS = true
|
|
||||||
hnpath = "/etc/hostname"
|
|
||||||
cfgpath = "/etc/minitel.cfg"
|
|
||||||
elseif _OSVERSION:sub(1,7) == "PsychOS" then
|
|
||||||
PSYCHOS = true
|
|
||||||
hnpath = "/boot/cfg/hostname"
|
|
||||||
cfgpath = "/boot/cfg/minitel.cfg"
|
|
||||||
elseif _OSVERSION:sub(1,8) == "KittenOS" then
|
|
||||||
KITTENOS = true
|
|
||||||
end
|
|
||||||
|
|
||||||
-- packet cache: [packet ID]=uptime
|
|
||||||
local pcache = {}
|
|
||||||
cfg.pctime = 30
|
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
LKR format:
|
LKR format:
|
||||||
address {
|
address {
|
||||||
@ -48,7 +39,6 @@ address {
|
|||||||
time last received
|
time last received
|
||||||
}
|
}
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
cfg.sroutes = {}
|
cfg.sroutes = {}
|
||||||
local rcache = setmetatable({},{__index=cfg.sroutes})
|
local rcache = setmetatable({},{__index=cfg.sroutes})
|
||||||
cfg.rctime = 15
|
cfg.rctime = 15
|
||||||
@ -66,60 +56,9 @@ packet queue format:
|
|||||||
]]--
|
]]--
|
||||||
local pqueue = {}
|
local pqueue = {}
|
||||||
|
|
||||||
local function saveconfig()
|
-- packet cache: [packet ID]=uptime
|
||||||
if OPENOS or PSYCHOS then
|
local pcache = {}
|
||||||
local f = io.open(cfgpath,"wb")
|
cfg.pctime = 30
|
||||||
if f then
|
|
||||||
f:write(serial.serialize(cfg))
|
|
||||||
f:close()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local function loadconfig()
|
|
||||||
hostname = os.getenv("HOSTNAME") or computer.address():sub(1,8)
|
|
||||||
if OPENOS or PSYCHOS then
|
|
||||||
local f,g=io.open(hnpath,"rb")
|
|
||||||
if f then
|
|
||||||
hostname = f:read("*a"):match("(.-)\n")
|
|
||||||
f:close()
|
|
||||||
end
|
|
||||||
local f = io.open(cfgpath,"rb")
|
|
||||||
if f then
|
|
||||||
local newcfg = serial.unserialize(f:read("*a")) or {}
|
|
||||||
f:close()
|
|
||||||
for k,v in pairs(newcfg) do
|
|
||||||
cfg[k] = v
|
|
||||||
end
|
|
||||||
else
|
|
||||||
saveconfig()
|
|
||||||
end
|
|
||||||
elseif KITTENOS then
|
|
||||||
local globals = neo.requestAccess("x.neo.pub.globals") -- KittenOS standard hostname stuff
|
|
||||||
if globals then
|
|
||||||
hostname = globals.getSetting("hostname") or hostname
|
|
||||||
globals.setSetting("hostname",hostname)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- specific OS support here
|
|
||||||
if PSYCHOS then -- PsychOS specific code
|
|
||||||
serial = require "serialization"
|
|
||||||
elseif OPENOS then -- OpenOS specific code
|
|
||||||
event = require "event"
|
|
||||||
component = require "component"
|
|
||||||
computer = require "computer"
|
|
||||||
serial = require "serialization"
|
|
||||||
listener = false
|
|
||||||
elseif KITTENOS then
|
|
||||||
neo.requireAccess("s.h.modem_message","pulling packets")
|
|
||||||
computer = {["uptime"]=os.uptime,["address"]=os.address} -- wrap computer so the OpenOS code more or less works
|
|
||||||
function computer.pushSignal(...)
|
|
||||||
for k,v in pairs(processes) do
|
|
||||||
v(...)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function dprint(...)
|
local function dprint(...)
|
||||||
if cfg.debug then
|
if cfg.debug then
|
||||||
@ -127,35 +66,44 @@ local function dprint(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function saveconfig()
|
||||||
|
local f = io.open("/boot/cfg/minitel.cfg","wb")
|
||||||
|
if f then
|
||||||
|
f:write(serial.serialize(cfg))
|
||||||
|
f:close()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local function loadconfig()
|
||||||
|
local f = io.open("/boot/cfg/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()
|
loadconfig()
|
||||||
|
hostname = os.getenv("HOSTNAME") or computer.address():sub(1,8)
|
||||||
print("Hostname: "..hostname)
|
print("Hostname: "..hostname)
|
||||||
if listener then return end
|
|
||||||
if OPENOS or PSYCHOS then
|
if pid then return false end
|
||||||
|
|
||||||
|
modems={}
|
||||||
for a,t in component.list("modem") do
|
for a,t in component.list("modem") do
|
||||||
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(cfg.port)
|
v.open(cfg.port)
|
||||||
print("Opened port "..cfg.port.." on "..v.address:sub(1,8))
|
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)
|
||||||
end
|
end
|
||||||
elseif KITTENOS then
|
|
||||||
for p in neo.requireAccess("c.modem","networking").list() do -- fun stuff for KittenOS
|
|
||||||
dprint(p.address)
|
|
||||||
modems[p.address] = p
|
|
||||||
end
|
|
||||||
for k,v in pairs(modems) do
|
|
||||||
v.open(port)
|
|
||||||
print("Opened port "..port.." on "..v.address)
|
|
||||||
end
|
|
||||||
for p in neo.requireAccess("c.tunnel","networking").list() do
|
|
||||||
dprint(p.address)
|
|
||||||
modems[p.address] = p
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local function genPacketID()
|
local function genPacketID()
|
||||||
local npID = ""
|
local npID = ""
|
||||||
@ -176,10 +124,11 @@ function start()
|
|||||||
else
|
else
|
||||||
dprint("Not cached", cfg.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
|
||||||
|
-- do not send message back to the wired or linked modem it came from
|
||||||
|
-- the check for tunnels is for short circuiting `v.isWireless()`, which does not exist for tunnels
|
||||||
if v.address ~= repeatingFrom or (v.type ~= "tunnel" and v.isWireless()) then
|
if v.address ~= repeatingFrom or (v.type ~= "tunnel" and v.isWireless()) then
|
||||||
if v.type == "modem" then
|
if v.type == "modem" then
|
||||||
v.broadcast(cfg.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
|
end
|
||||||
@ -255,7 +204,6 @@ function start()
|
|||||||
dprint(npID,table.unpack(pqueue[npID]))
|
dprint(npID,table.unpack(pqueue[npID]))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local function packetPusher()
|
local function packetPusher()
|
||||||
for k,v in pairs(pqueue) do
|
for k,v in pairs(pqueue) do
|
||||||
if v[5] < computer.uptime() then
|
if v[5] < computer.uptime() then
|
||||||
@ -273,43 +221,29 @@ function start()
|
|||||||
|
|
||||||
listeners["modem_message"]=processPacket
|
listeners["modem_message"]=processPacket
|
||||||
listeners["net_send"]=queuePacket
|
listeners["net_send"]=queuePacket
|
||||||
if OPENOS then
|
listeners["net_ack"]=dprint
|
||||||
event.listen("modem_message",processPacket)
|
|
||||||
print("Started packet listening daemon: "..tostring(processPacket))
|
|
||||||
event.listen("net_send",queuePacket)
|
|
||||||
print("Started packet queueing daemon: "..tostring(queuePacket))
|
|
||||||
timers[#timers+1]=event.timer(0,packetPusher,math.huge)
|
|
||||||
print("Started packet pusher: "..tostring(timers[#timers]))
|
|
||||||
elseif KITTENOS then
|
|
||||||
neo.requireAccess("r.svc.minitel","minitel daemon")(function(pkg,pid,sendSig)
|
|
||||||
processes[pid] = sendSig
|
|
||||||
return {["sendPacket"]=queuePacket}
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
if KITTENOS or PSYCHOS then
|
pid=os.spawn(function()
|
||||||
while true do
|
while true do
|
||||||
local ev = {coroutine.yield()}
|
local ev = {coroutine.yield()}
|
||||||
packetPusher()
|
packetPusher()
|
||||||
pruneCache()
|
pruneCache()
|
||||||
if ev[1] == "k.procdie" then
|
|
||||||
processes[ev[3]] = nil
|
|
||||||
end
|
|
||||||
if listeners[ev[1]] then
|
if listeners[ev[1]] then
|
||||||
pcall(listeners[ev[1]],table.unpack(ev))
|
pcall(listeners[ev[1]],table.unpack(ev))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end,"minitel")
|
||||||
|
print("Started Minitel daemon: "..tostring(pid))
|
||||||
|
return pid
|
||||||
end
|
end
|
||||||
|
|
||||||
function stop()
|
function stop()
|
||||||
for k,v in pairs(listeners) do
|
if pid then
|
||||||
event.ignore(k,v)
|
os.kill(pid)
|
||||||
print("Stopped listener: "..tostring(v))
|
pid = nil
|
||||||
end
|
return true
|
||||||
for k,v in pairs(timers) do
|
else
|
||||||
event.cancel(v)
|
return false
|
||||||
print("Stopped timer: "..tostring(v))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -331,11 +265,9 @@ end
|
|||||||
|
|
||||||
function set_route(to,laddr,raddr)
|
function set_route(to,laddr,raddr)
|
||||||
cfg.sroutes[to] = {laddr,raddr,0}
|
cfg.sroutes[to] = {laddr,raddr,0}
|
||||||
|
saveconfig()
|
||||||
end
|
end
|
||||||
function del_route(to)
|
function del_route(to)
|
||||||
cfg.sroutes[to] = nil
|
cfg.sroutes[to] = nil
|
||||||
end
|
saveconfig()
|
||||||
|
|
||||||
if not OPENOS then
|
|
||||||
start()
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user