Skip to content

Commit b07169a

Browse files
author
Peter Spiess-Knafl
committed
Merge branch 'release/v0.3.1'
2 parents b7a4d2b + bf5cccc commit b07169a

File tree

9 files changed

+256
-9
lines changed

9 files changed

+256
-9
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
Changes in v0.3.1
2+
- Bugfixes
3+
- Changed SOVERSION
4+
- Added experimental Javascript client stub to stubgenerator
5+
- Adapted HTTP Server to enable CORS.
6+
17
Changes in v0.3
28
---------------
39
- Split up server and client into separate libraries
@@ -31,4 +37,4 @@ Changes in v0.2
3137

3238
Known issues
3339
-------------
34-
- Due to a bug in gcc 4.6.2 this project is not compiling under Mac OS gcc. Use clang++ instead. [See here](http://stackoverflow.com/questions/8887864/template-base-constructor-call-in-member-initialization-list-error)
40+
- Due to a bug in gcc 4.6.2 this project is not compiling under Mac OS gcc. Use clang++ instead. [See here](http://stackoverflow.com/questions/8887864/template-base-constructor-call-in-member-initialization-list-error)

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Wall --coverage")
1212

1313
SET(MAJOR_VERSION 0)
1414
SET(MINOR_VERSION 3)
15-
SET(PATCH_VERSION 0)
15+
SET(PATCH_VERSION 1)
1616

1717
#possible modules that can be enabled/disabled
1818
SET(HTTP_SERVER_MONGOOSE YES CACHE BOOL "Include HTTP server using mongoose")

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ It is fully JSON-RPC 2.0 compatible ([JSON-RPC 2.0](http://www.jsonrpc.org/speci
2828
- Tested under RaspberryPi (raspbian). This library offers great opportunities to remotely control your raspberry pi.
2929
- Automated testing using `make test`
3030
- Useful Examples provided. e.g. XBMC Remote using json-rpc client part and stub generator.
31+
- JavaScript client stub generation.
3132

3233
Overview
3334
---------
@@ -51,11 +52,9 @@ sudo apt-get install libcurl4-openssl-dev libjsoncpp-dev libargtable2-dev libboo
5152

5253
You need [Brew](http://brew.sh) installed and type the following commands
5354
```sh
54-
brew install argtable cmake boost
55+
brew install argtable cmake boost jsoncpp
5556
```
5657

57-
[jsoncpp](https://github.com/open-source-parsers/jsoncpp) needs to be built by hand, because jsoncpp is not merged into brew yet. [See here](https://github.com/Homebrew/homebrew/pull/32916).
58-
5958
**Build and install this framework**
6059

6160
Open a terminal and copy the following commands:

doc/manpage

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ jsonrpcstub \- genearate stubs for the libjson\-rpc\-cpp framework.
77
.B
88
jsonrpcstub specfile.json [\-\-cpp\-server=namespace::ClassName]
99
[\-\-cpp\-server\-file=classname.h] [\-\-cpp\-client=namespace::ClassName]
10-
[\-\-cpp\-client-file=classname.h]
10+
[\-\-cpp\-client-file=classname.h] [\-\-js\-client=ClassName]
11+
[\-\-js-client-file=classname.js]
1112
.PP
1213
.B
1314
jsonrpcstub -i specfile.json -c ClassName

src/examples/index.html

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
5+
<meta content="utf-8" http-equiv="encoding">
6+
<title>Title of the document</title>
7+
<script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
8+
<script src="stubclient.js" type="text/javascript"></script>
9+
<script type="text/javascript">
10+
11+
$(document).ready(function () {
12+
function showResult(id, result) {
13+
alert("ID: " + id + " Result: " + result);
14+
}
15+
16+
function showError(code, message) {
17+
alert("Error: " + code + " -> " + message);
18+
}
19+
20+
var client = new StubClient("http://localhost:8383");
21+
22+
client.sayHello("Peter", showResult, showError);
23+
client.addNumbers(3,4,showResult, showError);
24+
client.isEqual("3","3",showResult,showError);
25+
client.isEqual("3",4,showResult,showError); //invalid params error
26+
client.notifyServer();
27+
});
28+
function displayResult(result) {
29+
alert(JSON.stringify(result));
30+
}
31+
</script>
32+
</head>
33+
<body>
34+
</body>
35+
</html>

src/jsonrpccpp/server/connectors/httpserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ bool HttpServer::SendResponse(const std::string& response, void* addInfo)
126126
if (mg_printf(conn, "HTTP/1.1 200 OK\r\n"
127127
"Content-Type: application/json\r\n"
128128
"Content-Length: %d\r\n"
129+
"Access-Control-Allow-Origin: *\r\n"
129130
"\r\n"
130131
"%s",(int)response.length(), response.c_str()) > 0)
131132
{
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*************************************************************************
2+
* libjson-rpc-cpp
3+
*************************************************************************
4+
* @file jsclientstubgenerator.h
5+
* @date 10/22/2014
6+
* @author Peter Spiess-Knafl <[email protected]>
7+
* @license See attached LICENSE.txt
8+
************************************************************************/
9+
10+
#ifndef JSONRPC_CPP_JSCLIENTSTUBGENERATOR_H
11+
#define JSONRPC_CPP_JSCLIENTSTUBGENERATOR_H
12+
13+
#include "../stubgenerator.h"
14+
15+
namespace jsonrpc {
16+
17+
class JSClientStubGenerator : public StubGenerator
18+
{
19+
public:
20+
JSClientStubGenerator(const std::string& stubname, std::vector<Procedure> &procedures, CodeGenerator &cg);
21+
22+
static std::string class2Filename(const std::string &classname);
23+
24+
virtual void generateStub();
25+
26+
private:
27+
virtual void generateMethod(Procedure &proc);
28+
29+
};
30+
31+
} // namespace jsonrpc
32+
33+
#endif // JSONRPC_JSCLIENTSTUBGENERATOR_H

src/stubgenerator/main.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <argtable2.h>
1717
#include "helper/cpphelper.h"
1818
#include "client/cppclientstubgenerator.h"
19+
#include "client/jsclientstubgenerator.h"
1920
#include "server/cppserverstubgenerator.h"
2021

2122
#define EXIT_ERROR(X) cerr << X << endl;arg_freetable(argtable,sizeof(argtable)/sizeof(argtable[0]));return 1;
@@ -32,9 +33,12 @@ int main(int argc, char** argv)
3233
struct arg_str *cppserverfile = arg_str0(NULL, "cpp-server-file", "<filename.h>", "name of the C++ server stub file");
3334
struct arg_str *cppclient = arg_str0(NULL, "cpp-client", "<namespace::classname>", "name of the C++ client stub class");
3435
struct arg_str *cppclientfile = arg_str0(NULL, "cpp-client-file", "<namespace::classname>", "name of the C++ client stub file");
36+
struct arg_str *jsclient = arg_str0(NULL, "js-client", "<classname>", "name of the JavaScript client stub class");
37+
struct arg_str *jsclientfile = arg_str0(NULL, "js-client-file", "<filename.js>", "name of the JavaScript client stub file");
38+
3539

3640
struct arg_end *end = arg_end(20);
37-
void* argtable[] = {inputfile, help, verbose, cppserver, cppserverfile, cppclient, cppclientfile, end};
41+
void* argtable[] = {inputfile, help, verbose, cppserver, cppserverfile, cppclient, cppclientfile, jsclient, jsclientfile,end};
3842

3943
if (arg_parse(argc,argv,argtable) > 0)
4044
{
@@ -80,7 +84,7 @@ int main(int argc, char** argv)
8084
else
8185
filename = CPPHelper::class2Filename(cppserver->sval[0]);
8286
if (verbose->count > 0)
83-
cout << "Generating C++ Serverstub to: " << CPPHelper::class2Filename(cppserver->sval[0]) << endl;
87+
cout << "Generating C++ Serverstub to: " << filename << endl;
8488
CodeGenerator cg(filename);
8589
CPPServerStubGenerator serverstub(cppserver->sval[0], procedures, cg);
8690
serverstub.generateStub();
@@ -94,11 +98,26 @@ int main(int argc, char** argv)
9498
else
9599
filename = CPPHelper::class2Filename(cppclient->sval[0]);
96100
if (verbose->count > 0)
97-
cout << "Generating C++ Clientstub to: " << CPPHelper::class2Filename(cppclient->sval[0]) << endl;
101+
cout << "Generating C++ Clientstub to: " << filename << endl;
98102
CodeGenerator cg(filename);
99103
CPPClientStubGenerator cppclientstub(cppclient->sval[0], procedures, cg);
100104
cppclientstub.generateStub();
101105
}
106+
107+
if (jsclient->count > 0)
108+
{
109+
string filename;
110+
if (jsclientfile->count > 0)
111+
filename = jsclientfile->sval[0];
112+
else
113+
filename = JSClientStubGenerator::class2Filename(jsclient->sval[0]);
114+
115+
if (verbose->count > 0)
116+
cout << "Generating JavaScript Clientstub to: " << filename << endl;
117+
CodeGenerator cg(filename);
118+
JSClientStubGenerator jsclientstub(jsclient->sval[0], procedures, cg);
119+
jsclientstub.generateStub();
120+
}
102121
}
103122
catch (const JsonRpcException &ex)
104123
{

0 commit comments

Comments
 (0)