-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNexusMainFrame.cpp
More file actions
158 lines (136 loc) · 5.42 KB
/
NexusMainFrame.cpp
File metadata and controls
158 lines (136 loc) · 5.42 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
#include <iostream>
#include <thread>
#include <exception>
#include "NexusMainFrame.hpp"
#include "pathing/PathManager.hpp"
#include "commands/CommandRegistry.hpp"
#include "commands/DisableModuleCommand.hpp"
#include "commands/DownloadCommand.hpp"
#include "commands/EnableModuleCommand.hpp"
#include "commands/HelpCommand.hpp"
#include "commands/ListCommand.hpp"
#include "commands/MonitorCommand.hpp"
#include "commands/ScanCommand.hpp"
#include "Modules/ModuleManager.hpp"
#include "mqtt/MQTTClient.hpp"
#include "Event/EventBus.hpp"
void NexusMainFrame::start() {
try {
auto& paths = PathManager::getInstance();
paths.ensureExists("modules.builtin");
paths.ensureExists("modules.downloaded");
paths.ensureExists("config");
paths.ensureExists("cache.downloads");
paths.ensureExists("logs");
paths.ensureExists("data.scans");
paths.registerPath("modules.registry", "modules/available-modules.json");
_moduleManager = std::make_unique<ModuleManager>();
// Register commands
CommandRegistry& registry = CommandRegistry::getInstance();
registry.registerCommand("help", std::make_unique<HelpCommand>());
registry.registerCommand("list", std::make_unique<ListCommand>(*_moduleManager));
registry.registerCommand("enable-module", std::make_unique<EnableModuleCommand>(*_moduleManager));
registry.registerCommand("disable-module", std::make_unique<DisableModuleCommand>(*_moduleManager));
registry.registerCommand("scan", std::make_unique<ScanCommand>());
registry.registerCommand("monitor", std::make_unique<MonitorCommand>());
registry.registerCommand("download", std::make_unique<DownloadCommand>());
// MQTT Client
_mqttClient = std::make_unique<MQTTClient>(EventBus::getInstance(), "nexus-core", "192.168.2.161", 1883);
// Wrap MQTT event subscription
EventBus::getInstance().subscribe("mqtt:event", [this](const Event &event) {
try {
std::any input = event.data;
std::string name;
std::string data;
if (input.type() == typeid(std::string)) {
std::string str = std::any_cast<std::string>(input);
auto pos = str.find(' ');
if (pos != std::string::npos) {
name = str.substr(0, pos);
data = str.substr(pos + 1);
} else {
name = str;
data = "";
}
}
Event newEvent;
newEvent.name = name;
newEvent.data = data;
EventBus::getInstance().publish(newEvent);
} catch(const std::exception& e) {
std::cerr << "[MQTT Event Callback Exception] " << e.what() << "\n";
} catch(...) {
std::cerr << "[MQTT Event Callback Exception] Unknown exception\n";
}
});
// Start server (ZONDER aparte thread!)
_server.start();
_running = true;
} catch(const std::exception& e) {
std::cerr << "[Start Exception] " << e.what() << "\n";
throw;
} catch(...) {
std::cerr << "[Start Exception] Unknown exception\n";
throw;
}
}
void NexusMainFrame::run() {
try {
start();
// Event loop
while (_running.load()) {
try {
EventBus::getInstance().dispatchPending();
} catch(const std::exception& e) {
std::cerr << "[Run EventBus Exception] " << e.what() << "\n";
} catch(...) {
std::cerr << "[Run EventBus Exception] Unknown exception\n";
}
try {
_scheduler.tick();
} catch(const std::exception& e) {
std::cerr << "[Run Scheduler Exception] " << e.what() << "\n";
} catch(...) {
std::cerr << "[Run Scheduler Exception] Unknown exception\n";
}
std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
} catch(const std::exception& e) {
std::cerr << "[Run Exception] " << e.what() << "\n";
} catch(...) {
std::cerr << "[Run Exception] Unknown exception\n";
}
stop();
}
void NexusMainFrame::stop() {
try {
_running = false;
EventBus::getInstance().shutdown();
if (_mqttClient) {
try {
_mqttClient->disconnect();
} catch(const std::exception& e) {
std::cerr << "[MQTT Disconnect Exception] " << e.what() << "\n";
} catch(...) {
std::cerr << "[MQTT Disconnect Exception] Unknown exception\n";
}
_mqttClient.reset();
}
if (_moduleManager) {
for (auto &m: _moduleManager->getModules()) {
try {
m.instance->shutdown();
} catch(const std::exception& e) {
std::cerr << "[Module Shutdown Exception] " << e.what() << "\n";
} catch(...) {
std::cerr << "[Module Shutdown Exception] Unknown exception\n";
}
}
}
_server.stop();
} catch(const std::exception& e) {
std::cerr << "[Stop Exception] " << e.what() << "\n";
} catch(...) {
std::cerr << "[Stop Exception] Unknown exception\n";
}
}