ADTF
Loading...
Searching...
No Matches
rpc_stubs.h
Go to the documentation of this file.
1
7#pragma once
8
12
13#include <jsonrpccpp/server/abstractserverconnector.h>
14#include <a_util/result.h>
15#include <rpc/json_rpc.h>
16#include <rpc/rpc_server.h>
17#include <json/value.h>
18
19#include <adtf_utils.h>
23
24#include <sstream>
25
26#ifndef EXCEPTION_TO_DEFAULT
28#define EXCEPTION_TO_DEFAULT(__default, __body) \
29 try \
30 { \
31 __body \
32 } \
33 catch (...) \
34 { \
35 return __default; \
36 }
37#endif
38
39#ifndef EXCEPTION_TO_DEFAULT_AND_LOG
41 #define EXCEPTION_TO_DEFAULT_AND_LOG(__default, __body) \
42 try \
43 { \
44 __body \
45 } \
46 catch (const std::exception& oEx) \
47 { \
48 LOG_ERROR("%s", oEx.what()); \
49 return __default; \
50 } \
51 catch (const tResult& oResult) \
52 { \
53 LOG_RESULT(oResult); \
54 return __default; \
55 } \
56 catch (...) \
57 { \
58 return __default; \
59 }
60#endif
61
62#ifndef EXCEPTION_TO_RESULT
64#if A_UTILS_VERSION_MAJOR < 4
65 #define EXCEPTION_TO_RESULT(__body) \
66 try \
67 { \
68 __body \
69 } \
70 catch (...) \
71 { \
72 return a_util::result::Result(ERR_FAILED); \
73 }
74#else
75 #define EXCEPTION_TO_RESULT(__body) \
76 try \
77 { \
78 __body \
79 } \
80 catch (const std::exception& oEx) \
81 { \
82 RETURN_ERROR_DESC(ERR_FAILED, "RPC call failed: %s", oEx.what()); \
83 } \
84 catch (...) \
85 { \
86 RETURN_ERROR(ERR_FAILED); \
87 }
88#endif
89#endif
90
91namespace adtf
92{
93namespace remote
94{
95namespace hollow
96{
97
99{
100
101public:
102
103 static inline tResult ToResult(const Json::Value& oValue)
104 {
105 if (oValue["ErrorCode"].asInt() == ERR_NOERROR.error_code)
106 {
107 return tResult(ERR_NOERROR);
108 }
109 else
110 {
111 return tResult(oValue["ErrorCode"].asInt(),
112 oValue["Description"].asString().c_str(),
113 oValue["Line"].asInt(),
114 oValue["File"].asString().c_str(),
115 oValue["Function"].asString().c_str());
116 }
117 }
118
119 static inline a_util::result::Result ToResult5(const tResult& oResult)
120 {
121 return a_util::result::Result(oResult.GetErrorCode().value,
122 oResult.GetDescription(),
123 oResult.GetLine(),
124 oResult.GetFile(),
125 oResult.GetFunction());
126 }
127
128 static inline tResult ToResult4(const a_util::result::Result& oResult)
129 {
130 return tResult(oResult.getErrorCode(),
131 oResult.getDescription(),
132 oResult.getLine(),
133 adtf::util::cFilename(oResult.getFile()).GetName(),
134 oResult.getFunction());
135 }
136
137 static inline Json::Value ToJson(const tResult & nResult)
138 {
139 Json::Value oResult;
140 oResult["ErrorCode"] = nResult.GetErrorCode().value;
141 oResult["Description"] = nResult.GetDescription();
142 oResult["Line"] = nResult.GetLine();
143 oResult["File"] = nResult.GetFile();
144 oResult["Function"] = nResult.GetFunction();
145 return oResult;
146 }
147
148 static inline std::string ToString(uint64_t nValue)
149 {
150#ifdef HAVE_STD_TO_STRING
151 return std::to_string(nValue);
152#else
153 // Bug in g++ 5.x.x header file, not having std::to_string for C++11
154 std::ostringstream stream;
155 stream << nValue;
156 return stream.str();
157#endif
158 }
159
160 static inline std::string ToString(int64_t nValue)
161 {
162#ifdef HAVE_STD_TO_STRING
163 return std::to_string(nValue);
164#else
165 // Bug in g++ 5.x.x header file, not having std::to_string for C++11
166 std::ostringstream stream;
167 stream << nValue;
168 return stream.str();
169#endif
170 }
171
172 static inline int64_t Stoll(const std::string& strValue)
173 {
174#ifdef HAVE_STD_STOLL
175 return std::stoll(strValue);
176#else
177 // Bug in g++ 5.x.x header file, not having std::sto(u)ll for C++11
178 return strtoll(strValue.c_str(), 0, 10);
179#endif
180 }
181
182 static inline uint64_t Stoull(const std::string& strValue)
183 {
184#ifdef HAVE_STD_STOLL
185 return std::stoull(strValue);
186#else
187 // Bug in g++ 5.x.x header file, not having std::sto(u)ll for C++11
188 return strtoull(strValue.c_str(), 0, 10);
189#endif
190 }
191
192};
193
194template <typename Stub, typename Interface>
196 private rpc::jsonrpc_remote_object<Stub, hollow::cADTFClientConnector, adtf::util::cString>,
197 public IRPCObjectClient,
198 protected cAdtfJsonConverter,
199 public Interface
200{
201public:
206 rpc_remote_interface(const adtf::util::cString& strUrl)
207 : rpc::jsonrpc_remote_object<Stub, hollow::cADTFClientConnector, adtf::util::cString>(strUrl)
208 {
209 }
210
211 tResult GetRPCIID(base::ant::IString&& strIID) const
212 {
213 strIID.Set(adtf::ucom::get_iid<Interface>());
215 }
216
217 tResult GetName(base::ant::IString&& strName) const
218 {
219 strName.Set(Interface::DEFAULT_NAME);
221 }
222
223protected:
228 Stub& GetStub() const
229 {
230 return *const_cast<Stub*>(static_cast<const Stub*>(this));
231 }
232
233};
234
235template <typename ServerStub, typename Interface>
237 private rpc::jsonrpc_object_server<ServerStub>,
238 protected cAdtfJsonConverter,
239 public ucom::catwo::object<remote::ant::IRPCObjectServer, remote::catwo::IRPCInterfaceDefinition>
240{
241 public:
242 tResult Receive(const base::ant::IRawMemory& oRequest,
243 base::ant::IRawMemory&& oResponse) override
244 {
245 try
246 {
247 auto pData = static_cast<const char*>(oRequest.Get());
248 std::string strRequest(pData, pData + oRequest.GetSize());
249 std::string strResponse;
251 oResponse.Set(strResponse.c_str(), strResponse.size());
252 }
253 catch(...)
254 {
255 RETURN_ERROR(ERR_UNEXPECTED);
256 }
257
259 }
260
261 tResult GetRPCIID(base::ant::IString&& strIID) const override
262 {
263 return strIID.Set(adtf::ucom::get_iid<Interface>());
264 }
265
266 tResult GetRPCName(base::ant::IString&& strRPCName) const override
267 {
268 return strRPCName.Set(m_strRPCObjectName);
269 }
270
271 tResult SetRPCName(const char* strRPCName) override
272 {
273 m_strRPCObjectName = strRPCName;
275 }
276
277 tResult GetInterfaceDefinition(base::ant::IString&& strInterfaceDefinition) const override
278 {
279 return strInterfaceDefinition.Set(ServerStub::interface_definition);
280 }
281
282 private:
283 adtf::util::cString m_strRPCObjectName;
284};
285
286}
287
291
292}
293}
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
Definition result.h:736
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
Definition result.h:29
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
Definition result.h:42
const tChar * GetFile() const noexcept
Get name of the file the error was reported in.
const tChar * GetDescription() const noexcept
Get user provided error description.
tInt32 GetLine() const noexcept
Get line in source file where the error was reported.
tErrorCode GetErrorCode() const noexcept
Get error code.
const tChar * GetFunction() const noexcept
Get name of the function the error was reported in.
Definition result_type_decl.h:34
const char * getFile() const
Definition result_type_impl.h:177
std::int32_t getLine() const
Definition result_type_impl.h:168
const char * getFunction() const
Definition result_type_impl.h:186
std::int32_t getErrorCode() const
Definition result_type_impl.h:136
const char * getDescription() const
Definition result_type_impl.h:154
Definition rawmemory_base.h:23
virtual size_t GetSize() const =0
virtual const void * Get() const =0
Definition string_intf.h:29
Definition rpc_object_client_intf.h:19
Definition adtf_client_connector.h:21
Definition rpc_stubs.h:240
rpc_remote_interface(const adtf::util::cString &strUrl)
Definition rpc_stubs.h:206
Stub & GetStub() const
Definition rpc_stubs.h:228
Definition object.h:397
Definition json_rpc.h:162
Definition json_rpc.h:77
jsonrpc_remote_object(const adtf::util::cString &oInitializer)
Definition json_rpc_impl.h:23
Namespace for all functionality of the ADTF Remote SDK provided since v3.7.
Definition adtf_client_connector.h:18
constexpr const char * get_iid() noexcept
Alias bringing the latest version of ant::get_iid() into scope.
Definition adtf_iid.h:429
Namespace for all functionality provided by ADTF and its SDKs.
Definition adtf_client_connector.h:14
definition of the rpc namespace
Definition json_rpc_impl.h:20