diff options
author | Jo-Philipp Wich <jo@mein.io> | 2018-07-18 14:43:27 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2018-07-18 14:43:27 +0200 |
commit | 9b4efaefa1b4c94a7d976c8d65169bf056032e09 (patch) | |
tree | b76fde8eeb5fe85df6e0dff1306134ea0ae17043 /modules/luci-mod-admin-full/luasrc/controller/admin | |
parent | 68dae07225c44ca43aec7d4826ebc954d0c9ef10 (diff) |
luci-mod-admin-full: use incremental background scanning for wireless join
The previous approach of synchroneously scanning while building the result
page was suboptimal since it frequently led to connection resets when
accessing LuCI via wireless.
It also exhibited problems when accessed via SSL on recent Firefox versions
where the page were only loaded partially.
Rework the wireless scanning to gather scan results in a background process
and put them into the ubus session data area where they can be readily
accessed without causing network interruptions.
Subsequently rebuild the wireless join page to use XHR polling to
incrementally fetch updated scan results.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'modules/luci-mod-admin-full/luasrc/controller/admin')
-rw-r--r-- | modules/luci-mod-admin-full/luasrc/controller/admin/network.lua | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/modules/luci-mod-admin-full/luasrc/controller/admin/network.lua b/modules/luci-mod-admin-full/luasrc/controller/admin/network.lua index 31b9416253..c45605a983 100644 --- a/modules/luci-mod-admin-full/luasrc/controller/admin/network.lua +++ b/modules/luci-mod-admin-full/luasrc/controller/admin/network.lua @@ -58,6 +58,12 @@ function index() page = entry({"admin", "network", "wireless_reconnect"}, post("wifi_reconnect"), nil) page.leaf = true + page = entry({"admin", "network", "wireless_scan_trigger"}, post("wifi_scan_trigger"), nil) + page.leaf = true + + page = entry({"admin", "network", "wireless_scan_results"}, call("wifi_scan_results"), nil) + page.leaf = true + page = entry({"admin", "network", "wireless"}, arcombine(cbi("admin_network/wifi_overview"), cbi("admin_network/wifi")), _("Wireless"), 15) page.leaf = true page.subindex = true @@ -309,6 +315,78 @@ function wifi_assoclist() luci.http.write_json(s.wifi_assoclist()) end + +local function _wifi_get_scan_results(cache_key) + local results = luci.util.ubus("session", "get", { + ubus_rpc_session = luci.model.uci:get_session_id(), + keys = { cache_key } + }) + + if type(results) == "table" and + type(results.values) == "table" and + type(results.values[cache_key]) == "table" + then + return results.values[cache_key] + end + + return { } +end + +function wifi_scan_trigger(radio, update) + local iw = radio and luci.sys.wifi.getiwinfo(radio) + + if not iw then + luci.http.status(404, "No such radio device") + return + end + + luci.http.status(200, "Scan scheduled") + + if nixio.fork() == 0 then + io.stderr:close() + io.stdout:close() + + local _, bss + local data, bssids = { }, { } + local cache_key = "scan_%s" % radio + + luci.util.ubus("session", "set", { + ubus_rpc_session = luci.model.uci:get_session_id(), + values = { [cache_key] = nil } + }) + + for _, bss in ipairs(iw.scanlist or { }) do + data[_] = bss + bssids[bss.bssid] = bss + end + + if update then + for _, bss in ipairs(_wifi_get_scan_results(cache_key)) do + if not bssids[bss.bssid] then + bss.stale = true + data[#data + 1] = bss + end + end + end + + luci.util.ubus("session", "set", { + ubus_rpc_session = luci.model.uci:get_session_id(), + values = { [cache_key] = data } + }) + end +end + +function wifi_scan_results(radio) + local results = radio and _wifi_get_scan_results("scan_%s" % radio) + + if results and #results > 0 then + luci.http.prepare_content("application/json") + luci.http.write_json(results) + else + luci.http.status(404, "No wireless scan results") + end +end + function lease_status() local s = require "luci.tools.status" |