/* mergeproxyuniverse.cpp Copyright (c) 2020 Kevin Matz (kevin.matz@gmail.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "universearbitrator.h" #include "universemerger.h" #include #include "config.h" namespace sACN { /** * @brief ArbitratingUniverse::MergeProxyUniverse */ UniverseArbitrator::UniverseArbitrator() : sACN::Universe() , expectedUniverse(0) , hold_last_look_(true) { } /** * @brief ArbitratingUniverse::setHoldLastLook * @param state */ void UniverseArbitrator::setHoldLastLook(const bool state) { std::unique_lock lk_ctl(mtx_control); hold_last_look_ = state; } /** * @brief ArbitratingUniverse::getHoldLastLook * @return */ bool UniverseArbitrator::getHoldLastLook() const { std::shared_lock lk_ctl(mtx_control); return hold_last_look_; } const std::vector UniverseArbitrator::sources() const { std::shared_lock lk_ctl(mtx_control); std::vector keys; for (const auto& [key, _] : sources_) keys.push_back(key); return keys; } /** * @brief ArbitratingUniverse::sourceUniverse * @param src * @return */ std::shared_ptr UniverseArbitrator::sourceUniverse(const DATA::data_header &src) { std::shared_lock lk_ctl(mtx_control); if (!hasSourceUniverse(src)) return nullptr; return sources_.at(src); } /** * @brief ArbitratingUniverse::hasSourceUniverse * @param src * @return */ bool UniverseArbitrator::hasSourceUniverse(const DATA::data_header& src) const { std::shared_lock lk_ctl(mtx_control); return (sources_.count(src)); } std::shared_ptr UniverseArbitrator::addNewSource(const DATA::data_header &src) { { std::unique_lock lk_ctl(mtx_control); auto [itr, ok] = sources_.try_emplace(src, std::make_shared()); if (!ok) return nullptr; auto univ = itr->second; source_data_tokens.push_back(univ->onDataChange( std::bind(&sACN::UniverseArbitrator::dataChangedNotifier, this, std::placeholders::_1))); source_status_tokens.push_back(univ->onStatusChange( std::bind(&sACN::UniverseArbitrator::doStatusCallbacks, this))); } doListChangeCallbacks(); doStatusCallbacks(); std::shared_lock lk_ctl(mtx_control); return sources_.at(src); } /** * @brief ArbitratingUniverse::deleteSourceUniverse * @param src */ void UniverseArbitrator::deleteSourceUniverse(const DATA::data_header& src) { if (!hasSourceUniverse(src)) return; if (sources_.size() > 1 || !hold_last_look_) { std::unique_lock lk_ctl(mtx_control); sources_.erase(src); } else { std::shared_lock lk_ctl(mtx_control); /// Set the universe's status to TERMINATED. sources_.at(src)->setStatus(Universe::sACN_TERMINATED); /// Resetting the sequencing on terminated universes results in faster reaquisition /// from the same source without waiting for the sequence to realign. sources_.at(src)->metadata()->sequence_number = 0; } doListChangeCallbacks(); doStatusCallbacks(); // if the deleted universe was dominant, the status has changed } /** * @brief ArbitratingUniverse::dataChangedNotifier * @param dmx */ void UniverseArbitrator::dataChangedNotifier(DMX::Universe* dmx) { #ifdef RTTI_ENABLED auto sacn = dynamic_cast(dmx); #else auto sacn = static_cast(dmx); #endif auto universe = dominant_(); if (!universe) return; if (sacn->metadata() == universe->metadata()) doDataCallbacks(); } /** * @brief ArbitratingUniverse::onSourceListChange * @param cb * @return */ std::shared_ptr UniverseArbitrator::onSourceListChange(std::function cb) { std::unique_lock lk_ctl(mtx_control); // wrap the callback with a shared pointer auto sp = std::make_shared>(std::move(cb)); // add callback to list (as a weak pointer) cb_sourceListChange.push_back(sp); // return token that caller must keep throughout it's scope return sp; } /** * @brief ArbitratingUniverse::doListChangeCallbacks */ void UniverseArbitrator::doListChangeCallbacks() { for (auto it = cb_sourceListChange.cbegin(); it != cb_sourceListChange.cend();) { if (auto sp = it->lock()) { // if the caller is still holding the token (*sp)(); ++it; } else { // or remove the callback std::unique_lock lk_ctl(mtx_control); it = cb_sourceListChange.erase(it); } } } /** * @brief ArbitratingUniverse::isSyncronized * @return */ bool UniverseArbitrator::isSyncronized() const { auto universe = dominant_(); if (!universe) return false; return universe->isSyncronized(); } /** * @brief ArbitratingUniverse::metadata * @return */ std::shared_ptr UniverseArbitrator::metadata() const { auto universe = dominant_(); if (universe) return universe->metadata(); std::shared_lock lk_ctl(mtx_control); auto metadata = std::make_shared(); metadata->universe = expectedUniverse; metadata->priority = 255; // invalid return metadata; } uint8_t UniverseArbitrator::status() const { auto universe = dominant_(); if (!universe) return Universe::DMX_NULL; return universe->status(); } /** * @brief ArbitratingUniverse::synchronize * @param sequence_number */ void UniverseArbitrator::synchronize(uint8_t sequence_number) { std::shared_lock lk_ctl(mtx_control); for ( auto& [_, uni] : sources_) uni->synchronize(sequence_number); } bool UniverseArbitrator::isEditable() const { return false; } uint16_t UniverseArbitrator::activeSlots() const { auto universe = dominant_(); if (!universe) return 0; return universe->activeSlots(); } /** * @brief ArbitratingUniverse::slot * @param address * @return */ uint8_t UniverseArbitrator::slot(const uint16_t address) const { auto universe = dominant_(); if (!universe) return 0; return universe->slot(address); } /** * @brief ArbitratingUniverse::rxRate * @return */ double UniverseArbitrator::rxRate() { purge_stale_sources_(); auto universe = dominant_(); if (!universe) return 0.0; return universe->rxRate(); } /** * @brief ArbitratingUniverse::rxDmpSetProperty * @param message */ void UniverseArbitrator::rxDmpSetProperty(ACN::PDU::Message message) { (void)message; } /** * @brief MergeProxyUniverse::dominant_ * @return */ std::shared_ptr UniverseArbitrator::dominant_() const { if (sources_.empty()) return nullptr; // cache the age of each universe std::unordered_map ages; { std::shared_lock lk_ctl(mtx_control); for (const auto& [header, universe] : sources_) ages.insert({header, universe->age()}); } // order universe into a two dimentional container; priority then age std::map>> by_priority; { std::shared_lock lk_ctl(mtx_control); for (const auto& [header, universe] : sources_) { auto age = ages.at(header); auto priority = header.priority; if (!by_priority.count(priority)) by_priority.emplace(priority, std::multimap>({{age,universe}})); else by_priority.at(priority).insert({age, universe}); } } if (getMergeMode() == MERGE_LTP || by_priority.crbegin()->second.size() == 1) // freshest universe at the hightest priority return by_priority.crbegin()->second.cbegin()->second; auto merged = std::make_shared(); merged->Universe::metadata()->universe = expectedUniverse; merged->Universe::metadata()->priority = by_priority.crbegin()->first; for (const auto & [_, universe] : by_priority.crbegin()->second) merged->addSource(universe); return merged; } /** * @brief ArbitratingUniverse::purge_stale_sources_ */ void UniverseArbitrator::purge_stale_sources_() { std::unordered_map ages; { std::shared_lock lk_ctl(mtx_control); for (const auto& [header, universe] : sources_) { universe->rxRate(); // DMX::Universe::rx_timeout for maintenance tasks ages.insert({header, universe->age()}); } } // order the member universes by age std::multimap by_age; { std::shared_lock lk_ctl(mtx_control); for (const auto& [header, _] : sources_) by_age.insert({ages.at(header), header}); } // clean up stale universes, oldest first for(auto it = by_age.crbegin(); it != by_age.crend(); it++) { auto age = it->first; auto key = it->second; // the lastest source universe is never purged if holding the last look if (sources_.size() == 1 && hold_last_look_) break; // it's a sorted container. If this universe is live, so are the remainder. if (age < E131_NETWORK_DATA_LOSS_TIMEOUT) break; // erase the zombie universe deleteSourceUniverse(key); } } } // namespace SACN