* Rewrote ffluci.util.split (optimizations)
[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()
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()
31         
32         if not data or not data.Links 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()
56         
57         if not data or not data.Links 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()
73         
74         if not data or not data.Links 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()
90         
91         if not data or not data.Links 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()
108         local rawdata = ffluci.sys.httpget("http://127.0.0.1:2006")
109         
110         if #rawdata == 0 then
111                 return nil
112         end
113         
114         local data = {}
115         
116         local tables = ffluci.util.split(ffluci.util.trim(rawdata), "\n\n")
117         
118
119         for i, tbl in ipairs(tables) do
120                 local lines = ffluci.util.split(tbl, "\n")
121                 local name  = table.remove(lines, 1):sub(8)
122                 local keys  = ffluci.util.split(table.remove(lines, 1), "\t")
123                 
124                 data[name] = {}
125                 
126                 for j, line in ipairs(lines) do
127                         local fields = ffluci.util.split(line, "\t")
128                         data[name][j] = {}
129                         for k, key in pairs(keys) do
130                                 data[name][j][key] = fields[k] 
131                         end
132                 end
133         end
134         
135         return data
136 end