-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
136 lines (92 loc) · 3.59 KB
/
main.cpp
File metadata and controls
136 lines (92 loc) · 3.59 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
#include <fstream>
#include <iostream>
#include <filesystem>
#include <vector>
#include <string>
#include <thread>
#include <list>
#include <nlohmann/json.hpp>
#include "Functions_networking.h"
#include "Functions_win32.h"
#include "Global_state.h"
#include "Request.h"
#include "Handlers_for_main.h"
static bool data_thread_running = true;
static std::thread client_thread;
static void handle_fatal_error(G_state::Error* err) {
Client::fatal_error = *err;
client_thread.join();
data_thread_running = false;
}
int main() {
G_state::Error init_err(G_state::Error_type::ok);
init_err = G_state::set_up_on_startup();
if (init_err.type == G_state::Error_type::ok) {
init_err = G_state::update_state();
}
if (init_err.type != G_state::Error_type::ok) { std::cout << "Fatal error on startup. \n" << std::endl; }
client_thread = std::thread(Client::client_thread); // This starts right away.
std::cout << "Started the client thread. \n" << std::endl;
if (init_err.type != G_state::Error_type::ok) {
handle_fatal_error(&init_err);
std::cout << "Failed on startup. \n" << std::endl;
}
else { std::cout << "Done setting up. \n" << std::endl; }
// ==================================================================
int n = 1;
while (data_thread_running) {
std::chrono::seconds sleep_length(G_state::Settings::n_sec_between_state_updates);
std::this_thread::sleep_for(sleep_length);
std::cout << " ------------ Data thread loop : " << n++ << " ------------ " << std::endl;
if (!Client::client_running) { // NOTE(damian): something weird happend to the client that made it stop itself.
std::cout << "Client broke down, GG. \n" << std::endl;
break;
}
G_state::Error err = G_state::update_state();
if (err.type != G_state::Error_type::ok) {
handle_fatal_error(&err);
std::cout << "Error when updating state. GG. \n" << std::endl;
break;
}
// Getting the first command has not yet been handled
auto p_to_request = Client::request_queue.begin();
bool unhandled_found = false;
for (; p_to_request != Client::request_queue.end(); ++p_to_request) {
if (!p_to_request->handled) {
unhandled_found = true;
break;
}
}
// TODO(damian): why the fuck does main add new processes, G_state should be doing it when it updates its state.
if (unhandled_found) {
Track_request* track = std::get_if<Track_request> (&p_to_request->request.variant);
Untrack_request* untrack = std::get_if<Untrack_request>(&p_to_request->request.variant);
Change_update_time_request* change_time = std::get_if<Change_update_time_request>(&p_to_request->request.variant);
G_state::Error maybe_err(G_state::Error_type::ok);
if (track != nullptr) {
maybe_err = G_state::add_process_to_track(&track->path);
}
else if (untrack != nullptr) {
maybe_err = G_state::remove_process_from_track(&untrack->path);
}
else if (change_time != nullptr) {
maybe_err = G_state::update_settings_file(change_time->duration_in_sec);
}
else {
Error err(Error_type::runtime_logics_failed, "A request was given to the data thread to be handled, but there is no handler for that type of request. ");
handle_fatal_error(&err);
std::cout << "A propogated request to the data thread by client is not handled inside main. GG. \n" << std::endl;
break;
}
if (maybe_err.type != G_state::Error_type::ok) {
handle_fatal_error(&maybe_err);
std::cout << "Error caught. \n" << std::endl;
break;
}
p_to_request->handled = true;
}
}
std::cout << "\n";
std::cout << "Finished running. \n" << std::endl;
return 0;
}