/** * @RDE_HEADER@ * AvmExerciseContext * $Id$ * $HeadURL$ * $Author$ * Created by Christian Fröbel, NLSE, 2018-07-30 */ #include "avm_exercise_context.h" #include #include #include #include #include #include #include #include #include #include #include namespace AudioVideoManagement { /** * Small composite that just holds all entities bound to the Exercise context. */ struct AvmExerciseContextManager::ExerciseContext { explicit ExerciseContext(LtsSession::LtsSessionDataDds *session) { static const int TIMEOUT_MS = 1000; Q_ASSERT(session); session->createTopicClient(dtc, ftc); opmDtc = OperatorManagement::Interface::Dtc::create(session->ddsConnectorGlobal()); exmDtc = ExerciseManagement::Interface::Dtc::create(session->ddsConnectorGlobal()); ismDtc = InfrastructureManagement::Interface::Dtc::create(session->ddsConnectorGlobal()); if (session->exerciseId() > 0) streamNameMapping.reset(StreamNameMapping::create(dtc.get())); opmDtc->enableUser(); opmDtc->waitForHistoricalDataUser(TIMEOUT_MS); // TODO: FIXME: solange man hier keinen ConnectingDialogNT benutzen kann, kein Assert exmDtc->enableExercise(); exmDtc->waitForHistoricalDataExercise(TIMEOUT_MS); ismDtc->enableMountingPointAsset(); ismDtc->waitForHistoricalDataMountingPointAsset(TIMEOUT_MS); dtc->enableCnrConfig(); dtc->enableTvcConfig(); dtc->enableCnrTvcProfile(); dtc->enableMenuStruct(); dtc->waitForHistoricalDataCnrConfig(TIMEOUT_MS); dtc->waitForHistoricalDataTvcConfig(TIMEOUT_MS); dtc->waitForHistoricalDataCnrTvcProfile(TIMEOUT_MS); dtc->waitForHistoricalDataMenuStruct(TIMEOUT_MS); } std::shared_ptr dtc; std::shared_ptr ftc; std::shared_ptr opmDtc; std::shared_ptr exmDtc; std::shared_ptr ismDtc; std::unique_ptr streamNameMapping; }; AvmExerciseContextManager::RefCountedContext::RefCountedContext(std::unique_ptr &&context) : context(std::move(context)) {} AvmExerciseContextManager::RefCountedContext::RefCountedContext(AvmExerciseContextManager::RefCountedContext &&) noexcept = default; AvmExerciseContextManager::RefCountedContext::~RefCountedContext() = default; AvmExerciseContextManager::~AvmExerciseContextManager() { // cfroebel 2018-07-31: Workaround for Bug REGG-4775 // All contexts should have been destroyed by now. // But due to the bug there is one last context for Ex 0. // Q_ASSERT(m_activeContexts.empty()); TODO: reactivate after fix if (m_activeContexts.empty()) return; Q_ASSERT(m_activeContexts.size() == 1); //Q_ASSERT(m_activeContexts.begin()->first == 0); TODO: reactivate after fix } AvmExerciseContextManager::ExerciseContext *AvmExerciseContextManager::acquire(int exerciseId, LtsSession::LtsSessionDataDds *session) { auto found = m_activeContexts.find(exerciseId); if (found != m_activeContexts.end()) { ++found->second.refCount; return found->second.context.get(); } std::unique_ptr composite(new ExerciseContext(session)); auto insertResult = m_activeContexts.insert(std::make_pair(exerciseId, RefCountedContext(std::move(composite)))); Q_ASSERT(insertResult.second); return insertResult.first->second.context.get(); } void AvmExerciseContextManager::release(int exerciseId, AvmExerciseContextManager::ExerciseContext *composite) { auto found = m_activeContexts.find(exerciseId); Q_ASSERT(found != m_activeContexts.end()); Q_ASSERT(found->second.context.get() == composite); --found->second.refCount; if (found->second.refCount == 0) m_activeContexts.erase(found); } AvmExerciseContextHandle::AvmExerciseContextHandle() = default; AvmExerciseContextHandle::AvmExerciseContextHandle(AvmExerciseContextManager *context, int exerciseId, LtsSession::LtsSessionDataDds *session) : m_context(context->acquire(exerciseId, session), [exerciseId, context](AvmExerciseContextManager::ExerciseContext *composite) { context->release(exerciseId, composite); }) { Q_ASSERT(context); Q_ASSERT(m_context); } StreamNameMapping *AvmExerciseContextHandle::streamNameMapping() const { Q_ASSERT(m_context); Q_ASSERT(m_context->streamNameMapping); return m_context->streamNameMapping.get(); } const std::shared_ptr &AvmExerciseContextHandle::dtc() const { Q_ASSERT(m_context); return m_context->dtc; } const std::shared_ptr &AvmExerciseContextHandle::ftc() const { Q_ASSERT(m_context); return m_context->ftc; } OperatorManagement::Interface::Dtc *AvmExerciseContextHandle::opmDtc() const { Q_ASSERT(m_context); return m_context->opmDtc.get(); } ExerciseManagement::Interface::Dtc *AvmExerciseContextHandle::exmDtc() const { Q_ASSERT(m_context); return m_context->exmDtc.get(); } InfrastructureManagement::Interface::Dtc *AvmExerciseContextHandle::ismDtc() const { Q_ASSERT(m_context); return m_context->ismDtc.get(); } } // namespace AudioVideoManagement