(no commit message)
[project/luci.git] / module / public-core / src / controller / public / olsr.lua
1 module("ffluci.controller.public.olsr", package.seeall)
2 require("ffluci.sys")
3
4 function action_index()
5         local data = fetch_txtinfo("links")
6         
7         if not data or not data.Links then
8                 ffluci.template.render("public_olsr/error_olsr")
9                 return nil
10         end
11         
12         local function compare(a, b)
13                 if tonumber(a.ETX) == 0 then
14                         return false
15                 end
16                 
17                 if tonumber(b.ETX) == 0 then
18                         return true
19                 end
20                 
21                 return tonumber(a.ETX) < tonumber(b.ETX)
22         end
23         
24         table.sort(data.Links, compare)
25         
26         ffluci.template.render("public_olsr/index", {links=data.Links})
27 end
28
29 function action_routes()
30         local data = fetch_txtinfo("routes")
31         
32         if not data or not data.Routes then
33                 ffluci.template.render("public_olsr/error_olsr")
34                 return nil
35         end
36         
37         local function compare(a, b)
38                 if tonumber(a.ETX) == 0 then
39                         return false
40                 end
41                 
42                 if tonumber(b.ETX) == 0 then
43                         return true
44                 end
45                 
46                 return tonumber(a.ETX) < tonumber(b.ETX)
47         end
48         
49         table.sort(data.Routes, compare)
50         
51         ffluci.template.render("public_olsr/routes", {routes=data.Routes})
52 end
53
54 function action_topology()
55         local data = fetch_txtinfo("topology")
56         
57         if not data or not data.Topology then
58                 ffluci.template.render("public_olsr/error_olsr")
59                 return nil
60         end
61         
62         local function compare(a, b)
63                 return a["Destination IP"] < b["Destination IP"]
64         end
65         
66         table.sort(data.Topology, compare)
67         
68         ffluci.template.render("public_olsr/topology", {routes=data.Topology})
69 end
70
71 function action_hna()
72         local data = fetch_txtinfo("hna")
73         
74         if not data or not data.HNA then
75                 ffluci.template.render("public_olsr/error_olsr")
76                 return nil
77         end
78         
79         local function compare(a, b)
80                 return a.Network < b.Network
81         end
82         
83         table.sort(data.HNA, compare)
84         
85         ffluci.template.render("public_olsr/hna", {routes=data.HNA})
86 end
87
88 function action_mid()
89         local data = fetch_txtinfo("mid")
90         
91         if not data or not data.MID then
92                 ffluci.template.render("public_olsr/error_olsr")
93                 return nil
94         end
95         
96         local function compare(a, b)
97                 return a.IP < b.IP
98         end
99         
100         table.sort(data.MID, compare)
101         
102         ffluci.template.render("public_olsr/mid", {mids=data.MID})
103 end
104
105
106 -- Internal
107 function fetch_txtinfo(otable)
108         otable = otable or ""
109         local rawdata = ffluci.sys.httpget("http://127.0.0.1:2006/"..otable)
110         
111         if #rawdata == 0 then
112                 return nil
113         end
114         
115         local data = {}
116         
117         local tables = ffluci.util.split(ffluci.util.trim(rawdata), "\n\n")
118         
119
120         for i, tbl in ipairs(tables) do
121                 local lines = ffluci.util.split(tbl, "\n")
122                 local name  = table.remove(lines, 1):sub(8)
123                 local keys  = ffluci.util.split(table.remove(lines, 1), "\t")
124                 
125                 data[name] = {}
126                 
127                 for j, line in ipairs(lines) do
128                         local fields = ffluci.util.split(line, "\t")
129                         data[name][j] = {}
130                         for k, key in pairs(keys) do
131                                 data[name][j][key] = fields[k] 
132                         end
133                 end
134         end
135         
136         return data
137 end