forked from izaya/OC-PsychOS2
more cross-platform improvements
This commit is contained in:
parent
638092b4cc
commit
59abb31fee
@ -10,8 +10,8 @@ sender: original sender of packet
|
|||||||
data: the actual packet data, duh.
|
data: the actual packet data, duh.
|
||||||
]]--
|
]]--
|
||||||
|
|
||||||
local listeners = {}
|
local listeners,timers,processes,modems = {},{},{},{}
|
||||||
local modems = {}
|
local hostname = os.getenv("HOSTNAME")
|
||||||
|
|
||||||
local cfg = {}
|
local cfg = {}
|
||||||
cfg.debug = false
|
cfg.debug = false
|
||||||
@ -20,7 +20,21 @@ cfg.retry = 10
|
|||||||
cfg.retrycount = 64
|
cfg.retrycount = 64
|
||||||
cfg.route = true
|
cfg.route = true
|
||||||
|
|
||||||
local hostname = computer.address():sub(1,8)
|
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
|
-- packet cache: [packet ID]=uptime
|
||||||
local pcache = {}
|
local pcache = {}
|
||||||
@ -52,37 +66,27 @@ packet queue format:
|
|||||||
]]--
|
]]--
|
||||||
local pqueue = {}
|
local pqueue = {}
|
||||||
|
|
||||||
local function loadconfig()
|
|
||||||
end
|
|
||||||
local function saveconfig()
|
local function saveconfig()
|
||||||
end
|
if OPENOS or PSYCHOS then
|
||||||
|
local f = io.open(cfgpath,"wb")
|
||||||
-- specific OS support here
|
|
||||||
if _OSVERSION:sub(1,6) == "OpenOS" then -- OpenOS specific code
|
|
||||||
local timers = {}
|
|
||||||
|
|
||||||
local event = require "event"
|
|
||||||
local component = require "component"
|
|
||||||
local computer = require "computer"
|
|
||||||
local serial = require "serialization"
|
|
||||||
local listener = false
|
|
||||||
|
|
||||||
local function saveconfig()
|
|
||||||
local f = io.open("/etc/minitel.cfg","wb")
|
|
||||||
if f then
|
if f then
|
||||||
f:write(serial.serialize(cfg))
|
f:write(serial.serialize(cfg))
|
||||||
f:close()
|
f:close()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
local function loadconfig()
|
local function loadconfig()
|
||||||
local f=io.open("/etc/hostname","rb")
|
hostname = os.getenv("HOSTNAME") or computer.address():sub(1,8)
|
||||||
|
if OPENOS or PSYCHOS then
|
||||||
|
print("Opening hostname file.",hnpath)
|
||||||
|
local f,g=io.open(hnpath,"rb")
|
||||||
if f then
|
if f then
|
||||||
hostname = f:read()
|
hostname = f:read("*a"):match("(.-)\n")
|
||||||
f:close()
|
f:close()
|
||||||
end
|
end
|
||||||
local f = io.open("/etc/minitel.cfg","rb")
|
local f = io.open(cfgpath,"rb")
|
||||||
if f then
|
if f then
|
||||||
local newcfg = serial.unserialize(f:read("*a"))
|
local newcfg = serial.unserialize(f:read("*a")) or {}
|
||||||
f:close()
|
f:close()
|
||||||
for k,v in pairs(newcfg) do
|
for k,v in pairs(newcfg) do
|
||||||
cfg[k] = v
|
cfg[k] = v
|
||||||
@ -90,40 +94,32 @@ if _OSVERSION:sub(1,6) == "OpenOS" then -- OpenOS specific code
|
|||||||
else
|
else
|
||||||
saveconfig()
|
saveconfig()
|
||||||
end
|
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
|
||||||
function stop()
|
|
||||||
for k,v in pairs(listeners) do
|
|
||||||
event.ignore(k,v)
|
|
||||||
print("Stopped listener: "..tostring(v))
|
|
||||||
end
|
|
||||||
for k,v in pairs(timers) do
|
|
||||||
event.cancel(v)
|
|
||||||
print("Stopped timer: "..tostring(v))
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function set(k,v)
|
-- specific OS support here
|
||||||
if type(cfg[k]) == "string" then
|
if PSYCHOS then -- PsychOS specific code
|
||||||
cfg[k] = v
|
serial = require "serialization"
|
||||||
elseif type(cfg[k]) == "number" then
|
elseif OPENOS then -- OpenOS specific code
|
||||||
cfg[k] = tonumber(v)
|
event = require "event"
|
||||||
elseif type(cfg[k]) == "boolean" then
|
component = require "component"
|
||||||
if v:lower():sub(1,1) == "t" then
|
computer = require "computer"
|
||||||
cfg[k] = true
|
serial = require "serialization"
|
||||||
else
|
listener = false
|
||||||
cfg[k] = 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
|
end
|
||||||
print("cfg."..k.." = "..tostring(cfg[k]))
|
|
||||||
saveconfig()
|
|
||||||
end
|
|
||||||
|
|
||||||
function set_route(to,laddr,raddr)
|
|
||||||
cfg.sroutes[to] = {laddr,raddr,0}
|
|
||||||
end
|
|
||||||
function del_route(to)
|
|
||||||
cfg.sroutes[to] = nil
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
local function dprint(...)
|
local function dprint(...)
|
||||||
@ -132,12 +128,11 @@ local function dprint(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function start()
|
function start()
|
||||||
loadconfig()
|
loadconfig()
|
||||||
print("Hostname: "..hostname)
|
print("Hostname: "..hostname)
|
||||||
if listener then return end
|
if listener then return end
|
||||||
modems={}
|
if OPENOS or PSYCHOS then
|
||||||
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
|
||||||
@ -148,6 +143,20 @@ function start()
|
|||||||
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 = ""
|
||||||
@ -263,22 +272,21 @@ function start()
|
|||||||
|
|
||||||
listeners["modem_message"]=processPacket
|
listeners["modem_message"]=processPacket
|
||||||
listeners["net_send"]=queuePacket
|
listeners["net_send"]=queuePacket
|
||||||
if _OSVERSION:sub(1,6) == "OpenOS" then
|
if OPENOS then
|
||||||
event.listen("modem_message",processPacket)
|
event.listen("modem_message",processPacket)
|
||||||
print("Started packet listening daemon: "..tostring(processPacket))
|
print("Started packet listening daemon: "..tostring(processPacket))
|
||||||
event.listen("net_send",queuePacket)
|
event.listen("net_send",queuePacket)
|
||||||
print("Started packet queueing daemon: "..tostring(queuePacket))
|
print("Started packet queueing daemon: "..tostring(queuePacket))
|
||||||
timers[#timers+1]=event.timer(0,packetPusher,math.huge)
|
timers[#timers+1]=event.timer(0,packetPusher,math.huge)
|
||||||
print("Started packet pusher: "..tostring(timers[#timers]))
|
print("Started packet pusher: "..tostring(timers[#timers]))
|
||||||
end
|
elseif KITTENOS then
|
||||||
if _OSVERSION:sub(1,8) == "KittenOS" then
|
|
||||||
neo.requireAccess("r.svc.minitel","minitel daemon")(function(pkg,pid,sendSig)
|
neo.requireAccess("r.svc.minitel","minitel daemon")(function(pkg,pid,sendSig)
|
||||||
processes[pid] = sendSig
|
processes[pid] = sendSig
|
||||||
return {["sendPacket"]=queuePacket}
|
return {["sendPacket"]=queuePacket}
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
if _OSVERSION:sub(1,8) == "KittenOS" or _OSVERSION:sub(1,7) == "PsychOS" then
|
if KITTENOS or PSYCHOS then
|
||||||
while true do
|
while true do
|
||||||
local ev = {coroutine.yield()}
|
local ev = {coroutine.yield()}
|
||||||
packetPusher()
|
packetPusher()
|
||||||
@ -293,6 +301,40 @@ function start()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if _OSVERSION:sub(1,6) ~= "OpenOS" then
|
function stop()
|
||||||
|
for k,v in pairs(listeners) do
|
||||||
|
event.ignore(k,v)
|
||||||
|
print("Stopped listener: "..tostring(v))
|
||||||
|
end
|
||||||
|
for k,v in pairs(timers) do
|
||||||
|
event.cancel(v)
|
||||||
|
print("Stopped timer: "..tostring(v))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
function set(k,v)
|
||||||
|
if type(cfg[k]) == "string" then
|
||||||
|
cfg[k] = v
|
||||||
|
elseif type(cfg[k]) == "number" then
|
||||||
|
cfg[k] = tonumber(v)
|
||||||
|
elseif type(cfg[k]) == "boolean" then
|
||||||
|
if v:lower():sub(1,1) == "t" then
|
||||||
|
cfg[k] = true
|
||||||
|
else
|
||||||
|
cfg[k] = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
print("cfg."..k.." = "..tostring(cfg[k]))
|
||||||
|
saveconfig()
|
||||||
|
end
|
||||||
|
|
||||||
|
function set_route(to,laddr,raddr)
|
||||||
|
cfg.sroutes[to] = {laddr,raddr,0}
|
||||||
|
end
|
||||||
|
function del_route(to)
|
||||||
|
cfg.sroutes[to] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if not OPENOS then
|
||||||
start()
|
start()
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user