ADTF
Loading...
Searching...
No Matches
Example Demo RPC

Description

Shows how to access the rpc interfaces.

Prebuilt Binaries

Source Code

./examples/demo_adtfplugins/demo_rpc/

Json

calculator.json

[
// see the documentation at https://github.com/cinemast/libjson-rpc-cpp
// that describes this interface definition format
{
"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
},
{
"name": "SetDelay",
"params": {
"nSeconds": 1
},
"returns": {
"ErrorCode": 0,
"Description": "no error",
"Line": 0,
"File": "file.cpp",
"Function": "foo"
}
}
]

Interfaces

calculator_intf.h

#pragma once
class ICalculatorRPC
{
public:
ADTF_IID(ICalculatorRPC, "calculator.demo.remote.adtf");
static constexpr const tChar* const DEFAULT_NAME = "calculator";
};
class ICalculator : public adtf::ucom::ant::IObject
{
public:
ADTF_IID(ICalculator, "calculator.demo.filter.adtf");
virtual int Add(int nValue1, int nValue2) = 0;
virtual int Subtract(int nValue1, int nValue2) = 0;
virtual int Multiply(int nValue1, int nValue2) = 0;
virtual int Divide(int nValue1, int nValue2) = 0;
virtual tResult SetDelay(int nSeconds) = 0;
};
#define ADTF_IID(_interface, _striid)
Definition adtf_iid.h:19
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
Definition object_intf.h:33

demo_rpc_remote_interface.h

#pragma once
#include <adtf_remotesdk.h>
// newer GCC versions warn about the deprecated throws declarations used by jsonrpc-cpp
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
#endif
#include <calculatorclientstub.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
class cRemoteCalculator : public adtf::remote::rpc_remote_interface<rpc_stubs::cCalculatorClientStub, ICalculatorRPC>, public object <ICalculator>
{
public:
using rpc_remote_interface::rpc_remote_interface;
ADTF_CLASS_ID(cRemoteCalculator, "calculator.demo.remote.adtf");
int Add(int nValue1, int nValue2) override EXCEPTION_TO_DEFAULT(0,
{
return GetStub().Add(nValue1, nValue2);
})
int Subtract(int nValue1, int nValue2) override EXCEPTION_TO_DEFAULT(0,
{
return GetStub().Subtract(nValue1, nValue2);
})
int Multiply(int nValue1, int nValue2) override EXCEPTION_TO_DEFAULT(0,
{
return GetStub().Multiply(nValue1, nValue2);
})
int Divide(int nValue1, int nValue2) override EXCEPTION_TO_DEFAULT(0,
{
return GetStub().Divide(nValue1, nValue2);
})
tResult SetDelay(int nSeconds) override EXCEPTION_TO_RESULT(
{
return ToResult(GetStub().SetDelay(nSeconds));
})
};
#define ADTF_CLASS_ID(_class, _strcid)
ADTF_CLASS_INFO_IMPL.
Definition class_id.h:91
Stub & GetStub() const
Definition rpc_stubs.h:228
#define EXCEPTION_TO_RESULT(__body)
macro to return a tResult in case of an exception
Definition rpc_stubs.h:75
#define EXCEPTION_TO_DEFAULT(__default, __body)
macro to return a default value in case of an exception
Definition rpc_stubs.h:28

Service Client

demo_rpc_client.h

#pragma once
#ifndef ADTF_EXAMPLES_CID
#define ADTF_EXAMPLES_CID ".local.cid"
#endif
#define CID_DEMO_ADTF_RPC_CLIENT "demo_calculator_rpc_client.service" ADTF_EXAMPLES_CID
class cRPCClientService final : public adtf::system::cADTFService
{
public:
ADTF_CLASS_ID_NAME(cRPCClientService, CID_DEMO_ADTF_RPC_CLIENT, "Demo Calculator RPC Client");
public: // overrides cService
cRPCClientService();
~cRPCClientService() override;
virtual tResult ServiceInit() override;
private:
adtf::base::property_variable<adtf::util::cString> m_strURL{"$(ADTF_REMOTE_CONTROL_URL)/calculator"};
};
#define ADTF_CLASS_ID_NAME(_class, _strcid, _strclabel)
Definition class_id.h:33
Property Variable template for the given T. A Property Variable will store a copy of a property value...
Definition configuration.h:808
Definition adtf_service.h:25

demo_rpc_client.cpp

