2025-07-28 07:20:44 -04:00
|
|
|
#include <config.h>
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <exception>
|
|
|
|
|
#include <opts.h>
|
2025-07-30 09:09:38 -04:00
|
|
|
#include <mind.h>
|
2025-07-28 07:20:44 -04:00
|
|
|
#include <componentThread.h>
|
|
|
|
|
#include <marionette/marionette.h>
|
2025-07-30 10:07:52 -04:00
|
|
|
#include <salmanoff.h>
|
2025-07-28 07:20:44 -04:00
|
|
|
|
|
|
|
|
namespace smo {
|
|
|
|
|
|
|
|
|
|
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 {
|
2025-07-28 07:20:44 -04:00
|
|
|
std::atomic<int> exitCode;
|
|
|
|
|
}
|
2025-07-22 02:03:09 -04:00
|
|
|
|
2025-07-28 07:20:44 -04:00
|
|
|
void ComponentThread::marionetteMain(ComponentThread& self)
|
2025-07-22 02:03:09 -04:00
|
|
|
{
|
2025-07-28 07:20:44 -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();
|
2025-08-03 09:57:29 -04:00
|
|
|
mrntt::exitCode = EXIT_SUCCESS;
|
2025-07-28 07:20:44 -04:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
OptionParser &options = OptionParser::getOptions();
|
|
|
|
|
|
|
|
|
|
std::cout << __func__ << ": " << PACKAGE_NAME << " " << PACKAGE_VERSION
|
|
|
|
|
<< std::endl;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
options.parseArguments(
|
|
|
|
|
crtCommandLineArgs.argc, crtCommandLineArgs.argv,
|
|
|
|
|
crtCommandLineArgs.envp);
|
|
|
|
|
|
|
|
|
|
std::cout << __func__ << ": " << options.stringifyOptions()
|
|
|
|
|
<< std::endl;
|
|
|
|
|
}
|
|
|
|
|
catch (const std::invalid_argument& e)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << __func__ << ": Exception occurred: " << e.what()
|
2025-08-03 10:19:22 -04:00
|
|
|
<< '\n';
|
2025-07-28 07:20:44 -04:00
|
|
|
|
2025-08-03 09:57:29 -04:00
|
|
|
options.printUsage = true;
|
|
|
|
|
mrntt::exitCode = EXIT_FAILURE;
|
2025-07-28 07:20:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (options.printUsage)
|
|
|
|
|
{
|
2025-08-03 10:22:28 -04:00
|
|
|
/* We could make RAII guard classes to always shutdown the mind
|
|
|
|
|
* threads properly in these pre-event loop situations.
|
|
|
|
|
*/
|
2025-08-10 13:12:17 -04:00
|
|
|
mind.finalizeReq([]{
|
|
|
|
|
mrntt::mrntt->getIoService().stop();
|
2025-08-03 09:57:29 -04:00
|
|
|
});
|
|
|
|
|
self.getIoService().reset();
|
|
|
|
|
self.getIoService().run();
|
2025-07-28 07:20:44 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-10 13:12:17 -04:00
|
|
|
self.getIoService().post([]()
|
2025-07-28 07:20:44 -04:00
|
|
|
{
|
2025-07-30 09:09:38 -04:00
|
|
|
try {
|
|
|
|
|
initializeSalmanoff();
|
|
|
|
|
mind.initialize();
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& e) {
|
|
|
|
|
mrntt::exitCode = EXIT_FAILURE;
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-07-28 07:20:44 -04:00
|
|
|
|
|
|
|
|
std::cout << __func__ << ": Entering event loop" << "\n";
|
2025-07-30 09:09:38 -04:00
|
|
|
|
|
|
|
|
/* 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;
|
2025-07-30 09:09:38 -04:00
|
|
|
try {
|
|
|
|
|
self.getIoService().reset();
|
|
|
|
|
self.getIoService().run();
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
2025-08-03 10:32:02 -04:00
|
|
|
sendExceptionInd = true;
|
2025-07-30 09:09:38 -04:00
|
|
|
std::cerr << self.name << ":" << __func__
|
|
|
|
|
<< ": Exception occurred: " << e.what() << "\n";
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
2025-08-03 10:32:02 -04:00
|
|
|
sendExceptionInd = true;
|
2025-07-30 09:09:38 -04:00
|
|
|
std::cerr << self.name << ":" << __func__
|
|
|
|
|
<< ": Unknown exception occurred" << "\n";
|
|
|
|
|
}
|
2025-08-03 10:32:02 -04:00
|
|
|
|
|
|
|
|
if (sendExceptionInd) { self.exceptionInd(self); }
|
2025-07-30 09:09:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout << __func__ << ": Exited event loop" << "\n";
|
2025-08-10 13:12:17 -04:00
|
|
|
mind.finalizeReq(nullptr);
|
2025-07-30 10:07:52 -04:00
|
|
|
shutdownSalmanoff();
|
2025-07-28 07:20:44 -04:00
|
|
|
}
|
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << __func__ << ": Exception occurred: " << e.what()
|
|
|
|
|
<< std::endl;
|
|
|
|
|
mrntt::exitCode = EXIT_FAILURE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
catch (...)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << __func__ << ": Unknown exception occurred" << std::endl;
|
|
|
|
|
mrntt::exitCode = EXIT_FAILURE;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::cout << __func__ << ": Exiting normally" << std::endl;
|
2025-07-22 02:03:09 -04:00
|
|
|
}
|
|
|
|
|
|
2025-07-28 07:20:44 -04:00
|
|
|
} // namespace smo
|