This framework provides cross platform JSON-RPC (remote procedure call) support for C++. It is fully JSON-RPC 2.0 & 1.0 compatible.
5 good reasons for using libjson-rpc-cpp in your next RPC project
- Full JSON-RPC 2.0 & 1.0 Client and Server Support.
- jsonrpcstub - a tool that generates stub-classes for your JSON-RPC client AND server applications.
- Ready to use HTTP server and client to provide simple interfaces for your JSON-RPC application.
- Cross platform build support and precompiled binaries for WIN32.
- Super liberal MIT-License.
Other good reasons to use libjson-rpc-cpp
- Easy to use cmake cross platform build system.
- Clean and simple architecture, which makes it easy to extend.
- Tested under MacOS X (10.9), GNU/Linux (Debian 8 64-bit), Windows 7 (MinGW32) and Raspbian Wheezy (armhf).
- Automated testing using
make test
. - Useful Examples provided. e.g. XBMC Remote using json-rpc client part and stub generator.
- The stubgenerator currently supports C++ and JavaScript.
Debian
sudo apt-get install libjsonrpccpp-dev libjsonrpccpp-tools
Arch Linux
For Arch Linux there is a PKGBUILD provided in the AUR.
sudo aura -A libjson-rpc-cpp
Gentoo Linux
sudo emerge dev-cpp/libjson-rpc-cpp
Mac OS X
For OS X a Brew package is available:
brew install libjson-rpc-cpp
Windows
There is a ready to use compiled package here. Just download execute the installer EXE.
UNIX
For Debian and Arch GNU/Linux based systems, all dependencies are available via the package manager. For OS X all dependencies are available in Brew
Windows
- Download the precompiled dependencies form here.
- Extract it into the cloned repository, so that there is a
win32-deps
folder in the root project directory.
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
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:
Default configuration should be fine for most systems, but here are available compilation flags:
-DCOMPILE_TESTS=NO
disables unit test suite even if boost libraries are found.-DCOMPILE_STUBGEN=NO
disables building the stubgenerator.-DCOMPILE_EXAMPLES=NO
disables examples.-DHTTP_SERVER=NO
disable the embedded mongoose webserver.-DHTTP_CLIENT=NO
disable the curl 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"
}
]
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 "abstractstubserver.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 -ljsoncpp -lmicrohttpd -ljsonrpccpp-common -ljsonrpccpp-server -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") << endl;
c.notifyServer();
}
catch (JsonRpcException e)
{
cerr << e.what() << endl;
}
}
Compile the client with:
g++ main.cpp -ljsoncpp -lcurl -ljsonrpccpp-common -ljsonrpccpp-client -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 scalable cloud based video transcoding platform.
- wgslib: A web geostatistics library.
- bitcoin-api-cpp: a C++ interface to bitcoin.
- NIT DASH Content Server: Dynamic Adaptive Streaming over HTTP server.
If you use this library and find it useful, I would be very pleased if you let me know about it.
Contributions of any kind are always very welcome. Here are some suggestions:
- Bugreports
- Bugfixes
- Extending documentation (especially doxygen)
- Suggestion of new features
- New features:
- Adding new connectors.
- Adding new languages to the stubgenerator.
Guidelines / Conventions
We do not want to prevent you from contributing by having too strict guidelines. If you have ideas for improvement, just do it your way, rather than doing it not at all.
Anyway here is a list of how we would prefer your contributions:
- Issues:
- Use the issue tracker on github to report bugs or improvements.
- Please avoid sending me mails directly, as this is not visible to others.
- Please close issues on your own if you think a problem has been dealt with.
- Code contributions:
- Please raise a pull-request against the develop branch.
- If you add features, please keep the test-coverage at 100% and document them (doxygen, manpage, etc.).
- If you fix a bug, please refer the issue in the commit message.
- Please make sure that the travis-ci build passes (you will get notified if you raise a pull-request).
- Add yourself to the AUTHORS.md.
- Use 4 spaces instead of tabs.
- Generate client stubs for other languages.
- Extend doxygen documentation.
Changelogs can be found here.
We do our best to keep the API/ABI stable, to prevent problems when updating this framework. A compatiblity report can be found here.
This framework is licensed under MIT. All of this libraries dependencies are licensed under MIT compatible licenses.
The documentation for this library can be generated using doxygen. If it is installed on your system, you can simply type:
cd build
make doc
This generates the Latex and HTML documentation into build/doc
For running the tests, the boost-test framework is required:
Debian based systems
cd build
sudo apt-get install libboost-test-dev
cmake .. && make test
Mac OS X You need Brew installed and type the following commands
cd build
brew install boost
cmake .. && make test
Testcoverage can be retrieved by invoking the dev/testcoverage.sh script.