-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
160 lines (121 loc) · 4.36 KB
/
main.cpp
File metadata and controls
160 lines (121 loc) · 4.36 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#if defined(__linux__) && defined(MRC_DAEMON_BUILD)
#include <signal.h>
#endif
#include <stdio.h>
#include "version.h"
#include <wpi/net/EventLoopRunner.hpp>
#include <wpi/net/uv/Timer.hpp>
#include <wpi/net/uv/Tcp.hpp>
#include "wpi/net/HttpUtil.hpp"
#include "wpi/net/ParallelTcpConnector.hpp"
#include "wpi/util/Logger.hpp"
#include "wpi/nt/NetworkTableInstance.hpp"
#include "wpi/nt/StringTopic.hpp"
#include "wpi/util/StringExtras.hpp"
#include "systemd-utils.h"
struct DataStorage {
wpi::util::Logger logger;
wpi::nt::StringSubscriber teamSubscriber;
std::shared_ptr<wpi::net::ParallelTcpConnector> tcpConnector;
std::weak_ptr<wpi::net::uv::Tcp> tcpConn;
std::optional<uint16_t> oldTeamNumber;
};
static bool startUvLoop(wpi::net::uv::Loop& loop, DataStorage& instData);
int main() {
printf("Starting RadioDaemon\n");
printf("\tBuild Hash: %s\n", MRC_GetGitHash());
printf("\tBuild Timestamp: %s\n", MRC_GetBuildTimestamp());
#if defined(__linux__) && defined(MRC_DAEMON_BUILD)
sigset_t signal_set;
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGTERM);
sigaddset(&signal_set, SIGINT);
sigprocmask(SIG_BLOCK, &signal_set, nullptr);
#endif
auto ntInst = wpi::nt::NetworkTableInstance::Create();
ntInst.SetServer({"localhost"}, 6810);
ntInst.StartClient("RadioDaemon");
DataStorage instData;
instData.teamSubscriber = ntInst.GetStringTopic("/sys/team").Subscribe("");
wpi::net::EventLoopRunner loopRunner;
bool success = false;
loopRunner.ExecSync([&success, &instData](wpi::net::uv::Loop& loop) {
success = startUvLoop(loop, instData);
});
if (!success) {
loopRunner.Stop();
return -1;
}
systemd_utils::notify_ready();
{
#if defined(__linux__) && defined(MRC_DAEMON_BUILD)
int sig = 0;
sigwait(&signal_set, &sig);
#else
(void)getchar();
#endif
}
systemd_utils::notify_stopping();
loopRunner.Stop();
ntInst.StopClient();
wpi::nt::NetworkTableInstance::Destroy(ntInst);
return 0;
}
// static std::optional<uint16_t> checkTeamNumber(DataStorage& instData) {
// auto teamStr = instData.teamSubscriber.Get();
// auto maybeTeam = wpi::parse_integer<uint16_t>(teamStr, 10);
// if (!maybeTeam.has_value()) {
// printf("failed to parse team number\n");
// return {};
// }
// auto team = *maybeTeam;
// if (team > 25599) {
// printf("Team number too large\n");
// return {};
// }
// return team;
// }
// static void tryRequest(wpi::uv::Loop& loop, DataStorage& instData) {
// }
static bool startUvLoop(wpi::net::uv::Loop& loop, DataStorage& instData) {
auto timer = wpi::net::uv::Timer::Create(loop);
if (!timer) {
return false;
}
// instData.tcpConnector = wpi::net::ParallelTcpConnector::Create(
// loop, wpi::net::uv::Timer::Time{2000}, instData.logger,
// [](wpi::net::uv::Tcp& tcp) {
// },
// true);
// if (!instData.tcpConnector) {
// return false;
// }
// timer->timeout.connect([&loop, &instData] {
// auto maybeTeam = checkTeamNumber(instData);
// if (!maybeTeam.has_value()) {
// instData.oldTeamNumber = maybeTeam;
// return;
// }
// if (maybeTeam != instData.oldTeamNumber) {
// // New team number.
// instData.oldTeamNumber = maybeTeam;
// // Restart TCP if it exists.
// std::array<std::pair<std::string, unsigned int>, 1> servers;
// servers[0] = {"localhost", 80};
// instData.tcpConnector->SetServers(servers);
// instData.tcpConnector->Disconnected();
// return;
// }
// tryRequest(loop, instData);
// // fmt::format("10.{}.{}.1", static_cast<int>(team / 100),
// // static_cast<int>(team % 100));
// // auto tcp = wpi::uv::Tcp::Create(loop, 0);
// // if (!tcp) {
// // printf("Failed to allocate tcp\n");
// // return;
// // }
// // tcp->Reuse
// });
// timer->Start(wpi::uv::Timer::Time{100}, wpi::uv::Timer::Time{3000});
return true;
}