Coder Social home page Coder Social logo

vincentlao / libjson-rpc-cpp Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cinemast/libjson-rpc-cpp

0.0 2.0 0.0 1.69 MB

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

License: MIT License

CMake 15.41% Makefile 0.33% Shell 0.30% C++ 82.96% HTML 0.28% C 0.72%

libjson-rpc-cpp's Introduction

Master Build Status codecov Develop Build Status codecov | Coverity Status

libjson-rpc-cpp

This framework provides cross platform JSON-RPC (remote procedure call) support for C++. It is fully JSON-RPC 2.0 & 1.0 compatible.

libjson-rpc-cpp logo

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

  • Full JSON-RPC 2.0 & partial JSON-RPC 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 + TCP server and client to provide simple interfaces for your JSON-RPC application.
  • Cross platform build support for Linux and OS X.
  • 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.
  • Continuously tested under MacOS, and various linux distributions.
  • 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.

Overview

libjson-rpc-cpp logo

Install the framework

Debian (stretch) and Ubuntu (15.10 or later)

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.

Build from source

Install the dependencies

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

Build

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.
  • -DCOMPILE_STUBGEN=NO disables building the stubgenerator.
  • -DCOMPILE_EXAMPLES=NO disables examples.
  • -DHTTP_SERVER=NO disable the libmicrohttpd webserver.
  • -DHTTP_CLIENT=NO disable the curl client.
  • -DREDIS_SERVER=NO disable the redis server connector.
  • -DREDIS_CLIENT=NO disable the redis client connector.
  • -DUNIX_DOMAIN_SOCKET_SERVER=NO disable the unix domain socket server connector.
  • -DUNIX_DOMAIN_SOCKET_CLIENT=NO disable the unix domain socket client connector.
  • -DFILE_DESCRIPTOR_SERVER=NO disable the file descriptor server connector.
  • -DFILE_DESCRIPTOR_CLIENT=NO disable the file descriptor client connector.
  • -DTCP_SOCKET_SERVER=NO disable the tcp socket server connector.
  • -DTCP_SOCKET_CLIENT=NO disable the tcp socket client connector.

Using the framework

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"
	}
]

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

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

Contributions

Please take a look at CONTRIBUTING.md

Changelogs

Changelogs can be found here.

API compatibility

We do our best to keep the API/ABI stable, to prevent problems when updating this framework. A compatiblity report can be found here.

License

This framework is licensed under MIT. All of this libraries dependencies are licensed under MIT compatible licenses.

References

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

libjson-rpc-cpp's People

Contributors

adah1972 avatar akindyakov avatar alexandre-poirot avatar bcachet avatar bertrandboudaud avatar caktux avatar cblp avatar chfast avatar cinemast avatar debris avatar ericlagergren avatar gogo40 avatar iammattcoleman avatar ignatenkobrain avatar jdmichaud avatar jmassey-jacques avatar joegen avatar jugglershu avatar kaidokert avatar knzm avatar kraj avatar lookfwd avatar martinbrook avatar mgorny avatar milk avatar ruslo avatar smurfy avatar stelcheck avatar vraychev avatar zhangyafeikimi avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.