GithubHelp home page GithubHelp logo

pkg_rpc's Introduction

RPC Package

The RPC Package defines a generic interprocess communication (remote procedure call) programming model. The main header to include is:

#include <rpc_pkg.h>

Introduction

One of the basic prerequisites for creating a set of independent tools is the possibility to let the tools communicate with each other. A common way of handling communication between different processes may be provided by an extensible RPC mechanism.

Basics

Starting from ADTF 3.1 the RPC package uses libjsoncpp and libjson-rpc-cpp to perform the heavy lifting of handling remote calls. These libraries are integrated into this library, so there is no need for them to be installed on your system seperately. For additional documentation on stubs and json take a look at [https://github.com/cinemast/libjson-rpc-cpp] .

RPC Example

First start with defining you interface via a .json file, in this case a simple calculator

 [    
    {
        "name": "Add",
        "params": {
            "nValue1": 1,
            "nValue2": 1
        },
        "returns": 1
    },
    {
        "name": "Subtract",
        "params": {
            "nValue1": 1,
            "nValue2": 1
        },
        "returns": 1
    },
    {
        "name": "Multiply",
        "params": {
            "nValue1": 1,
            "nValue2": 1
        },
        "returns": 1
    },
    {
        "name": "Divide",
        "params": {
            "nValue1": 1,
            "nValue2": 1
        },
        "returns": 1
    }
]

To generate the neccessary stub headers you can use the supplied CMake macros

    jsonrpc_generate_client_stub

and

    jsonrpc_generate_server_stub

In our case somethin like the following will do the job:

jsonrpc_generate_client_stub(${CMAKE_CURRENT_SOURCE_DIR}/calculator.json rpc_stubs::cCalculatorClientStub ${CMAKE_CURRENT_BINARY_DIR}/calculatorclientstub.h)
jsonrpc_generate_server_stub(${CMAKE_CURRENT_SOURCE_DIR}/calculator.json rpc_stubs::cCalculatorServerStub ${CMAKE_CURRENT_BINARY_DIR}/calculatorserverstub.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

You can then create a simple client with the help of the @ref rpc::jsonrpc_remote_object template:

#include <rpc_pkg.h>
#include <calculatorclientstub.h>
...
rpc::jsonrpc_remote_object<rpc_stubs::cCalculatorClientStub> oRemoteCalculator("http://localhost:1234/calculator");
tInt nThree = oRemoteCalculator.Add(1, 2);

To implement the remote objects functionality you need to subclass rpc::jsonrpc_object_server and implement the pure virtual methods of the generated stub.

#include <rpc_pkg.h>
#include <calculatorserverstub.h>

class cCalculator: public rpc::jsonrpc_object_server<rpc_stubs::cCalculatorServerStub>
{
    public:
        int Add(int nValue1, int nValue2) override
        {
            return nValue1 + nValue2;
        }

        int Subtract(int nValue1, int nValue2) override
        {
            return nValue1 - nValue2;
        }

        int Multiply(int nValue1, int nValue2) override
        {
            return nValue1 * nValue2;
        }

        int Divide(int nValue1, int nValue2) override
        {
            return nValue1 / nValue2;
        }
};

In order for the remote object to be accessible we need to create an RPC server

void Server()
{
    rpc::cJSONRPCServer oServer;
    cCalculator oCalculator;
    
    oServer.RegisterRPCObject("calculator", &oCalculator);
    oServer.StartListening("http://0.0.0.0:1234");
    
    // wait for quit
    
    oServer.StopListening();
    oServer.UnregisterRPCObject("calculator");         
}

License

The package RPC is delivered under the MPL - Mozilla Public License - Version 2.0

Dependency

The RPC Package depends on the a_util library.

pkg_rpc's People

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.