ADTF  3.18.2
Source Code for Demo RPC Calculator Interface and Filter Plugin
Location
./src/examples/src/adtf/system_services/rpc/calculator/
Namespace for entire ADTF SDK.
Build Environment
To see how to set up the build environment have a look at ADTF CMake Environment
this implementation shows:
  • how to implement a filter which converts a rpc interface into a interface for the Filter Graph
  • how to call methods of a remote object from a filter
Header (Interface Filter)
#pragma once
#include "calculator_intf.h"
#define CID_DEMO_ADTF_RPC_TO_INTERFACE_FILTER "demo_calculator_rpc_interface.filter.adtf.cid"
class cRPCInterfaceFilter: public adtf::filter::cFilter
{
public:
ADTF_CLASS_ID_NAME(cRPCInterfaceFilter, CID_DEMO_ADTF_RPC_TO_INTERFACE_FILTER, "Demo Calculator RPC Interface Filter");
public: // overrides cService
cRPCInterfaceFilter();
tResult Init(tInitStage) override;
private:
property_variable<adtf::util::cString> m_strUrl = { "http://localhost:8000/calculator" };
object_ptr<adtf::streaming::cBindingServer> m_pBindingServer;
};
#define ADTF_CLASS_ID_NAME(_class, _strcid, _strclabel)
Common macro to enable correct treatment of class identifier AND Class Name by IClassInfo.
Definition: class_id.h:33
tResult Init(tInitStage eStage) override
Initializes the filter.
Implementation (Interface Filter)
#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/adtf_html/page_demo_rpc.html");
// describe and register property
m_strUrl.SetDescription("URL to the server object");
RegisterPropertyVariable("url", m_strUrl);
}
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",
ucom_object_ptr_cast<ICalculator>( make_object_ptr<cRemoteCalculator>(m_strUrl)));
SetDescription("calculator", "Calculator interface example via RPC");
}
}
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Namespace for the ADTF Base SDK.
Namespace for the ADTF Streaming SDK.
Namespace for the ADTF uCOM3 SDK.
Header (Client Filter)
#pragma once
#include "calculator_intf.h"
#define CID_DEMO_ADTF_CALCULATION_FILTER "demo_calculator_client_filter.filter.adtf.cid"
class cCalculationClientFilter: public adtf::filter::cFilter
{
public:
ADTF_CLASS_ID_NAME(cCalculationClientFilter, CID_DEMO_ADTF_CALCULATION_FILTER, "Demo Calculator Filter");
public: // overrides cFilter
cCalculationClientFilter();
adtf::streaming::IRunner* pRunner) override;
tResult Init(tInitStage eStage) override;
private:
property_variable<int32_t> m_oServerDelay = { 0 };
};
virtual tResult Process(base::flash::tNanoSeconds tmTrigger, streaming::ant::IRunner *pRunner)
The default Runner function of the graph object.
Helper class that wraps a streaming::ant::IBindingClient.
Definition: graph_object.h:67
The Interface defines a runnable item of the GraphObjects providing a IRuntimeBehaviour.
Definition: runner_intf.h:24
Implementation (Client Filter)
#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/adtf_html/page_demo_rpc.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);
}
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));
}
};