ADTF  3.18.2
Source Code for Demo RPC Calculator Server and Client 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 an object that accepts remote calls
  • how to call methods of a remote object
Header (Server)
#pragma once
#define CID_DEMO_ADTF_RPC_SERVER "demo_calculator_rpc_server.service.adtf.cid"
class cRPCServerService: public cADTFService
{
public:
ADTF_CLASS_ID_NAME(cRPCServerService, CID_DEMO_ADTF_RPC_SERVER, "Demo Calculator RPC Server");
public: // overrides cService
cRPCServerService();
virtual tResult ServiceInit() override;
virtual tResult ServiceShutdown() override;
};
#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
Implementation (Server)
#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 System 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/adtf_html/page_demo_rpc.html");
}
tResult cRPCServerService::ServiceInit()
{
RETURN_IF_FAILED(cADTFService::ServiceInit());
// create our server object
object_ptr<adtf::remote::IRPCObjectServer> pServerObject = make_object_ptr<cCalculator>();
// 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);
}
#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 System SDK.
Namespace for the ADTF uCOM3 SDK.
Header (Client)
#pragma once
#define CID_DEMO_ADTF_RPC_CLIENT "demo_calculator_rpc_client.service.adtf.cid"
class cRPCClientService: public cADTFService
{
public:
ADTF_CLASS_ID_NAME(cRPCClientService, CID_DEMO_ADTF_RPC_CLIENT, "Demo Calculator RPC Client");
public: // overrides cService
cRPCClientService();
virtual tResult ServiceInit() override;
};
Implementation (Client)
#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 System 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/adtf_html/page_demo_rpc.html");
// set default runlevel
SetDefaultRunlevel(tADTFRunLevel::RL_Session);
}
tResult cRPCClientService::ServiceInit()
{
RETURN_IF_FAILED(cADTFService::ServiceInit());
// direct use of the stub methods
{
adtf::remote::remote_object<rpc_stubs::cCalculatorClientStub> oRemoteCalculator("http://localhost:8000/calculator");
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>("http://localhost:8000/calculator");
int nTwo = pRemoteCalculator->Multiply(1, 2);
LOG_INFO("The result of 1 * 2 equals %d", nTwo);
}
}
Template for direct usage of the stub methods.
Definition: json_rpc.h:77
@ RL_Session
The session level.
Definition: adtf_runtime.h:30