GSoC: Add LuCId RPC-Slave
[project/luci.git] / libs / lucid-rpc / luasrc / lucid / rpc / ruci.lua
1 --[[
2 LuCIRPCd
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 local uci = require "luci.model.uci"
14 local tostring, getmetatable, pairs = tostring, getmetatable, pairs
15 local error, type = error, type
16 local nixio = require "nixio"
17 local srv = require "luci.lucid.rpc.server"
18
19 module "luci.lucid.rpc.ruci"
20
21 function _factory()
22         local m = srv.Module("Remote UCI API")
23         
24         for k, v in pairs(_M) do
25                 if type(v) == "function" and v ~= _factory then
26                         m:add(k, srv.Method.extended(v))
27                 end
28         end
29         
30         return m
31 end
32
33 local function getinst(session, name)
34         return session.ruci and session.ruci[name]
35 end
36
37 local function setinst(session, obj)
38         session.ruci = session.ruci or {}
39         local name = tostring(obj):match("0x([a-z0-9]+)")
40         session.ruci[name] = obj
41         return name
42 end
43
44 local Cursor = getmetatable(uci.cursor())
45
46 for name, func in pairs(Cursor) do
47         _M[name] = function(session, inst, ...)
48                 inst = getinst(session, inst)
49                 return inst[name](inst, ...)
50         end
51 end
52
53 function cursor(session, ...)
54         return setinst(session, uci.cursor(...))
55 end
56
57 function cursor_state(session, ...)
58         return setinst(session, uci.cursor_state(...))
59 end
60
61 function foreach(session, inst, config, sectiontype)
62         local inst = getinst(session, inst)
63         local secs = {}
64         inst:foreach(config, sectiontype, function(s) secs[#secs+1] = s end)
65         return secs
66 end