opus/sys/services/network.lua

74 lines
1.4 KiB
Lua
Raw Normal View History

2017-10-23 23:33:53 +00:00
_G.requireInjector()
2016-12-11 19:24:52 +00:00
local Util = require('util')
2017-10-23 23:33:53 +00:00
local device = _G.device
local fs = _G.fs
local multishell = _ENV.multishell
local os = _G.os
local printError = _G.printError
local network = { }
_G.network = network
2016-12-11 19:24:52 +00:00
2017-10-23 23:33:53 +00:00
multishell.setTitle(multishell.getCurrent(), 'Net Daemon')
2016-12-11 19:24:52 +00:00
local function netUp()
2017-10-23 23:33:53 +00:00
_G.requireInjector()
2017-05-21 05:42:41 +00:00
2017-10-23 23:33:53 +00:00
local Event = require('event')
2017-10-26 22:56:55 +00:00
2017-09-28 22:52:57 +00:00
for _,file in pairs(fs.list('sys/network')) do
2017-10-23 23:33:53 +00:00
local fn, msg = Util.run(_ENV, 'sys/network/' .. file)
2017-09-28 22:52:57 +00:00
if not fn then
2016-12-11 19:24:52 +00:00
printError(msg)
end
end
2017-08-03 05:46:39 +00:00
Event.on('device_detach', function()
if not device.wireless_modem then
Event.exitPullEvents()
2016-12-11 19:24:52 +00:00
end
2017-08-03 05:46:39 +00:00
end)
Event.pullEvents()
for _,c in pairs(network) do
c.active = false
os.queueEvent('network_detach', c)
2016-12-11 19:24:52 +00:00
end
2017-08-03 05:46:39 +00:00
os.queueEvent('network_down')
Event.pullEvent('network_down')
2016-12-11 19:24:52 +00:00
Util.clear(_G.network)
end
print('Net daemon started')
local function startNetwork()
print('Starting network services')
local success, msg = Util.runFunction(
2017-10-23 23:33:53 +00:00
Util.shallowCopy(_ENV), netUp)
2016-12-11 19:24:52 +00:00
if not success and msg then
printError(msg)
end
print('Network services stopped')
end
if device.wireless_modem then
startNetwork()
else
print('No modem detected')
end
while true do
2017-10-23 23:33:53 +00:00
local _, deviceName = os.pullEvent('device_attach')
2016-12-11 19:24:52 +00:00
if deviceName == 'wireless_modem' then
startNetwork()
end
end
print('Net daemon stopped')