Files
salmanoff/smocore/marionette/main.cpp
T

235 lines
5.9 KiB
C++
Raw Normal View History

#include <stdexcept>
#include <config.h>
#include <iostream>
#include <opts.h>
2025-09-03 14:43:00 -04:00
#include <typeinfo>
2026-02-22 17:46:27 -04:00
#include <spinscale/component.h>
2025-12-27 16:21:22 -04:00
#include <spinscale/componentThread.h>
2026-02-22 17:46:27 -04:00
#include <marionette/marionette.h>
2026-06-09 11:19:42 -04:00
#include <spinscale/co/nonViralCompletion.h>
2026-02-22 17:46:27 -04:00
#include <spinscale/runtime.h>
#include <componentThread.h>
2025-12-27 16:21:22 -04:00
#include <mindManager/mindManager.h>
#include <salmanoff.h>
2026-02-22 17:46:27 -04:00
namespace smo {
namespace mrntt {
std::shared_ptr<sscl::PuppeteerThread> thread = std::make_shared<
sscl::PuppeteerThread>(
SmoThreadId::MRNTT, smo::getThreadName(SmoThreadId::MRNTT),
2026-02-22 17:46:27 -04:00
sscl::pptr::PuppeteerComponent::defaultPuppeteerMain,
mrntt, &MarionetteComponent::preJoltHook);
2026-02-22 17:46:27 -04:00
MarionetteComponent mrntt(thread);
stim_buff::SmoThreadingModelDesc smoThreadingModelDesc = {
.componentThread = nullptr
};
void initializeSmoThreadingModelDesc(
const std::shared_ptr<sscl::ComponentThread>& componentThread)
{
if (!componentThread)
{
throw std::runtime_error(
std::string(__func__) + ": componentThread must be non-null");
}
if (smoThreadingModelDesc.componentThread
&& smoThreadingModelDesc.componentThread != componentThread)
{
throw std::runtime_error(
std::string(__func__)
+ ": SmoThreadingModelDesc already initialized with a different "
"ComponentThread");
}
smoThreadingModelDesc.componentThread = componentThread;
}
const stim_buff::SmoThreadingModelDesc& getSmoThreadingModelDesc()
{
return smoThreadingModelDesc;
}
2026-02-22 17:46:27 -04:00
void marionetteInitializeReqCb(bool success)
{
2026-02-22 17:46:27 -04:00
if (success)
{
std::cout << __func__ << ": Marionette initialized." << '\n';
return;
}
std::cerr << __func__ << ": Failed to initialize Marionette. Shutting down."
<< '\n';
mrntt.holdFinalizeCReq(
2026-06-09 11:19:42 -04:00
[](std::exception_ptr &exceptionPtr)
{
sscl::co::NonViralCompletion nvc(exceptionPtr);
nvc.checkAndRethrowException();
marionetteFinalizeReqCb(true);
});
}
2025-07-22 02:03:09 -04:00
void marionetteFinalizeReqCb(bool success)
{
2025-09-16 18:36:11 -04:00
if (!success)
{
std::cerr << __func__ << ": Failed to finalize Marionette." << '\n';
2025-09-16 18:36:11 -04:00
// Fallthrough.
}
std::cout << __func__ << ": Marionette finalized." << '\n';
2026-02-22 17:46:27 -04:00
thread->exitLoop();
}
2026-02-18 01:14:26 -04:00
2026-02-22 17:46:27 -04:00
void MarionetteComponent::preJoltHook(sscl::PuppeteerThread &self)
{
2026-02-22 17:46:27 -04:00
sscl::pptr::exitCode = EXIT_SUCCESS;
pthread_setname_np(pthread_self(), self.name.c_str());
std::cout << __func__ << ": Waiting for command line JOLT" << std::endl;
}
2026-02-22 17:46:27 -04:00
void MarionetteComponent::postJoltHook()
{
auto th = smo::mrntt::thread;
2026-02-22 17:46:27 -04:00
callShutdownSalmanoff = false;
2026-02-22 17:46:27 -04:00
// Register SIGINT (Ctrl+C) and SIGSEGV handlers
signals = std::make_unique<boost::asio::signal_set>(
th->getIoContext(), SIGINT);
2025-12-27 16:21:22 -04:00
2026-02-22 17:46:27 -04:00
signals->async_wait(
[](const boost::system::error_code& ec, int signal)
{
if (ec) { return; }
switch (signal) {
case SIGINT:
std::cerr << "SIGINT (Ctrl+C) received. Initiating "
"shutdown..." << '\n';
break;
case SIGSEGV:
std::cerr << "FATAL: Segmentation fault detected. "
"Initiating shutdown..." << '\n';
break;
default:
break;
}
mrntt.holdFinalizeCReq(
2026-06-09 11:19:42 -04:00
[](std::exception_ptr &exceptionPtr)
{
sscl::co::NonViralCompletion nvc(exceptionPtr);
nvc.checkAndRethrowException();
marionetteFinalizeReqCb(true);
});
2026-02-22 17:46:27 -04:00
});
}
2025-12-27 16:21:22 -04:00
2026-02-22 17:46:27 -04:00
void MarionetteComponent::tryBlock1Hook()
2025-07-22 02:03:09 -04:00
{
2026-02-22 17:46:27 -04:00
OptionParser &options = OptionParser::getOptions();
2026-02-22 17:46:27 -04:00
std::cout << "PuppeteerThread::main: " << PACKAGE_NAME << " "
<< PACKAGE_VERSION << std::endl;
2026-02-22 17:46:27 -04:00
options.parseArguments(
sscl::crtCommandLineArgs.argc, sscl::crtCommandLineArgs.argv,
sscl::crtCommandLineArgs.envp);
2026-02-22 17:46:27 -04:00
std::cout << "PuppeteerThread::main: " << options.stringifyOptions()
<< std::endl;
2026-02-22 17:46:27 -04:00
if (options.printUsage) {
throw JustPrintUsageNoError(options);
}
}
2026-02-22 17:46:27 -04:00
void MarionetteComponent::preLoopHook()
{
/** EXPLANATION:
2026-02-22 17:46:27 -04:00
* Initialize Salmanoff's Manager classes first.
* Manager classes' initialization is synchronous in nature, so we
* don't need the minds to be running to initialize them.
*
* Then we initialize the Minds. Minds are asynchronous and they
* call upon the async methods of the Manager classes. Device
* attachment is actually Mind specific and not Smo-global
*
* It is arguable whether library loading is Mind specific or
* Smo-global. You could argue that libraries should be loaded and
* unloaded dynamically as-needed by the bodies and worlds of
* particular Minds. You could also argue that we should load all
* libraries at startup and unload them at shutdown.
*
* The latter is cleaner and more resource-respecting. The former is
* easier to implement.
*/
smo::initializeSalmanoff();
callShutdownSalmanoff = true;
2026-06-09 11:19:42 -04:00
taskNursery.openAdmission();
holdInitializeCReq(
2026-06-09 11:19:42 -04:00
[](std::exception_ptr &exceptionPtr)
{
sscl::co::NonViralCompletion nvc(exceptionPtr);
nvc.checkAndRethrowException();
marionetteInitializeReqCb(true);
});
2026-02-22 17:46:27 -04:00
std::cout << "PuppeteerThread::main: Entering event loop" << "\n";
}
2026-02-22 17:46:27 -04:00
void MarionetteComponent::postLoopHook()
{
2026-06-09 11:19:42 -04:00
taskNursery.requestCancelOnAll();
taskNursery.closeAdmission();
2026-02-22 17:46:27 -04:00
std::cout << "PuppeteerThread::main: Exited event loop" << "\n";
}
2026-02-22 17:46:27 -04:00
void MarionetteComponent::postTryBlock1CatchHook()
{
if (callShutdownSalmanoff) {
smo::shutdownSalmanoff();
}
2026-02-22 17:46:27 -04:00
}
void MarionetteComponent::handleTryBlock1TypedException(const std::exception& e)
{
const OptionParser::Exception* optEx =
dynamic_cast<const OptionParser::Exception*>(&e);
if (optEx)
2025-08-13 09:43:34 -04:00
{
std::ostream *out = &std::cout;
2025-08-13 09:43:34 -04:00
std::string outUsageMsg;
2026-02-22 17:46:27 -04:00
if (typeid(*optEx) == typeid(OptionsParserError))
2025-08-13 09:43:34 -04:00
{
2026-02-18 01:14:26 -04:00
sscl::pptr::exitCode = EXIT_FAILURE;
out = &std::cerr;
2026-02-22 17:46:27 -04:00
outUsageMsg = std::string("main: ") + ": ";
2025-08-13 09:43:34 -04:00
}
2026-02-22 17:46:27 -04:00
// Else it's a JustPrintUsageNoError.
2025-08-13 09:43:34 -04:00
2026-02-22 17:46:27 -04:00
*out << outUsageMsg << optEx->what() << std::endl;
return;
}
2026-02-22 17:46:27 -04:00
std::cerr << "main: Exception occurred: " << e.what()
<< std::endl;
sscl::pptr::exitCode = EXIT_FAILURE;
2025-07-22 02:03:09 -04:00
}
2026-02-22 17:46:27 -04:00
void MarionetteComponent::handleTryBlock1UnknownException()
{
std::cerr << "main: Unknown exception occurred" << std::endl;
sscl::pptr::exitCode = EXIT_FAILURE;
}
} // namespace mrntt
} // namespace smo