From 8cb80181ecb9172ccd18d6f9f99eaad1a2ef377b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schr=C3=B6der?= Date: Fri, 15 Jan 2016 13:30:20 +0100 Subject: [PATCH] initial test using websockets --- juci-local-server | 21 +++++++++++++++++++-- juci/src/js/rpc.js | 31 ++++++++++++++++++++++++++++--- juci/src/js/rpc2.js | 13 +++++++++++++ tests/test-rpc2.js | 2 +- 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/juci-local-server b/juci-local-server index 724c071b..b5f4dcc7 100755 --- a/juci-local-server +++ b/juci-local-server @@ -7,7 +7,8 @@ var fs = require("fs"); var request = require("request"); var http = require("http"); var bodyParser = require('body-parser') -var config = { port: 3000, ubus_uri: "http://192.168.1.1/ubus" }; +var config = { port: 3000 }; +var exec = require("child_process").exec; //check if file exists and is a file if (fs.existsSync("juci-local-server.config")){ @@ -271,7 +272,22 @@ app.post('/ubus', function(req, res) { timedOut = true; sendResponse(body);*/ }, 5000); - + + var cmd = ""; + data.params[3].ubus_rpc_session = data.params[0]; + if(data.method == "call") cmd = "sudo ubus2 -s /tmp/router-ubus-json.sock "+data.method+" "+data.params[1]+" "+data.params[2]+" '"+JSON.stringify(data.params[3])+"'"; + else if(data.method == "list") cmd = "sudo ubus2 -s /tmp/router-ubus-json.sock list"; + console.log("command: "+cmd); + exec(cmd, function(error, stdout, stderr){ + console.log("Got stdout: "+stdout+", stderr: "+stderr); + var resp = JSON.parse(stdout); + sendResponse({ + jsonrpc:"2.0", + id: 1, + result: [0, resp[0]] + }); + }); + /* request({ url: config.ubus_uri, method: "POST", @@ -301,6 +317,7 @@ app.post('/ubus', function(req, res) { sendResponse(body); } }); + */ //console.log("Unknown RPC call "+name); //res.end(); } diff --git a/juci/src/js/rpc.js b/juci/src/js/rpc.js index 20d90219..74e7bd83 100644 --- a/juci/src/js/rpc.js +++ b/juci/src/js/rpc.js @@ -28,7 +28,9 @@ "local.features", "local.set_rpc_host" ]; - + + var connect = null; + function rpc_request(type, namespace, method, data){ var sid = ""; @@ -51,9 +53,31 @@ if(RPC_CACHE[k].deferred && RPC_CACHE[k].deferred.state() == "pending"){ retain[k] = RPC_CACHE[k]; } - }); + }); RPC_CACHE = retain; - + + data.ubus_rpc_session = RPC_SESSION_ID; + if(!connect) connect = $rpc2.$connect("ws://localhost:1234"); + connect.done(function(){ + if(type == "call"){ + $rpc2.$call(namespace, method, data).done(function(ret){ + var def = RPC_CACHE[key].deferred; + if(ret[0] && (ret[0].code != undefined || ret[0].error)){ + console.log("FAIL request: "+type+", object="+namespace+", method="+method+", data="+JSON.stringify(data)+", resp="+JSON.stringify(ret)); + def.reject(ret[0] || {}); + } + else { + console.log("request: "+type+", object="+namespace+", method="+method+", data="+JSON.stringify(data)+", resp="+JSON.stringify(ret)); + def.resolve(ret[0]); + } + }); + } else if(type == "list"){ + $rpc2.$list().done(function(ret){ + RPC_CACHE[key].deferred.resolve(ret[0] || {}); + }); + } + }); +/* // setup default rpcs $.jsonRPC.withOptions({ namespace: "", @@ -108,6 +132,7 @@ } }) }); + */ return RPC_CACHE[key].deferred.promise(); } diff --git a/juci/src/js/rpc2.js b/juci/src/js/rpc2.js index fb07bfcc..8bf0f941 100644 --- a/juci/src/js/rpc2.js +++ b/juci/src/js/rpc2.js @@ -98,6 +98,19 @@ } RPC.prototype.$list = function(){ + var self = this; + self.seq++; + var req = self.requests[self.seq] = { + id: self.seq, + deferred: $.Deferred() + }; + self.socket.send(JSON.stringify({ + jsonrpc: "2.0", + id: req.id, + method: "call", + params: ["/ubus/peer", "ubus.peer.list", {}] + })+"\n"); + return req.deferred.promise(); } diff --git a/tests/test-rpc2.js b/tests/test-rpc2.js index a17d52ce..e50b7142 100755 --- a/tests/test-rpc2.js +++ b/tests/test-rpc2.js @@ -20,7 +20,7 @@ $rpc2.$connect(params.host).done(function(){ var t = Date.now(); if(params.method == "list"){ - $rpc2.$call("/ubus/server", "ubus.server.list", {}).done(function(ret){ + $rpc2.$call("/ubus/peer", "ubus.peer.list", {}).done(function(ret){ var n = Date.now(); var objects = ret[0]; Object.keys(objects).map(function(k){