Caffa  1.1.0
C++ Application Framework for Embedded Systems with introspection
cafRpcClient.h
1 // ##################################################################################################
2 //
3 // Caffa
4 // Copyright (C) Kontur AS
5 //
6 // GNU Lesser General Public License Usage
7 // This library is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published by
9 // the Free Software Foundation; either version 2.1 of the License, or
10 // (at your option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT ANY
13 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE.
15 //
16 // See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
17 // for more details.
18 //
19 #pragma once
20 
21 #include "cafDocument.h"
22 #include "cafLogger.h"
23 #include "cafNotNull.h"
24 #include "cafPortableDataType.h"
25 #include "cafSession.h"
26 
27 #include <memory>
28 #include <string>
29 
30 namespace caffa
31 {
32 struct AppInfo;
33 }
34 
35 namespace caffa::rpc
36 {
37 class Client
38 {
39 public:
40  Client( const std::string& hostname, int port )
41  : m_hostname( hostname )
42  , m_port( port )
43  {
44  }
45  virtual ~Client() = default;
46 
47  virtual caffa::AppInfo appInfo() const = 0;
48  virtual std::shared_ptr<caffa::ObjectHandle> document( const std::string& documentId ) const = 0;
49  virtual std::vector<std::shared_ptr<caffa::ObjectHandle>> documents() const = 0;
50  virtual std::string execute( caffa::not_null<const caffa::ObjectHandle*> selfObject,
51  const std::string& methodName,
52  const std::string& jsonArguments ) const = 0;
53  virtual bool stopServer() = 0;
54  virtual void sendKeepAlive() = 0;
55 
61  virtual caffa::Session::Type checkSession() const = 0;
62  virtual void changeSession( caffa::Session::Type newType ) = 0;
63  virtual void destroySession() = 0;
64  virtual const std::string& sessionUuid() const = 0;
65  virtual void startKeepAliveThread() = 0;
66 
67  template <typename DataType>
68  void set( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, const DataType& value );
69 
70  template <typename DataType>
71  DataType get( const caffa::ObjectHandle* objectHandle, const std::string& fieldName ) const;
72 
73  virtual std::shared_ptr<caffa::ObjectHandle> getShallowCopyOfChildObject( const caffa::ObjectHandle* objectHandle,
74  const std::string& fieldName ) const = 0;
75 
76  virtual std::shared_ptr<caffa::ObjectHandle> getDeepCopyOfChildObject( const caffa::ObjectHandle* objectHandle,
77  const std::string& fieldName ) const = 0;
78  virtual void deepCopyChildObjectFrom( const caffa::ObjectHandle* objectHandle,
79  const std::string& fieldName,
80  const caffa::ObjectHandle* childObject ) = 0;
81 
82  virtual std::vector<std::shared_ptr<caffa::ObjectHandle>> getChildObjects( const caffa::ObjectHandle* objectHandle,
83  const std::string& fieldName ) const = 0;
84 
85  virtual void setChildObject( const caffa::ObjectHandle* objectHandle,
86  const std::string& fieldName,
87  const caffa::ObjectHandle* childObject ) = 0;
88 
89  virtual void
90  removeChildObject( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, size_t index ) = 0;
91 
92  virtual void clearChildObjects( const caffa::ObjectHandle* objectHandle, const std::string& fieldName ) = 0;
93 
94  virtual void insertChildObject( const caffa::ObjectHandle* objectHandle,
95  const std::string& fieldName,
96  size_t index,
97  const caffa::ObjectHandle* childObject ) = 0;
98 
99  const std::string& hostname() const { return m_hostname; }
100  int port() const { return m_port; }
101 
102 private:
103  virtual void
104  setJson( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, const nlohmann::json& value ) = 0;
105  virtual nlohmann::json getJson( const caffa::ObjectHandle*, const std::string& fieldName ) const = 0;
106 
107 private:
108  std::string m_hostname;
109  int m_port;
110 };
111 
112 //--------------------------------------------------------------------------------------------------
114 //--------------------------------------------------------------------------------------------------
115 template <typename DataType>
116 DataType caffa::rpc::Client::get( const caffa::ObjectHandle* objectHandle, const std::string& fieldName ) const
117 {
118  nlohmann::json jsonValue = getJson( objectHandle, fieldName );
119  return jsonValue.get<DataType>();
120 }
121 
122 //--------------------------------------------------------------------------------------------------
124 //--------------------------------------------------------------------------------------------------
125 template <typename DataType>
126 void caffa::rpc::Client::set( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, const DataType& value )
127 {
128  nlohmann::json jsonValue = value;
129  setJson( objectHandle, fieldName, jsonValue );
130 }
131 
132 } // namespace caffa::rpc
Definition: cafObjectHandle.h:55
Definition: cafRpcClient.h:38
DataType get(const caffa::ObjectHandle *objectHandle, const std::string &fieldName) const
Get a value through RPC.
Definition: cafRpcClient.h:116
void set(const caffa::ObjectHandle *objectHandle, const std::string &fieldName, const DataType &value)
Set a value through RPC.
Definition: cafRpcClient.h:126
virtual caffa::Session::Type checkSession() const =0
Check the session. Will return a session type (including possibly INVALID) if the session exists....
Main Caffa namespace.
Definition: __init__.py:1
Basic Application Information.
Definition: cafApplication.h:39