DAVID4 SDK  1.8.7
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
ClientJsonRpc.h
1 //=============================================================================
2 // See License in Related Pages
3 //=============================================================================
4 
5 #pragma once
6 
7 #ifndef DAVID_SDK_CLIENT_JSON_RPC_H
8 #define DAVID_SDK_CLIENT_JSON_RPC_H
9 
10 #include "davidSDK/IModules.h"
11 #include "davidSDK/TcpClient.h"
12 #include "davidSDK/json/json.h"
13 //#include "davidSDK/jsonrpc/jsonrpc.h"
14 //#include "davidSDK/JsonRpcFunctions.h"
15 
16 namespace david {
17 
18 //*****************************************************************************
19 /// @addtogroup ManagerGroup
20 /// @{
21 /// @defgroup ClientJsonRpcGroup Client for DAVID Enterprise Server
22 /// IModules implementation for connection to DAVID Enterprise Server.
23 /// @{
24 //*****************************************************************************
25 
26 //=============================================================================
27 // ServerInfo
28 //=============================================================================
29 
30 /// Information about the server.
31 struct ServerInfo
32 {
33  bool licenseOk; ///< Is license ok?
34  std::string serverVersion; ///< Describes the version of the server.
35 
36  /// Constructor.
37  ServerInfo() : licenseOk(false) {}
38 };
39 
40 
41 //=============================================================================
42 // ClientJsonRpc
43 //=============================================================================
44 
45 /// Implementation of IModules and client using Json RPC 2.0.
46 /// The client connects to a running DAVID Enterprise Server.
47 class ClientJsonRpc : public IModules
48 {
49 public:
50  /// Constructor.
51  ClientJsonRpc();
52 
53  /// Destructor.
54  virtual ~ClientJsonRpc();
55 
56  /// Connect to a running 'DAVID Enterprise Server' instance.
57  /// @param[in] address IP address of the running server. Use 127.0.0.1 for local host.
58  /// @param[in] port Port of server connection.
59  /// @return Some information about the server.
60  /// @exception david::Error_RpcClientVersion (david::FunctionException) Client version not compatible.
61  ServerInfo Connect(const std::string& address = "127.0.0.1", uint16_t port = DAVID_SDK_DefaultPort);
62 
63  /// Disconnect from server.
64  /// @param[in] stopServer Stop remote server?
65  virtual void Disconnect(bool stopServer);
66 
67  // See IModules
68  virtual IStructuredLightScanner& sls();
69 
70  // See IModules
71  virtual IShapeFusion& fusion();
72 
73  // See IModules
74  virtual ITurntable& turntable();
75 
76  // See IModules
77  virtual IMeasure& measure();
78 
79  // See IModules
80  virtual IMainWindow& mainWindow();
81 
82  /// Execute a remote procedure call all via RPC channel.
83  /// @param[out] response Response from server.
84  /// @param[in] rpc Remote procedure call.
85  void ExecuteRpc(Json::Value& response, const Json::Value& rpc);
86 
87  /// Prepare a remote procedure call.
88  ///
89  /// This following variables are set:
90  /// @code
91  /// rpc["jsonrpc"] = "2.0";
92  /// rpc["method"] = method;
93  /// rpc["id"] = newID;
94  /// @endcode
95  ///
96  /// @param[in,out] rpc Remote procedure call.
97  /// @param[in] method Name of the method.
98  void PrepareRpc(Json::Value& rpc, const std::string method);
99 
100  /// Read binary data from binary channel into buffer.
101  /// @param[out] buffer Destination buffer for binary data.
102  /// @param[in] bufferSize Size of buffer in bytes.
103  void ReadBinaryData(void* buffer, size_t bufferSize);
104 
105  /// Extract a parameter value from Json value.
106  /// @param[in] jsonParams Subtree of Json values.
107  /// @param[in] paramName Name of parameter to be extracted.
108  /// @return Value of parameter.
109  /// @exception david::Error_RpcInvalidParams Parameter not available or of wrong type.
110  template<class T>
111  T ExtractJsonParam(const Json::Value& jsonParams, const std::string& paramName)
112  {
113  T value;
114 
115  const Json::Value& v = jsonParams[paramName];
116  if (v.type() == Json::nullValue)
117  {
118  ThrowException(david::Error_RpcInvalidParams, "Missing parameter " + paramName, "ClientImpl::ExtractJsonParam");
119  }
120 
121  try
122  {
123  GetJsonParamValue(value, v);
124  }
125  catch (std::runtime_error&)
126  {
127  ThrowException(david::Error_RpcInvalidParams, "Wrong type of parameter " + paramName, "ClientImpl::ExtractJsonParam");
128  }
129 
130  return value;
131  }
132 
133  /// Extract a parameter value from Json value.
134  /// @param[out] optionalValue Gets the value, if one is available.
135  /// @param[in] jsonParams Subtree of Json values.
136  /// @param[in] paramName Name of parameter to be extracted.
137  /// @exception david::Error_RpcInvalidParams Parameter is of wrong type.
138  template<class T>
139  void ExtractJsonParam(Optional<T>& optionalValue, const Json::Value& jsonParams, const std::string& paramName)
140  {
141  const Json::Value& v = jsonParams[paramName];
142  if (v.type() != Json::nullValue)
143  {
144  try
145  {
146  T value;
147  GetJsonParamValue(value, v);
148  optionalValue = value;
149  }
150  catch (std::runtime_error&)
151  {
152  ThrowException(david::Error_RpcInvalidParams, "Wrong type of parameter " + paramName, "ClientImpl::ExtractJsonParam");
153  }
154  }
155  }
156 
157 private:
158  void GetJsonParamValue(int &result, const Json::Value &v) { result=v.asInt(); }
159  void GetJsonParamValue(uint32_t &result, const Json::Value &v) { result=v.asUInt(); }
160  void GetJsonParamValue(bool &result, const Json::Value &v) { result=v.asBool(); }
161  void GetJsonParamValue(double &result, const Json::Value &v) { result=v.asDouble(); }
162  void GetJsonParamValue(float &result, const Json::Value &v) { result=(float)v.asDouble(); }
163  void GetJsonParamValue(std::string &result, const Json::Value &v) { result=v.asString(); }
164 
165  template <class T>
166  void GetJsonParamValue(std::vector<T> &result, const Json::Value &v)
167  {
168  if (!v.isArray()) throw std::runtime_error("not an array");
169 
170  result.resize(v.size());
171  for (Json::Value::UInt i=0; i<v.size(); ++i) GetJsonParamValue(result[i], v[i]);
172  }
173 
174 
175 private:
176  IStructuredLightScanner* m_sls; ///< IStructuredLightScanner implementation.
177  IShapeFusion* m_fusion; ///< IShapeFusion implementation.
178  IMeasure* m_measure; ///< IMeasure implementation.
179  ITurntable* m_turntable; ///< ITurntable implementation.
180  IMainWindow* m_mainWindow; ///< IMainWindow implementation.
181  TcpClient* m_client; ///< JsonRpc connection to server.
182  TcpClient* m_binary; ///< Binary connection to server.
183  Json::StyledWriter m_writer; ///< Json writer.
184  Json::Reader m_reader; ///< Json reader.
185  int m_messageID; ///< Every message gets a unique ID.
186 };
187 
188 /// @} ClientJsonRpcGroup
189 /// @} ManagerGroup
190 
191 } // namespace
192 
193 #endif // DAVID_SDK_CLIENT_JSON_RPC_H