3 var rpcRequestRegistry = {},
4 rpcRequestBatch = null,
6 rpcSessionID = L.env.sessionid || '00000000000000000000000000000000',
7 rpcBaseURL = L.url('admin/ubus');
9 return L.Class.extend({
10 call: function(req, cbFn) {
11 var cb = cbFn.bind(this, req),
14 if (Array.isArray(req)) {
16 return Promise.resolve([]);
18 for (var i = 0; i < req.length; i++)
19 q += '%s%s.%s'.format(
26 q += '/%s.%s'.format(req.params[1], req.params[2]);
29 return L.Request.post(rpcBaseURL + q, req, {
30 timeout: (L.env.rpctimeout || 5) * 1000,
35 handleListReply: function(req, msg) {
36 var list = msg.result;
38 /* verify message frame */
39 if (typeof(msg) != 'object' || msg.jsonrpc != '2.0' || !msg.id || !Array.isArray(list))
45 handleCallReply: function(reqs, res) {
46 var type = Object.prototype.toString,
51 L.error('RPCError', 'RPC call failed with HTTP error %d: %s',
52 res.status, res.statusText || '?');
56 if (!Array.isArray(reqs)) {
61 for (var i = 0; i < msg.length; i++) {
62 /* fetch related request info */
63 var req = rpcRequestRegistry[reqs[i].id];
64 if (typeof(req) != 'object')
65 throw 'No related request for JSON response';
67 /* fetch response attribute and verify returned type */
70 /* verify message frame */
71 if (typeof(msg[i]) == 'object' && msg[i].jsonrpc == '2.0') {
72 if (typeof(msg[i].error) == 'object' && msg[i].error.code && msg[i].error.message)
73 req.reject(new Error('RPC call failed with error %d: %s'
74 .format(msg[i].error.code, msg[i].error.message || '?')));
75 else if (Array.isArray(msg[i].result) && msg[i].result[0] == 0)
76 ret = (msg[i].result.length > 1) ? msg[i].result[1] : msg[i].result[0];
79 req.reject(new Error('Invalid message frame received'));
83 for (var key in req.expect) {
84 if (ret != null && key != '')
87 if (ret == null || type.call(ret) != type.call(req.expect[key]))
88 ret = req.expect[key];
95 if (typeof(req.filter) == 'function') {
97 req.priv[1] = req.params;
98 ret = req.filter.apply(this, req.priv);
103 /* store response data */
104 if (typeof(req.index) == 'number')
105 data[req.index] = ret;
109 /* delete request object */
110 delete rpcRequestRegistry[reqs[i].id];
113 return Promise.resolve(data);
121 params: arguments.length ? this.varargs(arguments) : undefined
124 return this.call(msg, this.handleListReply);
128 if (!Array.isArray(rpcRequestBatch))
129 rpcRequestBatch = [ ];
133 if (!Array.isArray(rpcRequestBatch))
134 return Promise.resolve([]);
136 var req = rpcRequestBatch;
137 rpcRequestBatch = null;
140 return this.call(req, this.handleCallReply);
143 declare: function(options) {
144 return Function.prototype.bind.call(function(rpc, options) {
145 var args = this.varargs(arguments, 2);
146 return new Promise(function(resolveFn, rejectFn) {
147 /* build parameter object */
150 if (Array.isArray(options.params))
151 for (p_off = 0; p_off < options.params.length; p_off++)
152 params[options.params[p_off]] = args[p_off];
154 /* all remaining arguments are private args */
155 var priv = [ undefined, undefined ];
156 for (; p_off < args.length; p_off++)
157 priv.push(args[p_off]);
159 /* store request info */
160 var req = rpcRequestRegistry[rpcRequestID] = {
161 expect: options.expect,
162 filter: options.filter,
169 /* build message object */
182 /* when a batch is in progress then store index in request data
183 * and push message object onto the stack */
184 if (Array.isArray(rpcRequestBatch))
185 req.index = rpcRequestBatch.push(msg) - 1;
189 rpc.call(msg, rpc.handleCallReply);
191 }, this, this, options);
194 getSessionID: function() {
198 setSessionID: function(sid) {
202 getBaseURL: function() {
206 setBaseURL: function(url) {