libjson-rpc-cpp

C++ framework for json-rpc (json remote procedure call)

This project is maintained by cinemast

Build Status

libjson-rpc-cpp

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 are very welcome!

libjson-rpc-cpp logo

5 good reasons for using libjson-rpc-cpp in your next RPC project:

Other good reasons to use libjson-rpc-cpp

Build the framework

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.

Under Debian based systems type:

sudo apt-get install libcurl4-openssl-dev libjsoncpp-dev libargtable2-dev libboost-test-dev cmake

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:

Simple Example

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.

Step 1: Writing the specification file

[
    {
        "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.

Step 2: Generate the stubs for client and server

Call jsonrpcstub:

jsonrpcstub spec.json --cpp-server=AbstractStubServer --cpp-client=StubClient

This generates a serverstub and a clientstub class.

Step 3: implement the abstract server stub

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 -ljsonrpccppserver -o sampleserver

Step 4: Create the client application

#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 -ljsonrpccppclient -o sampleclient

References

If you use this library and find it useful, I would be very pleased if you let me know about it.

Roadmap for next release

Changelogs

Changelogs can be found here.

Known issues

Licsense

This framework is licensed under MIT.

Dependencies