forked from PokemonAutomation/Arduino-Source
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgramSession.cpp
More file actions
404 lines (331 loc) · 13.4 KB
/
Copy pathProgramSession.cpp
File metadata and controls
404 lines (331 loc) · 13.4 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* Program Session
*
* From: https://github.com/PokemonAutomation/
*
*/
#include <future>
#include "Common/Cpp/Exceptions.h"
#include "Common/Cpp/PanicDump.h"
#include "CommonFramework/GlobalSettingsPanel.h"
#include "CommonFramework/Logging/Logger.h"
#include "CommonFramework/Tools/GlobalThreadPools.h"
#include "CommonFramework/Panels/ProgramDescriptor.h"
#include "CommonFramework/ProgramSession.h"
#include "CommonFramework/ProgramStats/StatsDatabase.h"
#include "CommonFramework/Exceptions/OperationFailedException.h"
#include "CommonFramework/ResourceDownload/ProgramMissingResourceTracker.h"
#include "CommonFramework/ResourceDownload/GlobalResourceDownloadManager.h"
#include "CommonFramework/ResourceDownload/ResourceDownloadHelpers.h"
#include "Integrations/ProgramTracker.h"
namespace PokemonAutomation{
void ProgramSession::add_listener(Listener& listener){
m_listeners.add(listener);
}
void ProgramSession::remove_listener(Listener& listener){
m_listeners.remove(listener);
}
ProgramSession::ProgramSession(const ProgramDescriptor& descriptor)
: m_descriptor(descriptor)
, m_instance_id(ProgramTracker::instance().add_program(*this))
// , m_logger(global_logger_raw(), "Program:" + std::to_string(m_instance_id))
, m_logger(global_logger_raw(), "Program")
, m_timestamp(current_time())
, m_state(ProgramState::STOPPED)
{
load_historical_stats();
}
ProgramSession::~ProgramSession(){
ProgramTracker::instance().remove_program(m_instance_id);
// ProgramSession::request_program_stop();
// join_program_thread();
}
void ProgramSession::join_program_thread() noexcept{
m_program_thread.wait_and_ignore_exceptions();
}
const std::string& ProgramSession::identifier() const{
return m_descriptor.identifier();
}
std::string ProgramSession::current_stats() const{
std::lock_guard<Mutex> lg(m_lock);
if (m_current_stats){
return m_current_stats->to_str(StatsTracker::DISPLAY_ON_SCREEN);
}
return "";
}
std::string ProgramSession::historical_stats() const{
std::lock_guard<Mutex> lg(m_lock);
if (m_historical_stats){
return m_historical_stats->to_str(StatsTracker::DISPLAY_ON_SCREEN);
}
return "";
}
WallClock ProgramSession::timestamp() const{
return m_timestamp.load(std::memory_order_relaxed);
}
void ProgramSession::report_stats_changed(){
std::lock_guard<Mutex> lg(m_lock);
push_stats();
}
void ProgramSession::report_error(const std::string& message){
std::lock_guard<Mutex> lg(m_lock);
push_error(message);
}
void ProgramSession::report_download_error(const std::string& message){
std::lock_guard<Mutex> lg(m_lock);
push_download_error(message);
}
void ProgramSession::report_download_added(std::shared_ptr<ResourceDownload> download_ptr){
// std::lock_guard<Mutex> lg(m_lock);
m_listeners.run_method(&Listener::download_added, std::move(download_ptr));
}
void ProgramSession::report_all_downloads_done(){
m_listeners.run_method(&Listener::all_downloads_done);
}
void ProgramSession::set_state(ProgramState state){
switch (state){
case ProgramState::NOT_READY:
m_logger.log("Program State: NOT_READY");
break;
case ProgramState::STOPPED:
m_logger.log("Program State: STOPPED");
break;
case ProgramState::RUNNING:
m_logger.log("Program State: RUNNING");
break;
case ProgramState::STOPPING:
m_logger.log("Program State: STOPPING");
break;
}
m_state.store(state, std::memory_order_relaxed);
m_listeners.run_method(&Listener::state_change, state);
}
void ProgramSession::push_stats(){
m_listeners.run_method(
&Listener::stats_update,
m_current_stats.get(),
m_historical_stats.get()
);
}
void ProgramSession::push_error(const std::string& message){
m_listeners.run_method(&Listener::error, message);
}
void ProgramSession::push_download_error(const std::string& message){
m_listeners.run_method(&Listener::download_error, message);
}
void ProgramSession::load_historical_stats(){
// Load historical stats.
std::unique_ptr<StatsTracker> stats = m_descriptor.make_stats();
if (stats){
m_logger.log("Loading historical stats...");
// m_current_stats = m_descriptor.make_stats();
StatSet set;
set.open_from_file(GlobalSettings::instance().STATS_FILE);
const std::string& identifier = m_descriptor.identifier();
StatList& list = set[identifier];
if (list.size() != 0){
list.aggregate(*stats);
}
m_historical_stats = std::move(stats);
}
}
void ProgramSession::update_historical_stats_with_current(){
if (m_current_stats){
m_logger.log("Saving historical stats...");
bool ok = StatSet::update_file(
GlobalSettings::instance().STATS_FILE,
m_descriptor.identifier(),
*m_current_stats
);
if (ok){
m_logger.log("Stats successfully saved!", COLOR_BLUE);
}else{
m_logger.log("Unable to save stats.", COLOR_RED);
push_error("Unable to save stats.");
}
}
}
std::string ProgramSession::start_program(){
m_logger.log("Received Program Start Request");
std::lock_guard<Mutex> lg(m_lock);
ProgramState state = this->current_state();
switch (state){
case ProgramState::NOT_READY:
m_logger.log("Program is not ready.", COLOR_RED);
return "Program is not ready.";
case ProgramState::STOPPED:{
std::string error = check_validity();
if (!error.empty()){
m_logger.log(error, COLOR_RED);
return error;
}
// Wait for previous program thread to finish.
join_program_thread();
// Now start the program.
m_logger.log("Starting program...");
m_timestamp.store(current_time(), std::memory_order_relaxed);
set_state(ProgramState::RUNNING);
m_program_thread = GlobalThreadPools::unlimited_realtime().dispatch_now_blocking(
[this]{
run_with_catch(
"ProgramSession::start_program()",
[this]{ run_program(); }
);
}
);
#if 0
m_thread = Thread([this]{
run_with_catch(
"ProgramSession::start_program()",
[this]{ run_program(); }
);
});
#endif
return "";
}
case ProgramState::RUNNING:
m_logger.log("Program is already running.", COLOR_RED);
return "Program is already running.";
case ProgramState::STOPPING:
m_logger.log("Program is currently stopping.", COLOR_RED);
return "Program is currently stopping.";
}
throw InternalProgramError(
&m_logger,
PA_CURRENT_FUNCTION,
"Invalid State: " + std::to_string((int)state)
);
}
std::string ProgramSession::stop_program(){
m_logger.log("Received Stop Request");
{
std::lock_guard<Mutex> lg(m_lock);
ProgramState state = this->current_state();
switch (state){
case ProgramState::NOT_READY:
m_logger.log("Program is not ready.", COLOR_RED);
return "Program is not ready.";
case ProgramState::STOPPED:
m_logger.log("Program is already stopped.", COLOR_RED);
return "Program is already stopped.";
case ProgramState::RUNNING:
m_logger.log("Stopping program...");
set_state(ProgramState::STOPPING);
break;
case ProgramState::STOPPING:
m_logger.log("Program is already stopping.", COLOR_RED);
return "Program is already stopping.";
default:
throw InternalProgramError(
&m_logger,
PA_CURRENT_FUNCTION,
"Invalid State: " + std::to_string((int)state)
);
}
}
internal_stop_program();
return "";
}
void ProgramSession::run_program(){
{
std::lock_guard<Mutex> lg(m_lock);
m_current_stats = m_descriptor.make_stats();
load_historical_stats();
push_stats();
}
internal_run_program();
{
std::lock_guard<Mutex> lg(m_lock);
push_stats();
update_historical_stats_with_current();
set_state(ProgramState::STOPPED);
}
}
RequiredResourceResult ProgramSession::find_missing_resources(){
bool upgrade_warning = false;
std::vector<std::string> missing_resources;
for(const std::string& resource_type : m_descriptor.required_resources()){
ResourceVersionStatus version_status = get_local_version_info(resource_type).version_status;
switch(version_status){
case ResourceVersionStatus::CURRENT:
// we have this resource, check the next one
continue;
case ResourceVersionStatus::FUTURE_VERSION:
// we have this resource, check the next one
// however, the resource that was downloaded is more updated than what the program is expecting
// warn the user to upgrade CC.
upgrade_warning = true;
continue;
case ResourceVersionStatus::OUTDATED:
case ResourceVersionStatus::NOT_APPLICABLE:{
// we don't have the resource. check remote to see if correct version is available.
DownloadedResourceMetadata remote_resource = get_remote_resource_metadata_from_resource_slug(resource_type);
DownloadedResourceMetadata expected_resource = get_expected_resource_metadata_from_resource_slug(resource_type);
uint16_t expected_version_num = expected_resource.version_num.value();
uint16_t remote_version_num = remote_resource.version_num.value();
if (expected_version_num < remote_version_num){
// remote version is more updated than we expect
// warn the user to upgrade CC.
upgrade_warning = true;
}else if (expected_version_num > remote_version_num){
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "resources_to_download: expected_version_num > remote_version_num. This shouldn't happen.");
}
missing_resources.emplace_back(remote_resource.resource_name);
break;
}
default:
throw InternalProgramError(nullptr, PA_CURRENT_FUNCTION, "resources_to_download: Unknown enum.");
}
}
return RequiredResourceResult{std::move(missing_resources), upgrade_warning};
}
bool ProgramSession::download_prereqs(CancellableScope& scope){
try{
auto [missing_resources, requires_upgrade] = find_missing_resources();
if (requires_upgrade){
std::string warning_string =
"The program is expecting an older version of a resource than is available. "
"This likely means that your version of Computer Control is out of date. "
"We recommend that you upgrade the Computer Control program.";
report_error(warning_string);
// cout << warning_string << endl;
}
if (missing_resources.empty()){
// cout << "required_download_list is empty. Start the program." << endl;
return true;
}
// 1. Create a C++ standard promise to hold the final boolean result
std::promise<bool> download_promise;
std::future<bool> download_future = download_promise.get_future();
// ProgramMissingResourceTracker is responsible for calling report_error() if
// the download fails, or if exceptions are thrown within the download.
ProgramMissingResourceTracker missing_resource_tracker(scope, download_promise, *this);
GlobalResourceDownloadManager& global_download_manager = GlobalResourceDownloadManager::instance();
for (const std::string& resource_slug : missing_resources){
// cout << download_ptr->get_name() << endl;
auto download_ptr = global_download_manager.add_to_download_list(resource_slug);
if (!download_ptr) {
std::cerr << "Error: Null download pointer for " << resource_slug << std::endl;
continue;
}
missing_resource_tracker.add_resource(download_ptr);
report_download_added(download_ptr);
}
missing_resource_tracker.finalize_initial_batch();
// Block until the ProgramMissingResourceTracker calls set_value(),
// when the downloads all succeed, or one fails, or the user clicks "Stop Program"
bool success = download_future.get();
report_all_downloads_done();
return success;
}catch(OperationFailedException& e){
report_error(e.message());
}catch(InternalProgramError& e){
report_error(e.message());
}catch (const std::exception& e) {
std::string message = std::string(e.what()) + "Report this as an error.";
report_error(message);
}catch(...){
report_error("show_download_prereqs_popup: Unknown exception caught. Report this as an error.");
}
return false;
}
}