407 lines
9.5 KiB
C++
407 lines
9.5 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::~MergeProxyUniverse
|
|
*/
|
|
ArbitratingUniverse::~ArbitratingUniverse()
|
|
{
|
|
}
|
|
|
|
|
|
/**
|
|
* @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);
|
|
sources_.emplace(src, std::make_shared<Universe>());
|
|
auto data_token = sources_.at(src)->onDataChange(
|
|
std::bind(&sACN::ArbitratingUniverse::dataChangedNotifier, this, std::placeholders::_1));
|
|
source_data_tokens.push_back(data_token);
|
|
auto status_token = sources_.at(src)->onStatusChange(
|
|
std::bind(&sACN::ArbitratingUniverse::doStatusCallbacks, this));
|
|
source_status_tokens.push_back(status_token);
|
|
}
|
|
|
|
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_wctl(lk_ctl);
|
|
sources_.erase(src);
|
|
}
|
|
doListChangeCallbacks();
|
|
}
|
|
|
|
|
|
/**
|
|
* @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.begin(); it != cb_sourceListChange.end();)
|
|
{
|
|
if (auto sp = it->lock())
|
|
{ // if the caller is still holding the token
|
|
(*sp)();
|
|
++it;
|
|
}
|
|
else
|
|
{ // or remove the callback
|
|
std::unique_lock lk_wctl(lk_ctl);
|
|
it = cb_sourceListChange.erase(it);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief ArbitratingUniverse::isSyncronized
|
|
* @return
|
|
*/
|
|
bool ArbitratingUniverse::isSyncronized()
|
|
{
|
|
auto universe = dominant_();
|
|
if (!universe)
|
|
return false;
|
|
return universe->isSyncronized();
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief ArbitratingUniverse::metadata
|
|
* @return
|
|
*/
|
|
std::shared_ptr<DATA::data_header> ArbitratingUniverse::metadata()
|
|
{
|
|
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()
|
|
{
|
|
uint8_t status;
|
|
auto universe = dominant_();
|
|
if (universe)
|
|
{
|
|
status = universe->status();
|
|
setStatus(status);
|
|
}
|
|
else
|
|
{
|
|
status = Universe::status();
|
|
}
|
|
return 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()
|
|
{
|
|
auto universe = dominant_();
|
|
if (!universe)
|
|
return 0;
|
|
return universe->activeSlots();
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief ArbitratingUniverse::slot
|
|
* @param s
|
|
* @return
|
|
*/
|
|
uint8_t ArbitratingUniverse::slot(const uint16_t s)
|
|
{
|
|
auto universe = dominant_();
|
|
if (!universe)
|
|
return 0;
|
|
return universe->slot(s);
|
|
}
|
|
|
|
|
|
/**
|
|
* @brief ArbitratingUniverse::rxRate
|
|
* @return
|
|
*/
|
|
double ArbitratingUniverse::rxRate()
|
|
{
|
|
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_()
|
|
{
|
|
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()});
|
|
}
|
|
purge_stale_sources_(&ages);
|
|
|
|
// 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_
|
|
* @param age_cache
|
|
*/
|
|
void ArbitratingUniverse::purge_stale_sources_(std::unordered_map<DATA::data_header,uint> *age_cache)
|
|
{
|
|
std::unordered_map<DATA::data_header,uint> ages;
|
|
if (age_cache)
|
|
ages = *age_cache;
|
|
else
|
|
{
|
|
std::shared_lock lk_ctl(mtx_control);
|
|
for (const auto& [header, universe] : sources_)
|
|
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
|
|
{
|
|
std::unique_lock lk_ctl(mtx_control);
|
|
sources_.erase(key);
|
|
}
|
|
}
|
|
|
|
// if zombies were erased, notify about the changes
|
|
if (sources_.size() != by_age.size())
|
|
doListChangeCallbacks();
|
|
}
|
|
|
|
} // namespace SACN
|