Motion Master
Loading...
Searching...
No Matches
proto_server.h
Go to the documentation of this file.
1#pragma once
2
3#include <uwebsockets/App.h>
4#include <atomic>
5#include <mutex>
6#include <thread>
7
10
12
25 public:
34 std::string id;
35 };
36
50 explicit ProtoServer(
52 incomingMachineMessageQueue,
54 outgoingMachineMessageQueue,
55 uint16_t reqResPort = 63524, uint16_t pubSubPort = 63525);
56
63
78 void start();
79
98 void stop();
99
108
109 private:
130 void handleUpgrade(uWS::HttpResponse<false>* res, uWS::HttpRequest* req,
131 us_socket_context_t* context,
132 bool checkDuplicateClientIds = false);
133
160 void runReqRes();
161
185 void runPubSub();
186
188 std::atomic<bool> running_ = false;
189
191 uint16_t reqResPort_ = 63524;
193 std::thread reqResThread_;
195 std::atomic<uWS::Loop*> reqResLoop_ = nullptr;
197 std::atomic<us_listen_socket_t*> reqResListenToken_ = nullptr;
199 std::unordered_map<std::string,
200 uWS::WebSocket<false, true, ProtoServer::ConnectionData>*>
201 reqResConnectionIdToWebSocketMap_;
202 std::mutex reqResConnectionIdToWebSocketMapMutex_;
203
205 uint16_t pubSubPort_ = 63525;
207 std::thread pubSubThread_;
209 std::atomic<uWS::Loop*> pubSubLoop_ = nullptr;
211 std::atomic<us_listen_socket_t*> pubSubListenToken_ = nullptr;
213 std::unordered_map<std::string,
214 uWS::WebSocket<false, true, ProtoServer::ConnectionData>*>
215 pubSubConnectionIdToWebSocketMap_;
216 std::mutex pubSubConnectionIdToWebSocketMapMutex_;
217
220 incomingMachineMessageQueue_;
223 outgoingMachineMessageQueue_;
224
226 std::thread serverThread_;
227
244 void runServer();
245
283 void sendOutgoingMachineMessages();
284
293 std::unordered_map<std::string,
294 std::tuple<std::string, std::set<std::string>>>
295 monitoringSubscriptionMap_;
296 std::mutex monitoringSubscriptionMapMutex_;
297};
~ProtoServer()
Destructor for ProtoServer.
Definition proto_server.cc:19
ProtoServer(mm::core::containers::ThreadSafeQueue< IncomingMachineMessage > &incomingMachineMessageQueue, mm::core::containers::ThreadSafeQueue< OutgoingMachineMessage > &outgoingMachineMessageQueue, uint16_t reqResPort=63524, uint16_t pubSubPort=63525)
Construct a new ProtoServer object.
Definition proto_server.cc:8
void start()
Starts the ProtoServer by launching its main threads.
Definition proto_server.cc:21
void clearMonitoringSubscriptions()
Clears all active monitoring subscriptions.
Definition proto_server.cc:106
void stop()
Stops the ProtoServer and performs a clean shutdown.
Definition proto_server.cc:37
Thread-safe FIFO queue with optional capacity limit.
Definition thread_safe_queue.h:23
Per-connection data for each WebSocket.
Definition proto_server.h:32
std::string id
Definition proto_server.h:34