From d3d9e2e7715f7cbdf9182a7676d663e1947f01cd Mon Sep 17 00:00:00 2001 From: ansuz Date: Mon, 4 Dec 2017 11:10:25 +0100 Subject: [PATCH] WIP: refactoring rpc code --- www/assert/main.js | 49 +++++++++++++++++++++++- www/common/wire.js | 94 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 www/common/wire.js diff --git a/www/assert/main.js b/www/assert/main.js index 968200dc9..56cc29116 100644 --- a/www/assert/main.js +++ b/www/assert/main.js @@ -6,8 +6,9 @@ define([ '/common/test.js', '/common/common-hash.js', '/common/common-thumbnail.js', + '/common/wire.js', '/common/flat-dom.js', -], function ($, Hyperjson, Sortify, Drive, Test, Hash, Thumb, Flat) { +], function ($, Hyperjson, Sortify, Drive, Test, Hash, Thumb, Wire, Flat) { window.Hyperjson = Hyperjson; window.Sortify = Sortify; @@ -30,7 +31,7 @@ define([ ASSERTS.forEach(function (f, index) { f(function (err) { - console.log("test " + index); + //console.log("test " + index); done(err, index); }, index); }); @@ -235,6 +236,50 @@ define([ return cb(true); }, "version 2 hash failed to parse correctly"); + assert(function (cb) { + Wire.create({ + constructor: function (cb) { + var service = function (type, data, cb) { + switch (type) { + case "HEY_BUDDY": + return cb(void 0, "SALUT!"); + default: + cb("ERROR"); + } + }; + + var respond; + cb(void 0, { + send: function (raw, cb) { + try { + var parsed = JSON.parse(raw); + var txid = parsed.txid; + var message = parsed.message; + setTimeout(function () { + service(message.command, message.content, function (e, result) { + respond(JSON.stringify({ + txid: txid, + error: e, + content: result, + })); + }); + }); + } catch (e) { console.error("PEWPEW"); } + }, + receive: function (f) { + respond = f; + }, + }); + }, + }, function (e, rpc) { + if (e) { return cb(false); } + rpc.send('HEY_BUDDY', null, function (e, out) { + if (e) { return void cb(false); } + if (out === 'SALUT!') { cb(true); } + }); + }); + }, "Test rpc factory"); + /* assert(function (cb) { var getBlob = function (url, cb) { diff --git a/www/common/wire.js b/www/common/wire.js new file mode 100644 index 000000000..e0a3530b2 --- /dev/null +++ b/www/common/wire.js @@ -0,0 +1,94 @@ +define([ + +], function () { + var Wire = {}; + + /* MISSION: write a generic RPC framework + +Requirements + +* some transmission methods can be interrupted + * handle disconnects and reconnects +* handle callbacks +* configurable timeout +* Service should expose 'addClient' method + * and handle broadcast + + +* + + */ + + var uid = function () { + return Number(Math.floor(Math.random () * + Number.MAX_SAFE_INTEGER)).toString(32); + }; + +/* +opt = { + send: function () { + + }, + receive: function () { + + }, + constructor: function (cb) { + cb(void 0 , { + send: function (content, cb) { + + }, + receive: function () { + + } + }); + }, +}; +*/ + + Wire.create = function (opt, cb) { + var ctx = {}; + var pending = ctx.pending = {}; + ctx.connected = false; + + var rpc = {}; + + opt.constructor(function (e, service) { + if (e) { return setTimeout(function () { cb(e); }); } + + rpc.send = function (type, data, cb) { + var txid = uid(); + if (typeof(cb) !== 'function') { + throw new Error('expected callback'); + } + + ctx.pending[txid] = function (err, response) { + cb(err, response); + }; + + service.send(JSON.stringify({ + txid: txid, + message: { + command: type, + content: data, + }, + })); + }; + + service.receive(function (raw) { + try { + var data = JSON.parse(raw); + var txid = data.txid; + if (!txid) { throw new Error('NO_TXID'); } + var cb = pending[txid]; + if (data.error) { return void cb(data.error); } + cb(void 0, data.content); + } catch (e) { console.error("UNHANDLED_MESSAGE", data); } + }); + + cb(void 0, rpc); + }); + }; + + + return Wire; +});