forb
base_skeleton.hpp
1 //
2 // Created by gabriele on 04/11/18.
3 //
4 
5 #ifndef LIBFORB_BASE_SKELETON_HPP
6 #define LIBFORB_BASE_SKELETON_HPP
7 
8 #include <mutex>
9 #include <thread>
10 #include <vector>
11 
12 #include <forb/stream/socket.hpp>
13 
14 
15 namespace forb {
16 
18  class base_skeleton {
19  /* ************************************* ALIASES AND STATIC ATTRIBUTES ************************************** */
20  private:
21  using thread_list_t = std::vector<std::thread>;
22 
23  /* *********************************************** ATTRIBUTES *********************************************** */
24  private:
26  int _port = 0;
27 
29  int _queue_size = 0;
30 
32  std::thread _server_thread;
33 
35  forb::streams::socket _incoming_calls;
36 
37  /* ********************************************** CONSTRUCTORS ********************************************** */
38  public:
42  explicit base_skeleton(int port, int queue_size = 10) : _port(port), _queue_size(queue_size) {};
43 
44  /**************************************************************************************************************/
45 
47  virtual ~base_skeleton() = default;
48 
50  base_skeleton(const base_skeleton &other) = delete;
51 
53  base_skeleton &operator=(const base_skeleton &other) = delete;
54 
56  base_skeleton(base_skeleton &&other) noexcept = default;
57 
59  base_skeleton &operator=(base_skeleton &&other) noexcept = default;
60 
61  /**************************************************************************************************************/
62  protected:
66  static void listen_thread_body(base_skeleton *impl);
67 
71  static void call_thread_body(base_skeleton *impl, forb::streams::socket call_socket);
72 
73  /**************************************************************************************************************/
74  public:
76  void start_server();
77 
82  void join_server();
83 
84  protected:
87  virtual void execute_call(call_id_t code,
88  forb::streams::stream *callstream,
89  forb::streams::stream *datastream) = 0;
90 
91  private:
93  void accept_connection();
94  };
95 }
96 
97 
98 #endif //LIBFORB_BASE_SKELETON_HPP
C++ wrapper of the Socket POSIX API, which uses IPv4 and blocking communication.
Definition: socket.hpp:28
virtual ~base_skeleton()=default
Virtual destructor.
static void call_thread_body(base_skeleton *impl, forb::streams::socket call_socket)
The body of the threads which will serve each call request.
Definition: base_skeleton.cpp:33
static void listen_thread_body(base_skeleton *impl)
The body of the server thread, which will block on accept calls and spawn other threads, one for each new request.
Definition: base_skeleton.cpp:19
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
virtual void execute_call(call_id_t code, forb::streams::stream *callstream, forb::streams::stream *datastream)=0
Virtual method that implementations shall define to actually serve a request call.
base_skeleton & operator=(const base_skeleton &other)=delete
This class does not support copy assignment.
base_skeleton(int port, int queue_size=10)
Constructs a new skeleton object associated with the given port.
Definition: base_skeleton.hpp:42
The class from which any automatically generated skeleton will inherit.
Definition: base_skeleton.hpp:18
void start_server()
The object will spawn a new thread ans start accepting connection requests.
Definition: base_skeleton.cpp:94
void join_server()
Joins the server thread, waiting for it to terminate, which happens only in case of an error within t...
Definition: base_skeleton.cpp:103
uint16_t call_id_t
The type that will be used to transmit the call_id.
Definition: declarations.hpp:12