#include <adtf_systemsdk.h>
using namespace adtf::ucom;
using namespace adtf::base;
using namespace adtf::system;
#include "demo_rpc_client.h"
#include "calculator_intf.h"
#include "demo_rpc_remote_interface.h"
cRPCClientService::cRPCClientService()
{
// sets a short description for the component
SetDescription("Use this service to extend the ADTF System with a calculator client using RPC interfaces.");
// set help link to jump to documentation from ADTF Configuration Editor
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_calculator_rpc_client.html");
// set default runlevel
SetDefaultRunlevel(tADTFRunLevel::RL_Session);
m_strURL.SetDescription("State a RPC endpoint of the current ADTF System");
RegisterPropertyVariable("remote_control_url", m_strURL);
}
cRPCClientService::~cRPCClientService()= default;
tResult cRPCClientService::ServiceInit()
{
RETURN_IF_FAILED(cADTFService::ServiceInit());
// direct use of the stub methods
{
adtf::remote::remote_object<rpc_stubs::cCalculatorClientStub> oRemoteCalculator(m_strURL->GetPtr());
int nThree = oRemoteCalculator.Add(1, 2);
LOG_INFO("The result of 1 + 2 equals %d", nThree);
}
// use our mapped interface methods
{
object_ptr<ICalculator> pRemoteCalculator = make_object_ptr<cRemoteCalculator>(m_strURL->GetPtr());
int nTwo = pRemoteCalculator->Multiply(1, 2);
LOG_INFO("The result of 1 * 2 equals %d", nTwo);
}
}
#define LOG_INFO(...)
Logs an info message.
Definition log.h:388
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
Definition object_ptr.h:384
Namespace for the ADTF Base SDK.
Definition adtf_base_type_traits.h:13
Namespace for the ADTF System SDK.
Definition adtf_service.h:14
Namespace for the ADTF uCOM SDK.
Definition adtf_system.h:324
object_ptr< Implementation > make_object_ptr(Args &&... args)
Alias always bringing the latest version of ant::make_object_ptr() into scope.
Definition object_ptr_utilities.h:129

Service Server

demo_rpc_server.h

#pragma once
#ifndef ADTF_EXAMPLES_CID
#define ADTF_EXAMPLES_CID ".local.cid"
#endif
#define CID_DEMO_ADTF_RPC_SERVER "demo_calculator_rpc_server.service" ADTF_EXAMPLES_CID
class cRPCServerService final : public adtf::system::cADTFService
{
public:
ADTF_CLASS_ID_NAME(cRPCServerService, CID_DEMO_ADTF_RPC_SERVER, "Demo Calculator RPC Server");
public: // overrides cService
cRPCServerService();
~cRPCServerService() override;
virtual tResult ServiceInit() override;
virtual tResult ServiceShutdown() override;
};

demo_rpc_server.cpp

#include <adtf_systemsdk.h>
using namespace adtf::ucom;
using namespace adtf::base;
using namespace adtf::system;
#include "demo_rpc_server.h"
#include "demo_rpc_object_server.h"
cRPCServerService::cRPCServerService()
{
// sets a short description for the component
SetDescription("Use this service to extend the ADTF System with a calculator server using RPC interfaces.");
// set help link to jump to documentation from ADTF Configuration Editor
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_calculator_rpc_server.html");
}
cRPCServerService::~cRPCServerService() = default;
tResult cRPCServerService::ServiceInit()
{
RETURN_IF_FAILED(cADTFService::ServiceInit());
// create our server object
// and register it at the RPC object registry
RETURN_IF_FAILED(adtf::remote::rpc_register_object_server(ICalculatorRPC::DEFAULT_NAME, pServerObject));
}
tResult cRPCServerService::ServiceShutdown()
{
// tell the registry that our RPC object is no longer available
adtf::remote::rpc_unregister_object_server(ICalculatorRPC::DEFAULT_NAME);
}

Filter Client

demo_calculator_client_filter.h

#pragma once
#include "calculator_intf.h"
#ifndef ADTF_EXAMPLES_CID
#define ADTF_EXAMPLES_CID ".local.cid"
#endif
#define CID_DEMO_ADTF_CALCULATION_FILTER "demo_calculator_client_filter.filter" ADTF_EXAMPLES_CID
class cCalculationClientFilter final : public adtf::filter::cFilter
{
public:
ADTF_CLASS_ID_NAME(cCalculationClientFilter, CID_DEMO_ADTF_CALCULATION_FILTER, "Demo Calculator");
public: // overrides cFilter
cCalculationClientFilter();
~cCalculationClientFilter() override;
adtf::streaming::IRunner* pRunner) override;
tResult Init(tInitStage eStage) override;
private:
};
Definition filter.h:289
tResult Init(tInitStage eStage) override
virtual tResult Process(base::flash::tNanoSeconds tmTrigger, streaming::ant::IRunner *pRunner)
Definition graph_object.h:66
Definition runner_intf.h:24
A timestamp with nanosecond precision.
Definition chrono.h:23

