GSoC: Add RPC client
[project/luci.git] / libs / rpcc / luasrc / rpcc / ruci.lua
1 --[[
2 LuCIRPCc
3 (c) 2009 Steven Barth <steven@midlink.org>
4
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8
9         http://www.apache.org/licenses/LICENSE-2.0
10
11 $Id$
12 ]]--
13
14 local util = require "luci.util"
15 local rawget, setmetatable = rawget, setmetatable
16 local ipairs = ipairs
17
18 --- Transparent UCI over RPC client.
19 -- @cstyle instance
20 module "luci.rpcc.ruci"
21
22
23 local Proxy = util.class()
24
25 --- Create a new UCI over RPC proxy.
26 -- @param rpccl RPC client
27 -- @return Network transparent UCI module 
28 function factory(rpccl)
29         return {
30                 cursor = function(...)
31                         return Proxy(rpccl, rpccl:request("ruci.cursor", {...}))
32                 end,
33                 cursor_state = function(...)
34                         return Proxy(rpccl, rpccl:request("ruci.cursor_state", {...}))
35                 end
36         }
37 end
38
39 function Proxy.__init__(self, rpccl, objid)
40         self.__rpccl = rpccl
41         self.__objid = objid
42
43         setmetatable(self, {
44                 __index = function(self, key)
45                         return rawget(self, key) or Proxy[key] or function(self, ...)
46                                 local argv = {self.__objid, ...}
47                                 return self.__rpccl:request("ruci."..key, argv)
48                         end
49                 end
50         })
51 end
52
53 function Proxy.foreach(self, config, section, callback)
54         local sections = self.__rpccl:request("ruci.foreach", {self.__objid, config, section})
55         if sections then
56                 for _, s in ipairs(sections) do
57                         callback(s)
58                 end
59                 return true
60         else
61                 return false
62         end
63 end