OpenLCP/protocol/esta/sacn/arbitratinguniverse.cpp

391 lines
9.3 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 "arbitratinguniverse.h"
#include <map>
#include "config.h"
namespace sACN {
/**
* @brief ArbitratingUniverse::MergeProxyUniverse
*/
ArbitratingUniverse::ArbitratingUniverse()
: sACN::Universe()
, expectedUniverse(0)
, hold_last_look_(true)
{
}
/**
* @brief ArbitratingUniverse::setHoldLastLook
* @param state
*/
void ArbitratingUniverse::setHoldLastLook(const bool state)
{
std::unique_lock lk_ctl(mtx_control);
hold_last_look_ = state;
}
/**
* @brief ArbitratingUniverse::getHoldLastLook
* @return
*/
bool ArbitratingUniverse::getHoldLastLook() const
{
std::shared_lock lk_ctl(mtx_control);
return hold_last_look_;
}
const std::vector<DATA::data_header> ArbitratingUniverse::sources() const
{
std::shared_lock lk_ctl(mtx_control);
std::vector<DATA::data_header> keys;
for (const auto& [key, _] : sources_)
keys.push_back(key);
return keys;
}
/**
* @brief ArbitratingUniverse::sourceUniverse
* @param src
* @return
*/
std::shared_ptr<Universe> ArbitratingUniverse::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 ArbitratingUniverse::hasSourceUniverse(const DATA::data_header& src) const
{
std::shared_lock lk_ctl(mtx_control);
return (sources_.count(src));
}
std::shared_ptr<Universe> ArbitratingUniverse::addNewSource(const DATA::data_header &src)
{
{
std::unique_lock lk_ctl(mtx_control);
auto [itr, ok] = sources_.try_emplace(src, std::make_shared<Universe>());
if (!ok)
return nullptr;
auto univ = itr->second;
source_data_tokens.push_back(univ->onDataChange(
std::bind(&sACN::ArbitratingUniverse::dataChangedNotifier, this, std::placeholders::_1)));
source_status_tokens.push_back(univ->onStatusChange(
std::bind(&sACN::ArbitratingUniverse::doStatusCallbacks, this)));
}
doListChangeCallbacks();
doStatusCallbacks();
std::shared_lock lk_ctl(mtx_control);
return sources_.at(src);
}
/**
* @brief ArbitratingUniverse::deleteSourceUniverse
* @param src
*/
void ArbitratingUniverse::deleteSourceUniverse(const DATA::data_header& src)
{
{
std::shared_lock lk_ctl(mtx_control);
if (!hasSourceUniverse(src))
return;
if (sources_.size() == 1 && hold_last_look_)
return;
}
{
std::unique_lock lk_ctl(mtx_control);
sources_.erase(src);
}
doListChangeCallbacks();
doStatusCallbacks();
}
/**
* @brief ArbitratingUniverse::dataChangedNotifier
* @param dmx
*/
void ArbitratingUniverse::dataChangedNotifier(DMX::Universe* dmx)
{
#ifdef RTTI_ENABLED
auto sacn = dynamic_cast<sACN::Universe*>(dmx);
#else
auto sacn = static_cast<sACN::Universe*>(dmx);
#endif
auto universe = dominant_();
if (!universe)
return;
if (sacn->metadata() == universe->metadata())
doDataCallbacks();
}
/**
* @brief ArbitratingUniverse::onSourceListChange
* @param cb
* @return
*/
std::shared_ptr<void> ArbitratingUniverse::onSourceListChange(std::function<void()> cb)
{
std::unique_lock lk_ctl(mtx_control);
// wrap the callback with a shared pointer
auto sp = std::make_shared<std::function<void()>>(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 ArbitratingUniverse::doListChangeCallbacks()
{
std::shared_lock lk_ctl(mtx_control);
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
lk_ctl.unlock(); // release the shared lock
mtx_control.lock(); // take a unique lock
it = cb_sourceListChange.erase(it);
mtx_control.unlock(); // release the unique lock
lk_ctl.lock(); // retake the shared lock
}
}
}
/**
* @brief ArbitratingUniverse::isSyncronized
* @return
*/
bool ArbitratingUniverse::isSyncronized() const
{
auto universe = dominant_();
if (!universe)
return false;
return universe->isSyncronized();
}
/**
* @brief ArbitratingUniverse::metadata
* @return
*/
std::shared_ptr<DATA::data_header> ArbitratingUniverse::metadata() const
{
auto universe = dominant_();
if (universe)
return universe->metadata();
std::shared_lock lk_ctl(mtx_control);
auto metadata = std::make_shared<DATA::data_header>();
metadata->universe = expectedUniverse;
metadata->priority = 255; // invalid
return metadata;
}
uint8_t ArbitratingUniverse::status() const
{
auto universe = dominant_();
if (!universe)
return Universe::DMX_NULL;
return universe->status();
}
/**
* @brief ArbitratingUniverse::synchronize
* @param sequence_number
*/
void ArbitratingUniverse::synchronize(uint8_t sequence_number)
{
std::shared_lock lk_ctl(mtx_control);
for ( auto& [_, uni] : sources_)
uni->synchronize(sequence_number);
}
bool ArbitratingUniverse::isEditable() const
{
return false;
}
uint16_t ArbitratingUniverse::activeSlots() const
{
auto universe = dominant_();
if (!universe)
return 0;
return universe->activeSlots();
}
/**
* @brief ArbitratingUniverse::slot
* @param s
* @return
*/
uint8_t ArbitratingUniverse::slot(const uint16_t s) const
{
auto universe = dominant_();
if (!universe)
return 0;
return universe->slot(s);
}
/**
* @brief ArbitratingUniverse::rxRate
* @return
*/
double ArbitratingUniverse::rxRate()
{
purge_stale_sources_();
auto universe = dominant_();
if (!universe)
return 0.0;
return universe->rxRate();
}
/**
* @brief ArbitratingUniverse::rxDmpSetProperty
* @param message
*/
void ArbitratingUniverse::rxDmpSetProperty(ACN::PDU::Message<ACN::DMP::Pdu> message)
{
(void)message;
}
/**
* @brief MergeProxyUniverse::dominant_
* @return
*/
std::shared_ptr<Universe> ArbitratingUniverse::dominant_() const
{
if (sources_.empty())
return nullptr;
// cache the age of each universe
std::unordered_map<DATA::data_header,uint> 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<uint,std::multimap<uint,std::shared_ptr<Universe>>> 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<uint,std::shared_ptr<Universe>>({{age,universe}}));
else
by_priority.at(priority).insert({age, universe});
}
}
// freshest universe at the hightest priority
return by_priority.rbegin()->second.begin()->second;
}
/**
* @brief ArbitratingUniverse::purge_stale_sources_
*/
void ArbitratingUniverse::purge_stale_sources_()
{
std::unordered_map<DATA::data_header,uint> 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<uint,DATA::data_header> 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