GSoC: Add LuCId RPC-Slave
[project/luci.git] / libs / lucid-rpc / luasrc / lucid / rpc.lua
1 --[[
2 LuCI - Lua Development Framework
3
4 Copyright 2009 Steven Barth <steven@midlink.org>
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 $Id$
13 ]]
14
15 local require, ipairs, pcall = require, ipairs, pcall
16 local srv = require "luci.lucid.rpc.server"
17
18 module "luci.lucid.rpc"
19
20
21 function factory(publisher)
22         local root = srv.Module()
23         local server = srv.Server(root)
24
25         for _, r in ipairs(publisher) do
26                 for _, m in ipairs(r.export) do
27                         local s, mod = pcall(require, r.namespace .. "." .. m)
28                         if s and mod then
29                                 local module = mod._factory()
30                                 
31                                 if r.exec then
32                                         for _, x in ipairs(r.exec) do
33                                                 if x:sub(1,1) == ":" then
34                                                         module:restrict({interface = x:sub(2)})
35                                                 else
36                                                         module:restrict({user = x})
37                                                 end
38                                         end
39                                 end
40                                 
41                                 root:add(m, module)
42                         else
43                                 return nil, mod
44                         end
45                 end
46         end
47
48         return function(...) return server:process(...) end
49 end