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  virtual bool isReady( caffa::Session::Type type ) const = 0;
56 
57  virtual void
58  createSession( caffa::Session::Type type, const std::string& username = "", const std::string& password = "" ) = 0;
64  virtual caffa::Session::Type checkSession() const = 0;
65  virtual void changeSession( caffa::Session::Type newType ) = 0;
66  virtual void destroySession() = 0;
67  virtual const std::string& sessionUuid() const = 0;
68  virtual void startKeepAliveThread() = 0;
69 
70  template <typename DataType>
71  void set( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, const DataType& value );
72 
73  template <typename DataType>
74  DataType get( const caffa::ObjectHandle* objectHandle, const std::string& fieldName ) const;
75 
76  virtual std::shared_ptr<caffa::ObjectHandle> getChildObject( const caffa::ObjectHandle* objectHandle,
77  const std::string& fieldName ) const = 0;
78 
79  virtual std::vector<std::shared_ptr<caffa::ObjectHandle>> getChildObjects( const caffa::ObjectHandle* objectHandle,
80  const std::string& fieldName ) const = 0;
81 
82  virtual void setChildObject( const caffa::ObjectHandle* objectHandle,
83  const std::string& fieldName,
84  const caffa::ObjectHandle* childObject ) = 0;
85 
86  virtual void
87  removeChildObject( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, size_t index ) = 0;
88 
89  virtual void clearChildObjects( const caffa::ObjectHandle* objectHandle, const std::string& fieldName ) = 0;
90 
91  virtual void insertChildObject( const caffa::ObjectHandle* objectHandle,
92  const std::string& fieldName,
93  size_t index,
94  const caffa::ObjectHandle* childObject ) = 0;
95 
96  const std::string& hostname() const { return m_hostname; }
97  int port() const { return m_port; }
98 
99 private:
100  virtual void
101  setJson( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, const nlohmann::json& value ) = 0;
102  virtual nlohmann::json getJson( const caffa::ObjectHandle*, const std::string& fieldName ) const = 0;
103 
104 private:
105  std::string m_hostname;
106  int m_port;
107 };
108 
109 //--------------------------------------------------------------------------------------------------
111 //--------------------------------------------------------------------------------------------------
112 template <typename DataType>
113 DataType caffa::rpc::Client::get( const caffa::ObjectHandle* objectHandle, const std::string& fieldName ) const
114 {
115  nlohmann::json jsonValue = getJson( objectHandle, fieldName );
116  return jsonValue.get<DataType>();
117 }
118 
119 //--------------------------------------------------------------------------------------------------
121 //--------------------------------------------------------------------------------------------------
122 template <typename DataType>
123 void caffa::rpc::Client::set( const caffa::ObjectHandle* objectHandle, const std::string& fieldName, const DataType& value )
124 {
125  nlohmann::json jsonValue = value;
126  setJson( objectHandle, fieldName, jsonValue );
127 }
128 
129 } // namespace caffa::rpc
DataType get(const caffa::ObjectHandle *objectHandle, const std::string &fieldName) const
Get a value through RPC.
Definition: cafRpcClient.h:113
Basic Application Information.
Definition: cafApplication.h:38
Definition: cafRpcClient.h:37
Definition: cafObjectHandle.h:47
void set(const caffa::ObjectHandle *objectHandle, const std::string &fieldName, const DataType &value)
Set a value through RPC.
Definition: cafRpcClient.h:123
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: cafApplication.h:30
Definition: cafRestAppService.h:28