ADTF  3.18.3
rpc_stubs.h
Go to the documentation of this file.
1 
7 #pragma once
8 
11 #include "adtf_client_connector.h"
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>
20 #include <adtfucom3/adtf_ucom3.h>
21 #include <adtfbase/string_intf.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(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 
91 namespace adtf
92 {
93 namespace remote
94 {
95 namespace hollow
96 {
97 
99 {
100 
101 public:
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 
194 template <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 {
201 public:
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 
223 protected:
228  Stub& GetStub() const
229  {
230  return *const_cast<Stub*>(static_cast<const Stub*>(this));
231  }
232 
233 };
234 
235 template <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 }
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
A_UTILS_NS::cResult tResult
For backwards compatibility and to bring latest version into scope.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
const tChar * GetDescription() const
Get user provided error description.
tInt32 GetLine() const
Get line in source file where the error was reported.
tErrorCode GetErrorCode() const
Get error code.
const tChar * GetFunction() const
Get name of the function the error was reported in.
const tChar * GetFile() const
Get name of the file the error was reported in.
Represents a JSON value.
Definition: value.h:177
A common result class usable as return value throughout.
const char * getFunction() const
Get name of the function the error was reported in.
const char * getFile() const
Get name of the file the error was reported in.
std::int32_t getLine() const
Get line in source file where the error was reported.
const char * getDescription() const
Get user provided error description.
std::int32_t getErrorCode() const
Get error code.
The IRawMemory interface provides methods for getting and setting memory values through abstract inte...
virtual size_t GetSize() const =0
Returns the size in bytes of the memory.
virtual const void * Get() const =0
Returns the raw memory pointer.
The IString interface provides methods for getting and setting strings through abstract interfaces.
Definition: string_intf.h:28
rpc_remote_interface(const adtf::util::cString &strUrl)
Constructor.
Definition: rpc_stubs.h:206
Stub & GetStub() const
Access the rpc stub.
Definition: rpc_stubs.h:228
Use this template if you want to implement an ucom::ant::IObject based Interface and/or subclass an e...
Definition: object.h:379
Template to implement an RPC object.
Definition: json_rpc.h:162
Template for direct usage of the stub methods.
Definition: json_rpc.h:77
jsonrpc_remote_object(const adtf::util::cString &oInitializer)
Constructor.
Definition: json_rpc_impl.h:23
RPC Protocol declaration.
cString to_string(const tResult &i_oResult, eResultFormatFlags i_eFormatFlags=eResultFormatFlags::RFF_DisableNone, const tChar *i_strFormat=nullptr)
Copy all information of an assigned result object to a (formatted) string.
string_base< cStackString > cString
cString implementation for a stack string which works on stack if string is lower than A_UTILS_DEFAUL...
Definition: string.h:2778
Namespace for entire ADTF SDK.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
Copyright © Audi Electronics Venture GmbH.
RPC Protocol declaration.
Copyright © Audi Electronics Venture GmbH.
Common include for component a_util::result.