summaryrefslogtreecommitdiffhomepage
path: root/applications/luci-olsr/luasrc/controller/olsr.lua
blob: d795dc4ed69fc8fac345da1239db4b82699bc934 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module("luci.controller.olsr", package.seeall)

function index()
	if not luci.fs.isfile("/etc/config/olsr") then
		return
	end
	
	local page  = node("admin", "status", "olsr")
	page.target = call("action_index")
	page.title  = "OLSR"
	
	local page  = node("admin", "status", "olsr", "routes")
	page.target = call("action_routes")
	page.title  = "Routen"
	page.order  = 10
	
	local page  = node("admin", "status", "olsr", "topology")
	page.target = call("action_topology")
	page.title  = "Topologie"
	page.order  = 20
	
	local page  = node("admin", "status", "olsr", "hna")
	page.target = call("action_hna")
	page.title  = "HNA"
	page.order  = 30
	
	local page  = node("admin", "status", "olsr", "mid")
	page.target = call("action_mid")
	page.title  = "MID"
	page.order  = 50

	entry({"admin", "services", "olsrd"}, cbi("olsr/olsrd"), "OLSR")
end

function action_index()
	local data = fetch_txtinfo("links")
	
	if not data or not data.Links then
		luci.template.render("status-olsr/error_olsr")
		return nil
	end
	
	local function compare(a, b)
		if tonumber(a.ETX) == 0 then
			return false
		end
		
		if tonumber(b.ETX) == 0 then
			return true
		end
		
		return tonumber(a.ETX) < tonumber(b.ETX)
	end
	
	table.sort(data.Links, compare)
	
	luci.template.render("status-olsr/index", {links=data.Links})
end

function action_routes()
	local data = fetch_txtinfo("routes")
	
	if not data or not data.Routes then
		luci.template.render("status-olsr/error_olsr")
		return nil
	end
	
	local function compare(a, b)
		if tonumber(a.ETX) == 0 then
			return false
		end
		
		if tonumber(b.ETX) == 0 then
			return true
		end
		
		return tonumber(a.ETX) < tonumber(b.ETX)
	end
	
	table.sort(data.Routes, compare)
	
	luci.template.render("status-olsr/routes", {routes=data.Routes})
end

function action_topology()
	local data = fetch_txtinfo("topology")
	
	if not data or not data.Topology then
		luci.template.render("status-olsr/error_olsr")
		return nil
	end
	
	local function compare(a, b)
		return a["Destination IP"] < b["Destination IP"]
	end
	
	table.sort(data.Topology, compare)
	
	luci.template.render("status-olsr/topology", {routes=data.Topology})
end

function action_hna()
	local data = fetch_txtinfo("hna")
	
	if not data or not data.HNA then
		luci.template.render("status-olsr/error_olsr")
		return nil
	end
	
	local function compare(a, b)
		return a.Network < b.Network
	end
	
	table.sort(data.HNA, compare)
	
	luci.template.render("status-olsr/hna", {routes=data.HNA})
end

function action_mid()
	local data = fetch_txtinfo("mid")
	
	if not data or not data.MID then
		luci.template.render("status-olsr/error_olsr")
		return nil
	end
	
	local function compare(a, b)
		return a.IP < b.IP
	end
	
	table.sort(data.MID, compare)
	
	luci.template.render("status-olsr/mid", {mids=data.MID})
end


-- Internal
function fetch_txtinfo(otable)
	require("luci.sys")
	otable = otable or ""
	local rawdata = luci.sys.httpget("http://127.0.0.1:2006/"..otable)
	
	if #rawdata == 0 then
		return nil
	end
	
	local data = {}
	
	local tables = luci.util.split(luci.util.trim(rawdata), "\n\n")
	

	for i, tbl in ipairs(tables) do
		local lines = luci.util.split(tbl, "\n")
		local name  = table.remove(lines, 1):sub(8)
		local keys  = luci.util.split(table.remove(lines, 1), "\t")
		
		data[name] = {}
		
		for j, line in ipairs(lines) do
			local fields = luci.util.split(line, "\t")
			data[name][j] = {}
			for k, key in pairs(keys) do
				data[name][j][key] = fields[k] 
			end
		end
	end
	
	return data
end