|
| 1 | +/************************************************************************* |
| 2 | + * libjson-rpc-cpp |
| 3 | + ************************************************************************* |
| 4 | + * @file jsclientstubgenerator.cpp |
| 5 | + * @date 10/22/2014 |
| 6 | + * @author Peter Spiess-Knafl <[email protected]> |
| 7 | + * @license See attached LICENSE.txt |
| 8 | + ************************************************************************/ |
| 9 | + |
| 10 | +#include "jsclientstubgenerator.h" |
| 11 | +#include <algorithm> |
| 12 | + |
| 13 | +using namespace jsonrpc; |
| 14 | +using namespace std; |
| 15 | + |
| 16 | +#define TEMPLATE_JS_PROLOG "function <class>(url) {\n\ |
| 17 | + this.url = url;\n\ |
| 18 | + var id = 1;\n\ |
| 19 | + \n\ |
| 20 | + function doJsonRpcRequest(method, params, methodCall, callback_success, callback_error) {\n\ |
| 21 | + var request = {};\n\ |
| 22 | + if (methodCall)\n\ |
| 23 | + request.id = id++;\n\ |
| 24 | + request.jsonrpc = \"2.0\";\n\ |
| 25 | + request.method = method;\n\ |
| 26 | + request.params = params\n\ |
| 27 | + JSON.stringify(request);\n\ |
| 28 | + \n\ |
| 29 | + $.ajax({\n\ |
| 30 | + type: \"POST\",\n\ |
| 31 | + url: url,\n\ |
| 32 | + data: JSON.stringify(request),\n\ |
| 33 | + success: function (response) {\n\ |
| 34 | + if (methodCall) {\n\ |
| 35 | + if (response.hasOwnProperty(\"result\") && response.hasOwnProperty(\"id\")) {\n\ |
| 36 | + callback_success(response.id, response.result);\n\ |
| 37 | + } else if (response.hasOwnProperty(\"error\")) {\n\ |
| 38 | + if (callback_error != null)\n\ |
| 39 | + callback_error(response.error.code,response.error.message);\n\ |
| 40 | + } else {\n\ |
| 41 | + if (callback_error != null)\n\ |
| 42 | + callback_error(-32001, \"Invalid Server response: \" + response);\n\ |
| 43 | + }\n\ |
| 44 | + }\n\ |
| 45 | + },\n\ |
| 46 | + error: function () {\n\ |
| 47 | + if (methodCall)\n\ |
| 48 | + callback_error(-32002, \"AJAX Error\");\n\ |
| 49 | + },\n\ |
| 50 | + dataType: \"json\"\n\ |
| 51 | + });\n\ |
| 52 | + return id-1;\n\ |
| 53 | + }\n\ |
| 54 | + this.doRPC = function(method, params, methodCall, callback_success, callback_error) {\n\ |
| 55 | + return doJsonRpcRequest(method, params, methodCall, callback_success, callback_error);\n\ |
| 56 | + }\n\ |
| 57 | +}\n" |
| 58 | + |
| 59 | +#define TEMPLATE_JS_METHOD "<class>.prototype.<procedure> = function(<params>callbackSuccess, callbackError) {" |
| 60 | +#define TEMPLATE_JS_PARAM_NAMED "var params = {<params>};" |
| 61 | +#define TEMPLATE_JS_PARAM_POSITIONAL "var params = [<params>];" |
| 62 | +#define TEMPLATE_JS_PARAM_EMPTY "var params = null;" |
| 63 | +#define TEMPLATE_JS_CALL_METHOD "return this.doRPC(\"<procedure>\", params, true, callbackSuccess, callbackError);" |
| 64 | +#define TEMPLATE_JS_CALL_NOTIFICATION "this.doRPC(\"<procedure>\", params, false, callbackSuccess, callbackError);" |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +JSClientStubGenerator::JSClientStubGenerator(const std::string &stubname, std::vector<Procedure> &procedures, CodeGenerator &cg) : StubGenerator(stubname, procedures, cg) |
| 69 | +{ |
| 70 | +} |
| 71 | + |
| 72 | +string JSClientStubGenerator::class2Filename(const string &classname) |
| 73 | +{ |
| 74 | + string result = classname; |
| 75 | + std::transform(result.begin(), result.end(), result.begin(), ::tolower); |
| 76 | + return result + ".js"; |
| 77 | +} |
| 78 | + |
| 79 | +void JSClientStubGenerator::generateStub() |
| 80 | +{ |
| 81 | + cg.write(replaceAll(TEMPLATE_JS_PROLOG, "<class>", stubname)); |
| 82 | + cg.writeNewLine(); |
| 83 | + |
| 84 | + for (unsigned int i=0; i < procedures.size(); i++) |
| 85 | + { |
| 86 | + this->generateMethod(procedures[i]); |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +void JSClientStubGenerator::generateMethod(Procedure &proc) |
| 92 | +{ |
| 93 | + string method = TEMPLATE_JS_METHOD; |
| 94 | + replaceAll2(method, "<class>", stubname); |
| 95 | + replaceAll2(method, "<procedure>", proc.GetProcedureName()); |
| 96 | + |
| 97 | + stringstream param_string; |
| 98 | + stringstream params_assignment; |
| 99 | + |
| 100 | + parameterNameList_t list = proc.GetParameters(); |
| 101 | + for (parameterNameList_t::iterator it = list.begin(); it != list.end();) |
| 102 | + { |
| 103 | + param_string << it->first; |
| 104 | + |
| 105 | + if (proc.GetParameterDeclarationType() == PARAMS_BY_NAME) |
| 106 | + params_assignment << it->first << " : " << it->first; |
| 107 | + else |
| 108 | + params_assignment << it->first; |
| 109 | + |
| 110 | + if (++it != list.end()) |
| 111 | + { |
| 112 | + params_assignment << ", "; |
| 113 | + } |
| 114 | + param_string << ", "; |
| 115 | + } |
| 116 | + |
| 117 | + replaceAll2(method, "<params>", param_string.str()); |
| 118 | + |
| 119 | + cg.writeLine(method); |
| 120 | + cg.increaseIndentation(); |
| 121 | + |
| 122 | + string params; |
| 123 | + |
| 124 | + if (proc.GetParameters().size() > 0) |
| 125 | + { |
| 126 | + if (proc.GetParameterDeclarationType() == PARAMS_BY_NAME) |
| 127 | + params = TEMPLATE_JS_PARAM_NAMED; |
| 128 | + else |
| 129 | + params = TEMPLATE_JS_PARAM_POSITIONAL; |
| 130 | + |
| 131 | + replaceAll2(params, "<params>", params_assignment.str()); |
| 132 | + cg.writeLine(params); |
| 133 | + } |
| 134 | + else |
| 135 | + { |
| 136 | + cg.writeLine(TEMPLATE_JS_PARAM_EMPTY); |
| 137 | + } |
| 138 | + |
| 139 | + |
| 140 | + if (proc.GetProcedureType() == RPC_METHOD) |
| 141 | + method = TEMPLATE_JS_CALL_METHOD; |
| 142 | + else |
| 143 | + method = TEMPLATE_JS_CALL_NOTIFICATION; |
| 144 | + |
| 145 | + replaceAll2(method, "<procedure>", proc.GetProcedureName()); |
| 146 | + |
| 147 | + cg.writeLine(method); |
| 148 | + |
| 149 | + cg.decreaseIndentation(); |
| 150 | + cg.writeLine("};"); |
| 151 | + |
| 152 | + |
| 153 | +} |
0 commit comments