forb
base_stub.hpp
1 //
2 // Created by gabriele on 04/11/18.
3 //
4 
5 #ifndef LIBFORB_BASE_STUB_HPP
6 #define LIBFORB_BASE_STUB_HPP
7 
8 #include <vector>
9 #include <string>
10 #include <memory>
11 #include <mutex>
12 
13 #include <forb/stream/shared_memory.hpp>
14 #include <forb/stream/socket.hpp>
15 #include <forb/stream/stream.hpp>
16 
17 namespace forb {
18  // Forward declaration of registry class
19  class remote_registry;
20 
21  /* ************************************************** ALIASES *************************************************** */
22 
23  // Forward declaration
24  class base_stub;
25 
27  using remote_ptr = std::shared_ptr<base_stub>;
28 
30  using remote_var = std::unique_ptr<base_stub>;
31 
32  /* *************************************************** CLASS **************************************************** */
33 
35  class base_stub {
38  friend class remote_registry;
39 
40  /* ************************************* ALIASES AND STATIC ATTRIBUTES ************************************** */
41  public:
44 
45  private:
49 
51  constexpr static buffer_size_t default_buffer_size = 1024;
52 
53  /* *********************************************** ATTRIBUTES *********************************************** */
54  protected:
56  std::mutex _mutex;
57 
60  stream *rpcstream = nullptr;
61 
64  stream *datastream = nullptr;
65 
66  private:
68  ssocket _socket;
69 
71  shared_memory _shmem;
72 
74  bool _force_socket = false;
75 
78  buffer_size_t _buffer_size = default_buffer_size;
79 
80  /* ********************************************** CONSTRUCTORS ********************************************** */
81  protected:
84  explicit base_stub() = default;
85 
86  /**************************************************************************************************************/
87  public:
89  virtual ~base_stub() noexcept = default;
90 
92  base_stub(const base_stub &other) = delete;
93 
95  base_stub &operator=(const base_stub &other) = delete;
96 
98  base_stub(base_stub &&other) = delete;
99 
101  base_stub &operator=(base_stub &&other) = delete;
102 
103  /**************************************************************************************************************/
104  public:
107  shared_memory::size_t buffer_size() const {
108  return _buffer_size;
109  };
110 
111 
112  protected:
114  void init_call(call_id_t code);
115 
119  // TODO: check wait return
120  void wait_return();
121 
122  private:
125  void force_socket() {
126  _force_socket = true;
127  };
128 
131  void set_buffer_size(buffer_size_t buffer_size) {
132  _buffer_size = buffer_size;
133  };
134 
137  void set_skeleton(std::string address, int port);
138 
139  /* ******************************************** FACTORY METHODS ********************************************* */
140  protected:
142  virtual bool _match(const std::string &type) const = 0;
143 
145  virtual remote_var _create_empty() const = 0;
146 
148  static std::vector<base_stub *> &_protos();
149 
150  private:
153  static remote_var _create(const std::string &type);
154  };
155 
156  /* ************************************************* TEMPLATES ************************************************** */
157 
159  template<typename T>
160  bool is_nil(const std::shared_ptr<T> &ptr) {
161  static_assert(std::is_base_of<base_stub, T>::value,
162  "The given type is not derived from forb::base_stub!");
163  return ptr == nullptr;
164  }
165 
167  template<typename T>
168  bool is_nil(const std::unique_ptr<T> &ptr) {
169  static_assert(std::is_base_of<base_stub, T>::value,
170  "The given type is not derived from forb::base_stub!");
171  return ptr == nullptr;
172  }
173 
174 
175 } // namespace forb
176 
177 
178 #endif //LIBFORB_BASE_STUB_HPP
virtual remote_var _create_empty() const =0
Creates a new empty stub object of the current type.
static std::vector< base_stub * > & _protos()
Returns a reference to the vector of prototypes of all known stub implementations.
Definition: base_stub.cpp:133
C++ wrapper of the Socket POSIX API, which uses IPv4 and blocking communication.
Definition: socket.hpp:28
bool is_nil(const std::shared_ptr< T > &ptr)
Returns true if the given pointer to a base_stub (or derived class) is null.
Definition: base_stub.hpp:160
The class from which any automatically generated stub will inherit.
Definition: base_stub.hpp:35
Virtual class that represents a stream.
Definition: stream.hpp:69
This function implements a memcpy abstract that is valid also for volatile data.
Definition: base_skeleton.hpp:15
Emulates a complete registry of all active remote objects that use this framework.
Definition: remote_registry.hpp:17
Implementation of the forbcc::strams::stream API that uses a Linux Shared Memory to communicate...
Definition: shared_memory.hpp:21
stream * rpcstream
The pointer to the stream that will be used to make call requests.
Definition: base_stub.hpp:60
base_stub()=default
Default constructor, creates an empty stub handle.
std::mutex _mutex
The mutex that avoids concurrent call requests using the same stub handle.
Definition: base_stub.hpp:56
forb::streams::shared_memory::size_t buffer_size_t
The type of the buffer size.
Definition: base_stub.hpp:43
void wait_return()
Waits the termination of the call request.
Definition: base_stub.cpp:107
shared_memory::size_t buffer_size() const
The size of the buffer within the shared memory (if shared memory is adopted as data exchange stream)...
Definition: base_stub.hpp:107
stream * datastream
The pointer to the stream that will be used to exchange data.
Definition: base_stub.hpp:64
std::shared_ptr< base_stub > remote_ptr
Shared pointer to a base_class object (or a derived class).
Definition: base_stub.hpp:27
void init_call(call_id_t code)
Initializes a new call request.
Definition: base_stub.cpp:80
uint16_t call_id_t
The type that will be used to transmit the call_id.
Definition: declarations.hpp:12
virtual ~base_stub() noexcept=default
Virtual destructor.
std::size_t size_t
An unsigned size type.
Definition: stream.hpp:72
std::unique_ptr< base_stub > remote_var
Unique pointer to a base_class object (or a derived class).
Definition: base_stub.hpp:30
virtual bool _match(const std::string &type) const =0
Returns true of the type string equals the fully qualified name of the given stub class...