2021-08-24 18:10:20 -04:00
|
|
|
/*
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2022-12-12 13:39:10 -05:00
|
|
|
#include "universe.h"
|
2022-12-11 16:04:44 -05:00
|
|
|
#include "universearbitrator.h"
|
2022-12-11 15:58:28 -05:00
|
|
|
#include "universemerger.h"
|
2022-11-20 15:22:51 -05:00
|
|
|
#include <map>
|
2022-12-09 16:04:31 -05:00
|
|
|
#include "config.h"
|
2021-08-24 18:10:20 -04:00
|
|
|
|
2021-08-28 09:01:33 -04:00
|
|
|
namespace sACN {
|
2021-08-24 18:10:20 -04:00
|
|
|
|
|
|
|
/**
|
2022-12-11 21:30:26 -05:00
|
|
|
* @brief UniverseArbitrator::UniverseArbitrator
|
2022-12-12 13:39:10 -05:00
|
|
|
* @param universe
|
2021-08-24 18:10:20 -04:00
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
UniverseArbitrator::UniverseArbitrator(sACN::Universe * universe)
|
|
|
|
: m_universe(universe)
|
2022-12-11 21:30:26 -05:00
|
|
|
, hold_last_look_(true)
|
2022-12-12 13:43:40 -05:00
|
|
|
, merge_mode_(MERGE_HTP)
|
2022-12-11 21:30:37 -05:00
|
|
|
, m_dominant(std::weak_ptr<Universe>())
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-12 11:32:26 -05:00
|
|
|
/**
|
|
|
|
* @brief UniverseArbitrator::refresh
|
|
|
|
*/
|
|
|
|
void UniverseArbitrator::refresh()
|
|
|
|
{
|
|
|
|
purge_stale_sources_();
|
|
|
|
find_dominant_();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-10 16:26:31 -04:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::age
|
|
|
|
* @return
|
2021-09-10 16:26:31 -04:00
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
long UniverseArbitrator::age() const
|
2021-09-10 16:26:31 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
auto universe = m_dominant.lock();
|
|
|
|
if (!universe)
|
|
|
|
return 0;
|
|
|
|
return universe->age();
|
2021-09-10 16:26:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::status
|
2021-09-10 16:26:31 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
uint8_t UniverseArbitrator::status() const
|
2021-09-10 16:26:31 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
auto universe = m_dominant.lock();
|
|
|
|
if (!universe)
|
|
|
|
return Universe::DMX_NULL;
|
|
|
|
return universe->status();
|
2021-09-10 16:26:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-11 21:30:26 -05:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::slot
|
|
|
|
* @param address
|
2022-12-11 21:30:26 -05:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
uint8_t UniverseArbitrator::slot(const uint16_t address) const
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
auto universe = m_dominant.lock();
|
|
|
|
if (!universe)
|
|
|
|
return 0;
|
|
|
|
return universe->slot(address);
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::rxRate
|
2021-08-24 18:10:20 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
double UniverseArbitrator::rxRate()
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
refresh();
|
|
|
|
auto universe = m_dominant.lock();
|
|
|
|
if (!universe)
|
|
|
|
return 0.0;
|
|
|
|
return universe->rxRate();
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::metadata
|
2021-08-24 18:10:20 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_ptr<DATA::data_header> UniverseArbitrator::metadata() const
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
auto universe = m_dominant.lock();
|
|
|
|
if (universe)
|
|
|
|
return universe->metadata();
|
|
|
|
|
|
|
|
m_universe->metadata_->priority = 255; // invalid
|
|
|
|
return m_universe->metadata_;
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-11 21:30:26 -05:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::isEditable
|
2022-12-11 21:30:26 -05:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
bool UniverseArbitrator::isEditable() const
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
return false;
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::activeSlots
|
|
|
|
* @return
|
2021-08-24 18:10:20 -04:00
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
uint16_t UniverseArbitrator::activeSlots() const
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
auto universe = m_dominant.lock();
|
|
|
|
if (!universe)
|
|
|
|
return 0;
|
|
|
|
return universe->activeSlots();
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-09-10 12:00:50 -04:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::sources
|
2021-09-11 09:44:16 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
const std::vector<DATA::data_header> UniverseArbitrator::sources() const
|
2021-09-11 09:44:16 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_arbitrate);
|
|
|
|
std::vector<DATA::data_header> keys;
|
|
|
|
for (const auto& [key, _] : sources_)
|
|
|
|
keys.push_back(key);
|
|
|
|
return keys;
|
2021-09-11 09:44:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-24 18:10:20 -04:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::hasSourceUniverse
|
|
|
|
* @param src
|
2021-08-24 18:10:20 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
bool UniverseArbitrator::hasSourceUniverse(const DATA::data_header& src) const
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_arbitrate);
|
|
|
|
return (sources_.count(src));
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::sourceUniverse
|
|
|
|
* @param src
|
2021-08-24 18:10:20 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_ptr<Universe> UniverseArbitrator::sourceUniverse(const DATA::data_header &src)
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_arbitrate);
|
|
|
|
if (!hasSourceUniverse(src))
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return sources_.at(src);
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-11 21:30:26 -05:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::addNewSource
|
|
|
|
* @param src
|
2022-12-11 21:30:26 -05:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_ptr<Universe> UniverseArbitrator::addNewSource(const DATA::data_header &src)
|
2022-11-22 23:27:25 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_ptr<Universe> univ;
|
|
|
|
{
|
|
|
|
std::unique_lock lk_ctl(mtx_arbitrate);
|
|
|
|
auto [itr, ok] = sources_.emplace(src, std::make_shared<Universe>());
|
|
|
|
univ = itr->second;
|
|
|
|
if (!ok)
|
|
|
|
return univ;
|
|
|
|
// source_tokens_.push_back(univ->onStatusChange([this](DMX::Universe*) {
|
|
|
|
// refresh();
|
|
|
|
// }));
|
|
|
|
}
|
|
|
|
return univ;
|
2022-11-22 23:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-24 18:10:20 -04:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::deleteSourceUniverse
|
|
|
|
* @param src
|
2021-08-24 18:10:20 -04:00
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
void UniverseArbitrator::deleteSourceUniverse(const DATA::data_header& src)
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
if (!hasSourceUniverse(src))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (sources_.size() > 1 || !hold_last_look_)
|
|
|
|
{
|
|
|
|
std::unique_lock lk_ctl(mtx_arbitrate);
|
|
|
|
sources_.erase(src);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::shared_lock lk_ctl(mtx_arbitrate);
|
|
|
|
/// 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;
|
|
|
|
}
|
|
|
|
find_dominant_();
|
|
|
|
doListChangeCallbacks();
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-11 21:30:26 -05:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::onSourceListChange
|
|
|
|
* @param cb
|
2022-12-11 21:30:26 -05:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_ptr<void> UniverseArbitrator::onSourceListChange(const std::function<void(DMX::Universe*)> cb)
|
2022-12-09 11:18:27 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::unique_lock lk_ctl(mtx_arbitrate);
|
|
|
|
// wrap the callback with a shared pointer
|
|
|
|
auto sp = std::make_shared<std::function<void(DMX::Universe*)>>(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;
|
2021-09-03 17:37:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-11 21:30:26 -05:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::setHoldLastLook
|
|
|
|
* @param state
|
2022-12-11 21:30:26 -05:00
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
void UniverseArbitrator::setHoldLastLook(const bool state)
|
2021-09-03 17:43:50 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::unique_lock lk_ctl(mtx_arbitrate);
|
|
|
|
hold_last_look_ = state;
|
2021-09-03 17:43:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-24 18:10:20 -04:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::getHoldLastLook
|
2021-08-24 18:10:20 -04:00
|
|
|
* @return
|
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
bool UniverseArbitrator::getHoldLastLook() const
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_arbitrate);
|
|
|
|
return hold_last_look_;
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::setMergeMode
|
|
|
|
* @param mode
|
2021-08-24 18:10:20 -04:00
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
void UniverseArbitrator::setMergeMode(const MergeMode mode)
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
merge_mode_ = mode;
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-08 18:05:33 -05:00
|
|
|
/**
|
2022-12-12 13:39:10 -05:00
|
|
|
* @brief UniverseArbitrator::getMergeMode
|
|
|
|
* @return
|
2022-12-08 18:05:33 -05:00
|
|
|
*/
|
2022-12-12 13:39:10 -05:00
|
|
|
UniverseArbitrator::MergeMode UniverseArbitrator::getMergeMode() const
|
2022-12-08 18:05:33 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
return merge_mode_;
|
2022-12-08 18:05:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-12 13:39:10 -05:00
|
|
|
/**
|
|
|
|
* @brief UniverseArbitrator::doSourceListChange
|
|
|
|
*/
|
|
|
|
void UniverseArbitrator::doListChangeCallbacks()
|
|
|
|
{
|
|
|
|
m_universe->do_callbacks_(cb_sourceListChange);
|
|
|
|
}
|
|
|
|
|
2022-12-08 18:05:33 -05:00
|
|
|
|
2021-08-24 18:10:20 -04:00
|
|
|
/**
|
2022-12-11 21:30:37 -05:00
|
|
|
* @brief UniverseArbitrator::find_dominant_
|
2021-08-24 18:10:20 -04:00
|
|
|
*/
|
2022-12-11 21:30:37 -05:00
|
|
|
void UniverseArbitrator::find_dominant_()
|
2021-08-24 18:10:20 -04:00
|
|
|
{
|
2022-11-20 15:22:51 -05:00
|
|
|
if (sources_.empty())
|
2022-12-11 21:30:37 -05:00
|
|
|
{
|
|
|
|
m_dominant = std::weak_ptr<Universe>();
|
|
|
|
return;
|
|
|
|
}
|
2021-09-10 12:48:33 -04:00
|
|
|
|
2022-11-20 15:22:51 -05:00
|
|
|
// cache the age of each universe
|
|
|
|
std::unordered_map<DATA::data_header,uint> ages;
|
2022-12-09 11:18:27 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_arbitrate);
|
2022-12-09 11:18:27 -05:00
|
|
|
for (const auto& [header, universe] : sources_)
|
|
|
|
ages.insert({header, universe->age()});
|
|
|
|
}
|
2021-09-10 12:48:33 -04:00
|
|
|
|
2022-11-20 15:22:51 -05:00
|
|
|
// order universe into a two dimentional container; priority then age
|
|
|
|
std::map<uint,std::multimap<uint,std::shared_ptr<Universe>>> by_priority;
|
2022-12-09 11:18:27 -05:00
|
|
|
|
2022-11-20 15:22:51 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_arbitrate);
|
2022-12-09 11:18:27 -05:00
|
|
|
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<uint,std::shared_ptr<Universe>>({{age,universe}}));
|
|
|
|
else
|
|
|
|
by_priority.at(priority).insert({age, universe});
|
|
|
|
}
|
2022-11-20 15:22:51 -05:00
|
|
|
}
|
2021-09-10 12:48:33 -04:00
|
|
|
|
2022-12-11 15:58:28 -05:00
|
|
|
if (getMergeMode() == MERGE_LTP ||
|
|
|
|
by_priority.crbegin()->second.size() == 1)
|
2022-12-11 21:30:37 -05:00
|
|
|
{ // freshest universe at the hightest priority
|
|
|
|
auto universe = by_priority.crbegin()->second.cbegin()->second;
|
|
|
|
if (universe != m_dominant.lock())
|
|
|
|
{
|
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::unique_lock lk_ctl(mtx_arbitrate);
|
2022-12-11 21:30:37 -05:00
|
|
|
m_dominant = universe;
|
|
|
|
}
|
|
|
|
cb_tokens_.clear();
|
2022-12-12 13:39:10 -05:00
|
|
|
cb_tokens_.push_back(universe->onDataChange([this](DMX::Universe*){m_universe->doDataCallbacks();}));
|
|
|
|
cb_tokens_.push_back(universe->onStatusChange([this](DMX::Universe*){m_universe->doStatusCallbacks();}));
|
2022-12-11 21:30:37 -05:00
|
|
|
|
|
|
|
doListChangeCallbacks();
|
2022-12-12 13:39:10 -05:00
|
|
|
m_universe->doDataCallbacks();
|
|
|
|
m_universe->doStatusCallbacks();
|
2022-12-11 21:30:37 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2022-12-11 15:58:28 -05:00
|
|
|
|
2022-12-11 21:30:37 -05:00
|
|
|
auto dominant = m_dominant.lock();
|
|
|
|
if (dominant && dominant->hasSources())
|
|
|
|
{
|
|
|
|
std::vector<DATA::data_header> headers;
|
|
|
|
headers.reserve(by_priority.crbegin()->second.size());
|
|
|
|
for (const auto & [_, universe] : by_priority.crbegin()->second)
|
|
|
|
headers.push_back(*universe->metadata());
|
|
|
|
if (headers == dominant->sources())
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<UniverseMerger> merged;
|
|
|
|
if (!dominant)
|
|
|
|
merged = std::make_shared<UniverseMerger>();
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#ifdef RTTI_ENABLED
|
|
|
|
merged = std::dynamic_pointer_cast<UniverseMerger>(dominant);
|
|
|
|
#else
|
|
|
|
merged = std::static_pointer_cast<UniverseMerger>(dominant);
|
|
|
|
#endif
|
|
|
|
merged->clear();
|
|
|
|
}
|
2022-12-12 13:39:10 -05:00
|
|
|
merged->Universe::metadata()->universe = m_universe->metadata_->universe;
|
2022-12-11 16:13:10 -05:00
|
|
|
merged->Universe::metadata()->priority = by_priority.crbegin()->first;
|
2022-12-11 15:58:28 -05:00
|
|
|
for (const auto & [_, universe] : by_priority.crbegin()->second)
|
|
|
|
merged->addSource(universe);
|
2022-12-11 21:30:37 -05:00
|
|
|
|
|
|
|
m_dominant = merged;
|
|
|
|
cb_tokens_.clear();
|
2022-12-12 13:39:10 -05:00
|
|
|
cb_tokens_.push_back(merged->onDataChange([this](DMX::Universe*){m_universe->doDataCallbacks();}));
|
|
|
|
cb_tokens_.push_back(merged->onStatusChange([this](DMX::Universe*){m_universe->doStatusCallbacks();}));
|
2022-12-11 21:30:37 -05:00
|
|
|
|
2022-12-12 13:39:10 -05:00
|
|
|
m_universe->doDataCallbacks();
|
|
|
|
m_universe->doStatusCallbacks();
|
2022-12-11 21:30:37 -05:00
|
|
|
doListChangeCallbacks();
|
2022-11-20 15:22:51 -05:00
|
|
|
}
|
2021-09-10 12:48:33 -04:00
|
|
|
|
|
|
|
|
2022-11-20 15:22:51 -05:00
|
|
|
/**
|
|
|
|
* @brief ArbitratingUniverse::purge_stale_sources_
|
|
|
|
*/
|
2022-12-11 16:04:44 -05:00
|
|
|
void UniverseArbitrator::purge_stale_sources_()
|
2022-11-20 15:22:51 -05:00
|
|
|
{
|
|
|
|
std::unordered_map<DATA::data_header,uint> ages;
|
2022-12-10 11:15:02 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_arbitrate);
|
2022-12-10 11:15:02 -05:00
|
|
|
for (const auto& [header, universe] : sources_)
|
|
|
|
{
|
|
|
|
universe->rxRate(); // DMX::Universe::rx_timeout for maintenance tasks
|
|
|
|
ages.insert({header, universe->age()});
|
|
|
|
}
|
|
|
|
}
|
2022-11-20 15:22:51 -05:00
|
|
|
|
|
|
|
// order the member universes by age
|
|
|
|
std::multimap<uint,DATA::data_header> by_age;
|
2022-12-09 11:18:27 -05:00
|
|
|
{
|
2022-12-12 13:39:10 -05:00
|
|
|
std::shared_lock lk_ctl(mtx_arbitrate);
|
2022-12-09 11:18:27 -05:00
|
|
|
for (const auto& [header, _] : sources_)
|
2022-11-20 15:22:51 -05:00
|
|
|
by_age.insert({ages.at(header), header});
|
2022-12-09 11:18:27 -05:00
|
|
|
}
|
2022-11-20 15:22:51 -05:00
|
|
|
|
|
|
|
// 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
|
2022-12-09 18:17:04 -05:00
|
|
|
if (sources_.size() == 1 && hold_last_look_)
|
2022-11-20 15:22:51 -05:00
|
|
|
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
|
2022-12-10 11:15:02 -05:00
|
|
|
deleteSourceUniverse(key);
|
2022-11-20 15:22:51 -05:00
|
|
|
}
|
2021-08-24 18:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace SACN
|