-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsyncManager.cpp
More file actions
182 lines (139 loc) · 5.81 KB
/
Copy pathsyncManager.cpp
File metadata and controls
182 lines (139 loc) · 5.81 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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "pch.h"
#include <format>
#include <utility>
#include "syncManager.h"
#include "logger.h"
#include "utils.h"
#include "constants.h"
void vsid::sync::SyncManager::add(const std::string& callsign, const std::string& newScratch, const std::string& oldScratch)
{
auto& qcs = this->queue[callsign];
std::string trimmedNew = vsid::utils::trim(newScratch);
std::string trimmedOld = vsid::utils::trim(oldScratch);
if (!qcs.empty() && vsid::utils::svEqualCi(qcs.back().newScratch, newScratch)) return;
vsid::Logger::log(LogLevel::Debug, std::format("[{}] adding New: [{}] Old: [{}] to sync queue.",
callsign, trimmedNew, trimmedOld), DebugLevel::Sync);
qcs.push_back({ std::move(trimmedNew), std::move(trimmedOld)});
if (!this->states.contains(callsign))
this->states[callsign] = SyncData();
}
void vsid::sync::SyncManager::processQueue(EuroScopePlugIn::CPlugIn* plugin)
{
if (this->queue.empty()) return;
auto now = std::chrono::steady_clock::now();
vsid::Logger::log(LogLevel::Debug, std::format("Started sync processing queue... Size [{}]", this->queue.size()), DebugLevel::Dev);
for (auto it = this->queue.begin(); it != this->queue.end();)
{
const std::string& callsign = it->first;
auto& csQueue = it->second;
auto& data = this->states[callsign];
if (csQueue.empty() && data.state == SyncState::Free)
{
vsid::Logger::log(LogLevel::Debug, std::format("[{}] Removing from sync queue. "
"No more msgs and state is ::Free", callsign), DebugLevel::Sync);
this->states.erase(callsign);
it = this->queue.erase(it);
continue;
}
EuroScopePlugIn::CFlightPlan FlightPlan = plugin->FlightPlanSelect(callsign.c_str());
if (!FlightPlan.IsValid())
{
vsid::Logger::log(LogLevel::Debug, std::format("[{}] Flightplan not valid. Removing from sync queue.", callsign), DebugLevel::Sync);
this->states.erase(callsign);
it = this->queue.erase(it);
continue;
}
if (data.state == SyncState::Free && !csQueue.empty())
{
std::string& newScratch = csQueue.front().newScratch;
data.state = SyncState::WaitOnSync;
data.lastTriggerTime = now;
vsid::Logger::log(LogLevel::Debug, std::format("[{}] syncing [{}]", callsign, newScratch), DebugLevel::Sync, true);
FlightPlan.GetControllerAssignedData().SetScratchPadString(newScratch.c_str()); // set scratch pad after state update
}
else if (data.state != SyncState::Free)
{
auto elapsed = std::chrono::duration<double>(now - data.lastTriggerTime).count();
if (elapsed >= SYNC_TIMEOUT_SECONDS)
{
data.state = SyncState::Free;
SyncMsg front;
if (!csQueue.empty())
{
front = std::move(csQueue.front());
csQueue.pop_front();
}
FlightPlan.GetControllerAssignedData().SetScratchPadString(front.oldScratch.c_str()); // set scratch pad after state update
vsid::Logger::log(LogLevel::Warning, std::format("[{}] exceeded watch dog time [{} seconds]. "
"Removing [{}]. Restoring [{}] to scratch pad",
callsign, elapsed, front.newScratch, front.oldScratch), DebugLevel::Sync);
}
}
++it;
}
vsid::Logger::log(LogLevel::Debug, "Finished sync processing queue...", DebugLevel::Dev);
}
void vsid::sync::SyncManager::update(EuroScopePlugIn::CFlightPlan& FlightPlan, const std::string& scratchOverwrite)
{
std::string callsign = FlightPlan.GetCallsign();
auto qIt = this->queue.find(callsign);
if (!this->states.contains(callsign) || qIt == this->queue.end() || qIt->second.empty())
return;
vsid::Logger::log(LogLevel::Debug, std::format("[{}] Updating sync queue.", callsign), DebugLevel::Sync);
auto& data = this->states[callsign];
auto& msg = qIt->second.front();
EuroScopePlugIn::CFlightPlanControllerAssignedData fplnData = FlightPlan.GetControllerAssignedData();
std::string currScratch = vsid::utils::trim(fplnData.GetScratchPadString());
bool synced = false;
if (data.state == SyncState::WaitOnSync)
{
vsid::Logger::log(LogLevel::Debug, std::format("[{}] WaitOnSync. Current [{}] | Expected [{}]{}",
callsign, currScratch, msg.newScratch,
(scratchOverwrite.empty()) ? "" : std::format(" | Overwrite [{}]", scratchOverwrite)),
DebugLevel::Sync);
if (scratchOverwrite == "GND" && this->gndStates.contains(msg.newScratch))
{
synced = true;
vsid::Logger::log(LogLevel::Debug, std::format("[{}] overwritten 'GND'. sync set true", callsign), DebugLevel::Sync);
}
else if (vsid::utils::svEqualCi(currScratch, msg.newScratch))
{
synced = true;
vsid::Logger::log(LogLevel::Debug, std::format("[{}] Current [{}] equals [{}]. sync set true",
callsign, currScratch, msg.newScratch), DebugLevel::Sync);
}
else if (vsid::utils::svEqualCi(scratchOverwrite, msg.newScratch))
{
synced = true;
vsid::Logger::log(LogLevel::Debug, std::format("[{}] overwritten [{}] equals [{}]. sync set true",
callsign, scratchOverwrite, msg.newScratch), DebugLevel::Sync);
}
if (synced)
{
if (vsid::utils::svEqualCi(currScratch, msg.oldScratch))
{
vsid::Logger::log(LogLevel::Debug, std::format("[{}] Current [{}] already matches old [{}]. Skipping ::WaitOnRestore.",
callsign, currScratch, msg.oldScratch), DebugLevel::Sync);
qIt->second.pop_front();
data.state = SyncState::Free;
}
else
{
vsid::Logger::log(LogLevel::Debug, std::format("[{}] Setting ::WaitOnRestore", callsign), DebugLevel::Sync);
data.state = SyncState::WaitOnRestore;
data.lastTriggerTime = std::chrono::steady_clock::now();
fplnData.SetScratchPadString(msg.oldScratch.c_str()); // set scratch pad after state update
}
}
}
else if (data.state == SyncState::WaitOnRestore)
{
vsid::Logger::log(LogLevel::Debug, std::format("[{}] WaitOnRestore. Current [{}] | Expected [{}]",
callsign, currScratch, msg.oldScratch), DebugLevel::Sync);
if (vsid::utils::svEqualCi(currScratch, msg.oldScratch))
{
qIt->second.pop_front();
data.state = SyncState::Free;
}
}
}