1
0
Fork 0
OpenLCP/protocol/esta/sacn/universearbitrator.cpp

442 lines
11 KiB
C++

/*
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 "universe.h"
#include "universearbitrator.h"
#include "universemerger.h"
#include <map>
#include "config.h"
namespace sACN {
/**
* @brief UniverseArbitrator::UniverseArbitrator
* @param universe
*/
UniverseArbitrator::UniverseArbitrator(sACN::Universe * universe)
: m_universe(universe)
, hold_last_look_(true)
, merge_mode_(MERGE_HTP)
, m_dominant(std::weak_ptr<Universe>())
{
}
/**
* @brief UniverseArbitrator::refresh
*/
void UniverseArbitrator::refresh()
{
purge_stale_sources_();
find_dominant_();
}
/**
* @brief UniverseArbitrator::age
* @return
*/
long UniverseArbitrator::age() const
{
auto universe = m_dominant.lock();
if (!universe)
return 0;
return universe->age();
}
/**
* @brief UniverseArbitrator::status
* @return
*/
uint8_t UniverseArbitrator::status() const
{
auto universe = m_dominant.lock();
if (!universe)
return Universe::DMX_NULL;
return universe->status();
}
/**
* @brief UniverseArbitrator::slot
* @param address
* @return
*/
uint8_t UniverseArbitrator::slot(const uint16_t address) const
{
auto universe = m_dominant.lock();
if (!universe)
return 0;
return universe->slot(address);
}
/**
* @brief UniverseArbitrator::rxRate
* @return
*/
double UniverseArbitrator::rxRate()
{
refresh();
auto universe = m_dominant.lock();
if (!universe)
return 0.0;
return universe->rxRate();
}
/**
* @brief UniverseArbitrator::metadata
* @return
*/
std::shared_ptr<DATA::data_header> UniverseArbitrator::metadata() const
{
auto universe = m_dominant.lock();
if (universe)
return universe->metadata();
m_universe->metadata_->priority = 255; // invalid
return m_universe->metadata_;
}
/**
* @brief UniverseArbitrator::isEditable
* @return
*/
bool UniverseArbitrator::isEditable() const
{
return false;
}
/**
* @brief UniverseArbitrator::activeSlots
* @return
*/
uint16_t UniverseArbitrator::activeSlots() const
{
auto universe = m_dominant.lock();
if (!universe)
return 0;
return universe->activeSlots();
}
/**
* @brief UniverseArbitrator::sources
* @return
*/
const std::vector<DATA::data_header> UniverseArbitrator::sources() const
{
std::shared_lock lk_ctl(mtx_arbitrate);
std::vector<DATA::data_header> keys;
for (const auto& [key, _] : sources_)
keys.push_back(key);
return keys;
}
/**
* @brief UniverseArbitrator::hasSourceUniverse
* @param src
* @return
*/
bool UniverseArbitrator::hasSourceUniverse(const DATA::data_header& src) const
{
std::shared_lock lk_ctl(mtx_arbitrate);
return (sources_.count(src));
}
/**
* @brief UniverseArbitrator::sourceUniverse
* @param src
* @return
*/
std::shared_ptr<Universe> UniverseArbitrator::sourceUniverse(const DATA::data_header &src)
{
std::shared_lock lk_ctl(mtx_arbitrate);
if (!hasSourceUniverse(src))
return nullptr;
return sources_.at(src);
}
/**
* @brief UniverseArbitrator::addNewSource
* @param src
* @return
*/
std::shared_ptr<Universe> UniverseArbitrator::addNewSource(const DATA::data_header &src)
{
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;
}
/**
* @brief UniverseArbitrator::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_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();
}
/**
* @brief UniverseArbitrator::onSourceListChange
* @param cb
* @return
*/
std::shared_ptr<void> UniverseArbitrator::onSourceListChange(const std::function<void(DMX::Universe*)> cb)
{
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;
}
/**
* @brief UniverseArbitrator::setHoldLastLook
* @param state
*/
void UniverseArbitrator::setHoldLastLook(const bool state)
{
std::unique_lock lk_ctl(mtx_arbitrate);
hold_last_look_ = state;
}
/**
* @brief UniverseArbitrator::getHoldLastLook
* @return
*/
bool UniverseArbitrator::getHoldLastLook() const
{
std::shared_lock lk_ctl(mtx_arbitrate);
return hold_last_look_;
}
/**
* @brief UniverseArbitrator::setMergeMode
* @param mode
*/
void UniverseArbitrator::setMergeMode(const MergeMode mode)
{
merge_mode_ = mode;
}
/**
* @brief UniverseArbitrator::getMergeMode
* @return
*/
UniverseArbitrator::MergeMode UniverseArbitrator::getMergeMode() const
{
return merge_mode_;
}
/**
* @brief UniverseArbitrator::doSourceListChange
*/
void UniverseArbitrator::doListChangeCallbacks()
{
m_universe->do_callbacks_(cb_sourceListChange);
}
/**
* @brief UniverseArbitrator::find_dominant_
*/
void UniverseArbitrator::find_dominant_()
{
if (sources_.empty())
{
m_dominant = std::weak_ptr<Universe>();
return;
}
// cache the age of each universe
std::unordered_map<DATA::data_header,uint> ages;
{
std::shared_lock lk_ctl(mtx_arbitrate);
for (const auto& [header, universe] : sources_)
ages.insert({header, universe->age()});
}
// order universe into a two dimentional container; priority then age
std::map<uint,std::multimap<uint,std::shared_ptr<Universe>>> by_priority;
{
std::shared_lock lk_ctl(mtx_arbitrate);
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});
}
}
if (getMergeMode() == MERGE_LTP ||
by_priority.crbegin()->second.size() == 1)
{ // freshest universe at the hightest priority
auto universe = by_priority.crbegin()->second.cbegin()->second;
if (universe != m_dominant.lock())
{
{
std::unique_lock lk_ctl(mtx_arbitrate);
m_dominant = universe;
}
cb_tokens_.clear();
cb_tokens_.push_back(universe->onDataChange([this](DMX::Universe*){m_universe->doDataCallbacks();}));
cb_tokens_.push_back(universe->onStatusChange([this](DMX::Universe*){m_universe->doStatusCallbacks();}));
doListChangeCallbacks();
m_universe->doDataCallbacks();
m_universe->doStatusCallbacks();
}
return;
}
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();
}
merged->Universe::metadata()->universe = m_universe->metadata_->universe;
merged->Universe::metadata()->priority = by_priority.crbegin()->first;
for (const auto & [_, universe] : by_priority.crbegin()->second)
merged->addSource(universe);
m_dominant = merged;
cb_tokens_.clear();
cb_tokens_.push_back(merged->onDataChange([this](DMX::Universe*){m_universe->doDataCallbacks();}));
cb_tokens_.push_back(merged->onStatusChange([this](DMX::Universe*){m_universe->doStatusCallbacks();}));
m_universe->doDataCallbacks();
m_universe->doStatusCallbacks();
doListChangeCallbacks();
}
/**
* @brief ArbitratingUniverse::purge_stale_sources_
*/
void UniverseArbitrator::purge_stale_sources_()
{
std::unordered_map<DATA::data_header,uint> ages;
{
std::shared_lock lk_ctl(mtx_arbitrate);
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<uint,DATA::data_header> by_age;
{
std::shared_lock lk_ctl(mtx_arbitrate);
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