demo_calculator_client_filter.cpp

#include <adtf_filtersdk.h>
using namespace adtf::base;
using namespace adtf::streaming;
#include "demo_calculator_client_filter.h"
cCalculationClientFilter::cCalculationClientFilter()
{
// create a interface client pin to access the calculator interface
m_oInterfaceClient = CreateInterfaceClient<ICalculator>("calculator_client");
SetDescription("calculator_client", "Interface client to call a calculator function from a server");
// create a trigger function that can be connected to an active runner.
CreateRunner("trigger", cTimerTriggerHint(std::chrono::seconds{ 1 }));
SetDescription("trigger", "Runner to periodically trigger the function which prints the timestamp of the trigger");
// sets a short description for the component
SetDescription("Use this filter to provide a client which accesses the printer interface from Demo Interface Printer (see Streaming Sources).");
// set help link to jump to documentation from ADTF Configuration Editor
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_calculator_client.html");
// describe and register property we will set via the rpc interface
m_oServerDelay.SetDescription("To construct an example that should show how you can make a server call with an error return, you can set an artificial delay on the server here");
RegisterPropertyVariable("server_delay", m_oServerDelay);
}
cCalculationClientFilter::~cCalculationClientFilter() = default;
tResult cCalculationClientFilter::Process(tNanoSeconds /*tmTimeOfTrigger*/, IRunner * /*pRunner*/)
{
// call remote function
LOG_INFO("Sum: %d", m_oInterfaceClient->Add(2, 1));
}
tResult cCalculationClientFilter::Init(tInitStage eStage)
{
RETURN_IF_FAILED(cFilter::Init(eStage));
if (eStage == tInitStage::StageGraphReady)
{
// use of error code in rpc interface to stop initialisation if server dosn't except parameter
RETURN_IF_FAILED(m_oInterfaceClient->SetDelay(m_oServerDelay));
}
};
Namespace for the ADTF Streaming SDK.
Definition bindingproxyinport.h:14

Filter Interface

demo_rpc_interface_filter.h

#pragma once
#include "calculator_intf.h"
#ifndef ADTF_EXAMPLES_CID
#define ADTF_EXAMPLES_CID ".local.cid"
#endif
#define CID_DEMO_ADTF_RPC_TO_INTERFACE_FILTER "demo_calculator_rpc_interface.filter" ADTF_EXAMPLES_CID
class cRPCInterfaceFilter final : public adtf::filter::cFilter
{
public:
ADTF_CLASS_ID_NAME(cRPCInterfaceFilter, CID_DEMO_ADTF_RPC_TO_INTERFACE_FILTER, "Demo Calculator RPC Interface Provider");
public: // overrides cService
cRPCInterfaceFilter();
~cRPCInterfaceFilter() override;
tResult Init(tInitStage) override;
private:
adtf::base::property_variable<adtf::util::cString> m_strUrl{ "$(ADTF_REMOTE_CONTROL_URL)/calculator" };
};

demo_rpc_interface_filter.cpp

#include <adtf_filtersdk.h>
using namespace adtf::ucom;
using namespace adtf::base;
using namespace adtf::streaming;
#include "demo_rpc_interface_filter.h"
#include "demo_rpc_remote_interface.h"
cRPCInterfaceFilter::cRPCInterfaceFilter()
{
// sets a short description for the component
SetDescription("Use this filter to convert the rpc interface into a filter interface to provide the calculator functionality to other filters.");
// set help link to jump to documentation from ADTF Configuration Editor
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_calculator_rpc_interface_provider.html");
// describe and register property
m_strUrl.SetDescription("URL to the server object");
RegisterPropertyVariable("url", m_strUrl);
}
cRPCInterfaceFilter::~cRPCInterfaceFilter() = default;
tResult cRPCInterfaceFilter::Init(tInitStage oStage)
{
RETURN_IF_FAILED(cFilter::Init(oStage));
if(oStage == tInitStage::StageFirst)
{
// create a interface server to provide the remote interface via the calculator pin
CreateInterfaceServer<ICalculator>("calculator",
SetDescription("calculator", "Calculator interface example via RPC");
}
}
object_ptr< T > ucom_object_ptr_cast(object_ptr< T > oCasted)
Alias always bringing the latest version of ant::ucom_object_ptr_cast() into scope.
Definition object_ptr_utilities.h:104

Plugin

plugin.cpp

#include "demo_string_sending_filter.h"
#include "demo_string_receiving_filter.h"
ADTF_PLUGIN("Demo String Handling Filters Plugin",
cDemoStringSending,
cDemoStringReceiving);
#define ADTF_PLUGIN(__plugin_identifier,...)
Definition adtf_plugin.h:29