Show
Ignore:
Timestamp:
10/15/09 17:30:17 (4 years ago)
Author:
jow
Message:

libs/core: implement special treatment of wireless in network model

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • luci/trunk/libs/core/luasrc/model/network.lua

    r5391 r5405  
    1818]]-- 
    1919 
    20 local type, pairs, ipairs, table, i18n 
    21     = type, pairs, ipairs, table, luci.i18n 
     20local type, pairs, ipairs, loadfile, table, i18n 
     21    = type, pairs, ipairs, loadfile, table, luci.i18n 
    2222 
    2323local lmo = require "lmo" 
     
    3131module "luci.model.network" 
    3232 
     33-- load extensions 
     34local ext 
     35local handler = { } 
     36 
     37for ext in nfs.glob(utl.libpath() .. "/model/network/*.lua") do 
     38    if nfs.access(ext) then 
     39        local m = loadfile(ext) 
     40        if m then 
     41            handler[#handler+1] = m() 
     42        end 
     43    end 
     44end 
     45 
     46function foreach_handler(code, ...) 
     47    local h 
     48    for _, h in ipairs(handler) do 
     49        if code(h, ...) then 
     50            return true 
     51        end 
     52    end 
     53    return false 
     54end 
    3355 
    3456local ub = uct.bind("network") 
     
    4466        brs = { } 
    4567        sws = { } 
     68 
     69        -- init handler 
     70        foreach_handler(function(h) 
     71            h:init(cursor) 
     72            h:find_interfaces(ifs, brs) 
     73        end) 
    4674 
    4775        -- read interface information 
     
    76104                end 
    77105            end 
    78         end      
     106        end 
    79107 
    80108        -- read bridge informaton 
     
    151179                end 
    152180            end) 
     181 
     182        foreach_handler(function(h) h:del_network(n) end) 
    153183    end 
    154184    return r 
     
    180210                    end 
    181211                end) 
     212 
     213            foreach_handler(function(h) h:rename_network(old, new) end) 
    182214        end 
    183215    end 
     
    199231 
    200232function ignore_interface(self, x) 
    201     return (x:match("^wmaster%d") or x:match("^wifi%d") 
    202         or x:match("^hwsim%d") or x:match("^imq%d") or x == "lo") 
     233    if foreach_handler(function(h) return h:ignore_interface(x) end) then 
     234        return true 
     235    else 
     236        return (x:match("^wmaster%d") or x:match("^wifi%d") 
     237            or x:match("^hwsim%d") or x:match("^imq%d") or x == "lo") 
     238    end 
    203239end 
    204240 
     
    219255 
    220256function network.add_interface(self, ifname) 
     257    local ifaces, iface 
     258 
    221259    if type(ifname) ~= "string" then 
    222         ifname = ifname:name() 
    223     end 
    224     if ifs[ifname] then 
    225         self:ifname(ub:list((self:ifname() or ''), ifname)) 
     260        ifaces = { ifname:name() } 
     261    else 
     262        ifaces = ub:list(ifname) 
     263    end 
     264 
     265    for _, iface in ipairs(ifaces) do 
     266        if ifs[iface] then 
     267            -- make sure the interface is removed from all networks 
     268            local i = interface(iface) 
     269            local n = i:get_network() 
     270            if n then n:del_interface(iface) end 
     271 
     272            if ifs[iface].handler then 
     273                ifs[iface].handler:add_interface(self, iface, ifs[iface]) 
     274            else 
     275                self:ifname(ub:list((self:ifname() or ''), iface)) 
     276            end 
     277        end 
    226278    end 
    227279end 
     
    231283        ifname = ifname:name() 
    232284    end 
    233     self:ifname(ub:list((self:ifname() or ''), nil, ifname)) 
     285 
     286    if ifs[ifname] and ifs[ifname].handler then 
     287        ifs[ifname].handler:del_interface(self, ifname, ifs[ifname]) 
     288    else 
     289        self:ifname(ub:list((self:ifname() or ''), nil, ifname)) 
     290    end 
    234291end 
    235292 
     
    243300        end 
    244301    end 
     302    for iface, _ in pairs(ifs) do 
     303        if ifs[iface].network == self:name() then 
     304            ifaces[#ifaces+1] = interface(iface) 
     305        end 
     306    end 
    245307    return ifaces 
    246308end 
     
    256318    for _, i in ipairs(ifaces) do 
    257319        if i == iface then 
     320            return true 
     321        end 
     322    end 
     323 
     324    for i, _ in pairs(ifs) do 
     325        if ifs[i].dev and ifs[i].dev.network == self:name() then 
    258326            return true 
    259327        end 
     
    290358 
    291359function interface.type(self) 
    292     if iwi.type(self.ifname) and iwi.type(self.ifname) ~= "dummy" then 
    293         return "wifi" 
     360    if self.dev and self.dev.type then 
     361        return self.dev.type 
    294362    elseif brs[self.ifname] then 
    295363        return "bridge" 
     
    298366    else 
    299367        return "ethernet" 
     368    end 
     369end 
     370 
     371function interface.shortname(self) 
     372    if self.dev and self.dev.handler then 
     373        return self.dev.handler:shortname(self) 
     374    else 
     375        return self.ifname 
     376    end 
     377end 
     378 
     379function interface.get_i18n(self) 
     380    if self.dev and self.dev.handler then 
     381        return self.dev.handler:get_i18n(self) 
     382    else 
     383        return "%s: %q" %{ self:get_type_i18n(), self:name() } 
    300384    end 
    301385end 
     
    374458 
    375459function interface.get_network(self) 
     460    if self.dev and self.dev.network then 
     461        self.network = _M:get_network(self.dev.network) 
     462    end 
     463 
    376464    if not self.network then 
    377465        local net