Motion Master
Loading...
Searching...
No Matches
server.h
Go to the documentation of this file.
1#pragma once
2
3#include <ixwebsocket/IXConnectionState.h>
4#include <ixwebsocket/IXWebSocket.h>
5
6#include <atomic>
7#include <chrono>
8#include <thread>
9
12
14
15class Server {
16 public:
18 incoming_machine_message_queue,
20 outgoing_machine_message_queue);
21 ~Server();
22
26 void start();
27
31 void stop();
32
36 void wait();
37
38 /******************** CUSTOM WS CONNECTION STATE CLASS ********************/
39 class MotionMasterConnectionState : public ix::ConnectionState {
40 public:
41 MotionMasterConnectionState() : ConnectionState() {}
42
43 std::string getClientId() { return client_id_; }
44
45 bool setClientId(const std::string& client_id) {
46 if (client_id_.empty()) {
47 client_id_ = client_id;
48 return true;
49 } else {
50 return false;
51 }
52 }
53
54 static std::shared_ptr<ConnectionState>
56 return std::make_shared<MotionMasterConnectionState>();
57 }
58
59 protected:
60 std::string client_id_;
61 };
62
63 private:
64 std::unique_ptr<std::thread> server_thread_;
65 std::atomic_bool running_;
66
68 incoming_machine_message_queue_;
70 outgoing_machine_message_queue_;
71
72 // WebSockets
73 std::unordered_map<std::string /* client ID */,
74 std::shared_ptr<ix::WebSocket>>
75 dealer_ws_client_map_;
76 std::unordered_map<std::string /* client ID */,
77 std::shared_ptr<ix::WebSocket>>
78 sub_ws_client_map_;
79
80 std::unordered_map<
81 std::string /* client ID */,
83 dealer_ws_incoming_message_map_;
84
85 std::unordered_map<std::string /* topic */,
86 std::vector<std::string> /* client IDs */>
87 topic_to_client_ids_map_;
88
89 std::unordered_map<std::string /* request message ID */,
90 std::string /* topic */>
91 request_id_to_topic_map_;
92
93 std::mutex mutex_;
94
98 void run();
99
105 void dealer_ws_close_clean_up(const std::string& client_id);
106
112 void sub_ws_close_clean_up(const std::string& client_id);
113
117 void ws_clean_up();
118
122 void send_outgoing_messages();
123};
MotionMasterConnectionState()
Definition server.h:41
std::string getClientId()
Definition server.h:43
bool setClientId(const std::string &client_id)
Definition server.h:45
std::string client_id_
Definition server.h:60
static std::shared_ptr< ConnectionState > createMotionMasterConnectionState()
Definition server.h:55
void stop()
Stop the Server.
Definition server.cc:337
void wait()
block until the Server finishes execution
Definition server.cc:339
~Server()
Definition server.cc:24
Server(mm::core::containers::ThreadSafeQueue< IncomingMachineMessage > &incoming_machine_message_queue, mm::core::containers::ThreadSafeQueue< OutgoingMachineMessage > &outgoing_machine_message_queue)
Definition server.cc:16
void start()
Start the Server.
Definition server.cc:332
Thread-safe FIFO queue with optional capacity limit.
Definition thread_safe_queue.h:23