#include #include #include #include #include #include #include #include #include namespace smo { namespace mrntt { class MarionetteComponent::MrnttLifetimeMgmtOp : public PostedAsynchronousContinuation { public: MrnttLifetimeMgmtOp( MarionetteComponent &parent, const std::shared_ptr &caller, Callback callback) : PostedAsynchronousContinuation( caller, callback), parent(parent) {} private: MarionetteComponent &parent; public: void initializeReq1_posted( [[maybe_unused]] std::shared_ptr context ) { auto self = ComponentThread::getSelf(); if (self->id != ComponentThread::MRNTT) { throw std::runtime_error(std::string(__func__) + ": Must be executed on Marionette thread"); } smo::mind::globalMind = std::make_shared(); smo::mind::globalMind->initializeReq({context, std::bind( &MrnttLifetimeMgmtOp::initializeReq2, this, context, std::placeholders::_1)}); } void initializeReq2( std::shared_ptr context, bool success ) { if (!success) { std::cerr << __func__ << ": Failed to initialize globalMind" << std::endl; context->callOriginalCb(false); return; } device::DeviceManager::getInstance().initializeDeviceReattacher(); context->callOriginalCb(success); } void finalizeReq1_posted( [[maybe_unused]] std::shared_ptr context ) { auto self = ComponentThread::getSelf(); if (self->id != ComponentThread::MRNTT) { throw std::runtime_error(std::string(__func__) + ": Must be executed on Marionette thread"); } device::DeviceManager::getInstance().finalizeDeviceReattacher(); smo::mind::globalMind->finalizeReq({context, std::bind( &MrnttLifetimeMgmtOp::finalizeReq2, this, context, std::placeholders::_1)}); } void finalizeReq2( std::shared_ptr context, bool success ) { if (!success) { std::cerr << __func__ << ": globalMind finalization failed" << std::endl; context->callOriginalCb(false); return; } context->callOriginalCb(success); } }; void MarionetteComponent::initializeReq( Callback callback) { auto mrntt = ComponentThread::getSelf(); if (mrntt->id != ComponentThread::MRNTT) { throw std::runtime_error(std::string(__func__) + ": Must be executed on Marionette thread"); } auto request = std::make_shared( *this, mrntt, callback); mrntt->getIoService().post( std::bind( &MrnttLifetimeMgmtOp::initializeReq1_posted, request.get(), request)); } void MarionetteComponent::finalizeReq( Callback callback) { auto mrntt = ComponentThread::getSelf(); if (mrntt->id != ComponentThread::MRNTT) { throw std::runtime_error(std::string(__func__) + ": Must be executed on Marionette thread"); } auto request = std::make_shared( *this, mrntt, callback); mrntt->getIoService().post( std::bind( &MrnttLifetimeMgmtOp::finalizeReq1_posted, request.get(), request)); } } // namespace mrntt } // namespace smo