Files
salmanoff/smocore/marionette/marionette.cpp
T

186 lines
5.0 KiB
C++
Raw Normal View History

#include <config.h>
#include <cstdlib>
#include <iostream>
#include <exception>
#include <opts.h>
2025-09-03 14:43:00 -04:00
#include <typeinfo>
#include <boost/asio/signal_set.hpp>
#include <mind.h>
#include <componentThread.h>
#include <marionette/marionette.h>
#include <salmanoff.h>
namespace smo {
2025-09-03 14:43:00 -04:00
// Global Mind instance
std::shared_ptr<Mind> globalMind = std::make_shared<Mind>();
// Global marionette thread instance
std::shared_ptr<ComponentThread> mrntt::mrntt =
std::make_shared<ComponentThread>(ComponentThread::MRNTT, *globalMind);
CrtCommandLineArgs crtCommandLineArgs(0, nullptr, nullptr);
void CrtCommandLineArgs::set(int argc, char *argv[], char *envp[])
{
crtCommandLineArgs = CrtCommandLineArgs(argc, argv, envp);
}
2025-07-22 02:03:09 -04:00
namespace mrntt {
std::atomic<int> exitCode;
void exitMarionetteLoop()
{
mrntt::mrntt->keepLooping = false;
mrntt::mrntt->getIoService().stop();
std::cout << "Mrntt: Signaled main loop to exit." << "\n";
}
2025-07-22 02:03:09 -04:00
} // namespace mrntt
void ComponentThread::marionetteMain(ComponentThread& self)
2025-07-22 02:03:09 -04:00
{
// Wait for CRT's main() to post us the command line args.
std::cout << __func__ << ": Waiting for command line JOLT" << std::endl;
self.getIoService().run();
self.initializeTls();
mrntt::exitCode = EXIT_SUCCESS;
static boost::asio::signal_set signals(self.getIoService(), SIGINT);
bool callFinalizeReq = false;
try {
2025-09-07 11:45:54 -04:00
// Register SIGINT (Ctrl+C) and SIGSEGV handlers
signals.async_wait(
2025-09-07 11:45:54 -04:00
[&self](const boost::system::error_code& ec, int signal)
{
if (ec) { return; }
2025-09-07 11:45:54 -04:00
switch (signal) {
case SIGINT:
std::cerr << "SIGINT (Ctrl+C) received. Initiating shutdown..." << std::endl;
break;
case SIGSEGV:
std::cerr << "FATAL: Segmentation fault detected. Initiating shutdown..." << std::endl;
break;
default:
break;
}
self.userShutdownInd();
}
);
OptionParser &options = OptionParser::getOptions();
std::cout << __func__ << ": " << PACKAGE_NAME << " " << PACKAGE_VERSION
<< std::endl;
2025-08-13 09:43:34 -04:00
options.parseArguments(
crtCommandLineArgs.argc, crtCommandLineArgs.argv,
crtCommandLineArgs.envp);
2025-08-13 09:43:34 -04:00
std::cout << __func__ << ": " << options.stringifyOptions()
<< std::endl;
2025-08-13 09:43:34 -04:00
if (options.printUsage) {
throw JustPrintUsageNoError(options);
}
self.getIoService().post([]()
{
// Initialize Salmanoff first
initializeSalmanoff(mrntt::mrntt);
// Then initialize the global Mind object
2025-09-03 14:43:00 -04:00
globalMind->initialize();
});
std::cout << __func__ << ": Entering event loop" << "\n";
/* We loop here because when an exception occurs in mrntt, we need to
* both direct the mind threads to exit gracefully, and then we also
* need to post messages to our own event loop to initiate our own
* orderly exit. So we loop here to re-enter the event loop, both
* to receive the ACK messages from the mind threads, and to post
* messages to our own event loop to initiate our own orderly exit.
*/
for (self.keepLooping = true; self.keepLooping;)
{
2025-08-03 10:32:02 -04:00
bool sendExceptionInd = false;
try {
/** EXPLANATION:
* This reset() call is crucial for async bridging patterns
* to work.
* When the outermost thread's io_service is stop()ped (e.g.,
* from JOLT sequence), it won't process any new work until
* reset() is called, even if nested async operations try to
* post work to it. This means async bridges invoked from
* the outermost thread main sequence won't work until this
* reset() call.
*/
self.getIoService().reset();
self.getIoService().run();
}
catch (const std::exception& e)
{
2025-08-03 10:32:02 -04:00
sendExceptionInd = true;
std::cerr << self.name << ":" << __func__
<< ": Exception occurred: " << e.what() << "\n";
}
catch (...)
{
2025-08-03 10:32:02 -04:00
sendExceptionInd = true;
std::cerr << self.name << ":" << __func__
<< ": Unknown exception occurred" << "\n";
}
2025-08-03 10:32:02 -04:00
if (sendExceptionInd)
{
mrntt::exitCode = EXIT_FAILURE;
self.exceptionInd(self);
}
}
std::cout << __func__ << ": Exited event loop" << "\n";
}
2025-08-13 09:43:34 -04:00
catch (const OptionParser::Exception& e)
{
std::ostream *out = &std::cout;
2025-08-13 09:43:34 -04:00
std::string outUsageMsg;
if (typeid(e) == typeid(OptionsParserError))
{
mrntt::exitCode = EXIT_FAILURE;
out = &std::cerr;
2025-08-13 09:43:34 -04:00
outUsageMsg = std::string(__func__) + ": ";
}
*out << outUsageMsg << e.what() << std::endl;
callFinalizeReq = true;
2025-08-13 09:43:34 -04:00
}
catch (const std::exception& e)
{
std::cerr << __func__ << ": Exception occurred: " << e.what()
<< std::endl;
mrntt::exitCode = EXIT_FAILURE;
callFinalizeReq = true;
}
catch (...)
{
std::cerr << __func__ << ": Unknown exception occurred" << std::endl;
mrntt::exitCode = EXIT_FAILURE;
callFinalizeReq = true;
}
if (callFinalizeReq)
{
2025-09-03 14:43:00 -04:00
globalMind->finalizeReq([]{
mrntt::mrntt->getIoService().stop();
});
self.getIoService().reset();
self.getIoService().run();
}
shutdownSalmanoff();
2025-07-22 02:03:09 -04:00
}
} // namespace smo