-
Notifications
You must be signed in to change notification settings - Fork 0
/
rempos.h
69 lines (51 loc) · 1.58 KB
/
rempos.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#pragma once
#include <thread>
#include <mutex>
#include "senzor_server.h"
namespace rempos {
class RemPos {
public:
RemPos(uint16_t port)
: server_(GenerateCallback()),
worker_(&RemPos::RunServerByPort, this, port) {}
RemPos(std::string ip, std::string port)
: server_(GenerateCallback()),
worker_(&RemPos::RunServerByIPAndPort, this, std::move(ip), std::move(port)) {}
RemPos(RemPos&) = delete;
RemPos(RemPos&&) = delete;
~RemPos() {
server_.Stop();
worker_.join();
}
public:
RemPos& operator=(RemPos&) = delete;
RemPos& operator=(RemPos&&) = delete;
public:
// Gets last data packet. I'm not sure if we should collect all packets in a queue to be read.
[[nodiscard]] inline senzor_server::SenzorResult GetData() const {
std::lock_guard lock(guard_);
return last_data_;
}
private:
[[nodiscard]] inline senzor_server::Server::CallbackType GenerateCallback() {
return [this](senzor_server::SenzorResult&& data) {
this->UpdateData(std::move(data));
};
}
void RunServerByPort(uint16_t port) {
server_.Run(port);
}
void RunServerByIPAndPort(std::string ip, std::string port) {
server_.Run(ip, port);
}
inline void UpdateData(senzor_server::SenzorResult&& data) {
std::lock_guard lock(this->guard_);
last_data_ = std::move(data);
}
private:
senzor_server::SenzorResult last_data_;
mutable std::mutex guard_;
senzor_server::Server server_;
std::thread worker_;
};
} // namespace rempos