Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

APTriTec/libjson-rpc-cpp

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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 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

libjson-rpc-cpp logo

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: 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.

Overview

libjson-rpc-cpp logo

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.

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.

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 -ljsonrpccpp-server -ljsoncpp -ljsonrpccpp-common -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 -ljsonrpccpp-client -ljsoncpp -ljsonrpccpp-common -lcurl -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

  • 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

Changelogs can be found here.

Licsense

This framework is licensed under MIT.

Dependencies

  • 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.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C 47.8%
  • C++ 45.9%
  • CMake 5.7%
  • Other 0.6%