Files
salmanoff/main.cpp
T
hayodea 870b8de249 Marionette: Introduce concept and add other 3 component threads
We introduce the 4 main component threads of execution for Harikoff:
* Marionette: This is the resident hijacking module that makes Harikoff
  instances non-persons, if configured to allow hijacking.
* Director: :)
* Canvas: :)
* Subconscious: DB, storage and recall.
2025-01-10 17:37:49 -04:00

118 lines
2.9 KiB
C++

#include <iostream>
#include <exception>
#include <thread>
#include <mutex>
#include <unordered_map>
#include <condition_variable>
#include <boost/asio.hpp>
#include <opts.h>
#include <mind.h>
#include <deviceManager/deviceManager.h>
#include <senseApis/senseApiManager.h>
#include "componentThread.h"
namespace hk {
static int initializeHarikoff(int argc, char **argv, char **envp);
void startThreads();
void signalThreads();
} // namespace hk
int main(int argc, char **argv, char **envp)
{
try {
std::cout << __func__ << ": Entering main()" << std::endl;
// Print out the keys for each index in the map
for (const auto& [id, componentThread]
: hk::ComponentThread::componentThreads)
{
std::cout << __func__ << ": Thread ID: " << id << std::endl;
}
int ret = hk::initializeHarikoff(argc, argv, envp);
if (ret != 0) {
return ret;
}
}
catch (const std::exception& e) {
std::cerr << __func__ << ": Exception occurred: " << e.what() << std::endl;
return EXIT_FAILURE;
}
catch (...) {
std::cerr << __func__ << ": Unknown exception occurred" << std::endl;
return EXIT_FAILURE;
}
std::cout << __func__ << ": Exiting normally" << std::endl;
return 0;
}
namespace hk {
static int initializeHarikoff(int argc, char **argv, char **envp)
{
std::cout << __func__ << ": Entering" << std::endl;
using namespace hk;
OptionParser &options = OptionParser::getOptions();
hk::Mind mind;
std::cout << PACKAGE_NAME << " " << PACKAGE_VERSION << std::endl;
try {
options.parseArguments(argc, argv);
}
catch (const std::invalid_argument& e) {
std::cerr << __func__ << ": Exception occurred: " << e.what() << '\n' << options.getUsage() << '\n';
return EXIT_FAILURE;
}
if (options.printUsage) {
std::cout << options.getUsage() << std::endl;
return EXIT_SUCCESS;
}
options.dumpOptions();
DeviceManager::getInstance().collateAllDeviceSpecs(options);
DeviceManager::getInstance().parseAllDeviceSpecs();
std::cout << DeviceManager::printDeviceSpecs() << std::endl;
sense_api::SenseApiManager::getInstance().loadAllSenseApiLibsFromOptions();
std::cout << __func__ << ": Exiting" << std::endl;
return 0;
}
void startThreads()
{
for (auto& [id, componentThread] : ComponentThread::componentThreads) {
componentThread.thread = std::thread([&componentThread]() {
// We sleep on spawn until the marionette tells us to continue.
{
std::unique_lock<std::mutex> lock(
componentThread.startupSync.mutex);
componentThread.startupSync.cv.wait(lock, [&componentThread]() {
return componentThread.startupSync.ready;
});
}
componentThread.getIoService().run();
});
}
}
void signalThreads()
{
for (auto& [id, componentThread] : ComponentThread::componentThreads) {
{
std::lock_guard<std::mutex> lock(componentThread.startupSync.mutex);
componentThread.startupSync.ready = true;
}
componentThread.startupSync.cv.notify_one();
}
}
} // namespace hk