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";
};
{
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>
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
#endif
#include <calculatorclientstub.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
{
public:
using rpc_remote_interface::rpc_remote_interface;
{
return GetStub().Add(nValue1, nValue2);
})
{
return GetStub().Subtract(nValue1, nValue2);
})
{
return GetStub().Multiply(nValue1, nValue2);
})
{
return GetStub().Divide(nValue1, nValue2);
})
{
return ToResult(
GetStub().SetDelay(nSeconds));
})
};
#define ADTF_CLASS_ID(_class, _strcid)
ADTF_CLASS_INFO_IMPL.
Definition class_id.h:91
Definition rpc_stubs.h:200
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
{
public:
ADTF_CLASS_ID_NAME(cRPCClientService, CID_DEMO_ADTF_RPC_CLIENT,
"Demo Calculator RPC Client");
public:
cRPCClientService();
~cRPCClientService() override;
virtual tResult ServiceInit()
override;
private:
};
#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>
#include "demo_rpc_client.h"
#include "calculator_intf.h"
#include "demo_rpc_remote_interface.h"
cRPCClientService::cRPCClientService()
{
SetDescription("Use this service to extend the ADTF System with a calculator client using RPC interfaces.");
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_calculator_rpc_client.html");
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());
{
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);
}
{
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
{
public:
ADTF_CLASS_ID_NAME(cRPCServerService, CID_DEMO_ADTF_RPC_SERVER,
"Demo Calculator RPC Server");
public:
cRPCServerService();
~cRPCServerService() override;
virtual tResult ServiceInit()
override;
virtual tResult ServiceShutdown()
override;
};
demo_rpc_server.cpp
#include <adtf_systemsdk.h>
#include "demo_rpc_server.h"
#include "demo_rpc_object_server.h"
cRPCServerService::cRPCServerService()
{
SetDescription("Use this service to extend the ADTF System with a calculator server using RPC interfaces.");
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_calculator_rpc_server.html");
}
cRPCServerService::~cRPCServerService() = default;
tResult cRPCServerService::ServiceInit()
{
RETURN_IF_FAILED(cADTFService::ServiceInit());
RETURN_IF_FAILED(adtf::remote::rpc_register_object_server(ICalculatorRPC::DEFAULT_NAME, pServerObject));
}
tResult cRPCServerService::ServiceShutdown()
{
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
{
public:
ADTF_CLASS_ID_NAME(cCalculationClientFilter, CID_DEMO_ADTF_CALCULATION_FILTER,
"Demo Calculator");
public:
cCalculationClientFilter();
~cCalculationClientFilter() override;
private:
};
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>
#include "demo_calculator_client_filter.h"
cCalculationClientFilter::cCalculationClientFilter()
{
m_oInterfaceClient = CreateInterfaceClient<ICalculator>("calculator_client");
SetDescription("calculator_client", "Interface client to call a calculator function from a server");
CreateRunner("trigger", cTimerTriggerHint(std::chrono::seconds{ 1 }));
SetDescription("trigger", "Runner to periodically trigger the function which prints the timestamp of the trigger");
SetDescription("Use this filter to provide a client which accesses the printer interface from Demo Interface Printer (see Streaming Sources).");
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_calculator_client.html");
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;
{
LOG_INFO(
"Sum: %d", m_oInterfaceClient->Add(2, 1));
}
tResult cCalculationClientFilter::Init(tInitStage eStage)
{
RETURN_IF_FAILED(cFilter::Init(eStage));
if (eStage == tInitStage::StageGraphReady)
{
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
{
public:
ADTF_CLASS_ID_NAME(cRPCInterfaceFilter, CID_DEMO_ADTF_RPC_TO_INTERFACE_FILTER,
"Demo Calculator RPC Interface Provider");
public:
cRPCInterfaceFilter();
~cRPCInterfaceFilter() override;
private:
};
demo_rpc_interface_filter.cpp
#include <adtf_filtersdk.h>
#include "demo_rpc_interface_filter.h"
#include "demo_rpc_remote_interface.h"
cRPCInterfaceFilter::cRPCInterfaceFilter()
{
SetDescription("Use this filter to convert the rpc interface into a filter interface to provide the calculator functionality to other filters.");
SetHelpLink("$(ADTF_DIR)/doc/html/page_demo_calculator_rpc_interface_provider.html");
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)
{
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"
cDemoStringSending,
cDemoStringReceiving);
#define ADTF_PLUGIN(__plugin_identifier,...)
Definition adtf_plugin.h:29