This C++ library provides a json-rpc (remote procedure call) framework for Linux and MacOS (or any other UNIX derivate). It is fully JSON-RPC 2.0 compatible (JSON-RPC 2.0) and provides additional features, such as generic authentication mechanisms.
Notice: This framework is currently in a beta phase. Bug reports as well as contributions are very welcome! Heavy API/ABI changes might occur regularly but you can always find stable versions in the Releases section
- Full JSON-RPC 2.0/1.0 Client and Server Support
- jsonrpcstub: Generates automatically C++ stub-classes for your JSON-RPC client AND server.
- Embedded HTTP server to provide simple interfaces for your JSON-RPC server application.
- Embedded HTTP client to connect easily via HTTP protocol.
- Super liberal MIT-License.
Other good reasons to use libjson-rpc-cpp
- Easy to use.
- Method parameter names and checks can be easily configured through a simple json configuration file.
- Very verbose error reporting.
- Easy to use cmake cross platform build system.
- Clean and simple architecture.
- Tested under MacOS X (10.9), Linux (Debian 8 64-bit).
- Tested under RaspberryPi (raspbian). This library offers great opportunities to remotely control your raspberry pi.
- Automated testing using
make test
- Useful Examples provided. e.g. XBMC Remote using json-rpc client part and stub generator.
- JavaScript client stub generation.
You will need Git and CMake. You can click on the links to download the latest versions. libcurl is also required but should already be installed on most systems. CMake must be Version 2.6 or later.
Install the dependencies:
Debian based systems:
sudo apt-get install libcurl4-openssl-dev libjsoncpp-dev libargtable2-dev libboost-test-dev cmake
Mac OS X
You need Brew installed and type the following commands
brew install argtable cmake boost jsoncpp
Build and install this framework
Open a terminal and copy the following commands:
git clone git://github.com/cinemast/libjson-rpc-cpp.git
mkdir -p libjson-rpc-cpp/build
cd libjson-rpc-cpp/build
cmake .. && make
sudo make install #Not required, but makes it easier to use
sudo ldconfig #only required for linux
That's it!
If you are not happy with it, simply uninstall it from your system using (inside the build the directory):
sudo make uninstall
Build options:
-DCOMPILE_TESTS=NO
disables unit test suite.-DCOMPILE_STUBGEN=NO
disables building the stubgenerator.-DCOMPILE_EXAMPLES=NO
disables examples.-DHTTP_SERVER_MONGOOSE=NO
disable the embedded mongoose webserver.-DHTTP_CLIENT_CURL=NO
disable the curl client.-DSOCKET_SERVER=YES
enable the socket server.-DSOCKET_CLIENT=YES
enable the socket client.
This example will show the most simple way to create a rpc server and client. If you only need the server, ignore step 4. If you only need the client, ignore step 3. You can find all resources of this sample in the src/examples
directory of this repository.
[
{
"name": "sayHello",
"params": {
"name": "Peter"
},
"returns" : "Hello Peter"
},
{
"name" : "notifyServer",
"params": null
}
]
The type of a return value or parameter is defined by the literal assigned to it. In this example you can see how to specify methods and notifications.
Call jsonrpcstub:
jsonrpcstub spec.json --cpp-server=AbstractStubServer --cpp-client=StubClient
This generates a serverstub and a clientstub class.
Extend the abstract server stub and implement all pure virtual (abstract) methods defined in spec.json
.
#include "abstractsubserver.h"
#include <jsonrpccpp/server/connectors/httpserver.h>
using namespace jsonrpc;
using namespace std;
class MyStubServer : public AbstractStubServer
{
public:
MyStubServer(AbstractServerConnector &connector);
virtual void notifyServer();
virtual std::string sayHello(const std::string& name);
};
MyStubServer::MyStubServer(AbstractServerConnector &connector) :
AbstractStubServer(connector)
{
}
void MyStubServer::notifyServer()
{
cout << "Server got notified" << endl;
}
string MyStubServer::sayHello(const string &name)
{
return "Hello " + name;
}
int main()
{
HttpServer httpserver(8383);
MyStubServer s(httpserver);
s.StartListening();
getchar();
s.StopListening();
return 0;
}
In the main function the concrete server is instantiated and started. That is all for the server. Any JSON-RPC 2.0 compliant client can now connect to your server.
Compile the server with:
g++ main.cpp -ljsonrpccpp-server -ljsoncpp -ljsonrpccpp-common -o sampleserver
#include <iostream>
#include "stubclient.h"
#include <jsonrpccpp/client/connectors/httpclient.h>
using namespace jsonrpc;
using namespace std;
int main()
{
HttpClient httpclient("http://localhost:8383");
StubClient c(httpclient);
try
{
cout << c.sayHello("Peter Knafl") << endl;
c.notifyServer();
}
catch (JsonRpcException e)
{
cerr << e.what() << endl;
}
}
Compile the client with:
g++ main.cpp -ljsonrpccpp-client -ljsoncpp -ljsonrpccpp-common -lcurl -o sampleclient
- NASA Ames Research Center: use it to obtain aircraft state information from an aircraft simulator.
- LaseShark 3D Printer: used to control the firmware of the 3D printer.
- cpp-ethereum: a distributed computing framework.
- mage-sdk-cpp: a game engine.
- bitcodin: a scaleable cloud based video transcoding platform.
- wgslib: A web geostatistics library.
- bitcoin-api-cpp: a C++ interface to bitcoin.
If you use this library and find it useful, I would be very pleased if you let me know about it.
- Official win32/64 build support.
- Review socket connectors
- libmicrohttpd server connector (to replace mongoose, because of license issues)
- Generate client stubs for other languages.
Changelogs can be found here.
This framework is licensed under MIT.
- jsoncpp (licensed under MIT) jsoncpp is a very easy to use and powerful json library. It is used for all the JSON parsing and generation inside this library.
- mongoose (licensed under MIT) mongoose is a http server that can be easily embedded into other applications. It is used here for the HttpConnector to provide HTTP json-rpc Requests.
- curl lib curl is used for the HttpClient connections.
- argtable2 (licensed under LGPL) libargtable2 is used for handling commandline parameters of the jsonrpcstub tool.