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

377 lines
8.7 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>
namespace sACN {
/**
* @brief ArbitratingUniverse::MergeProxyUniverse
*/
ArbitratingUniverse::ArbitratingUniverse()
: sACN::Universe()
, expectedUniverse(0)
, HoldLastLook(true)
{
}
/**
* @brief ArbitratingUniverse::~MergeProxyUniverse
*/
ArbitratingUniverse::~ArbitratingUniverse()
{
}
/**
* @brief ArbitratingUniverse::setHoldLastLook
* @param state
*/
void ArbitratingUniverse::setHoldLastLook(const bool state)
{
HoldLastLook = state;
}
/**
* @brief ArbitratingUniverse::getHoldLastLook
* @return
*/
bool ArbitratingUniverse::getHoldLastLook() const
{
return HoldLastLook;
}
const std::vector<DATA::data_header> ArbitratingUniverse::sources() const
{
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)
{
if (!hasSourceUniverse(src))
return nullptr;
return sources_.at(src);
}
/**
* @brief ArbitratingUniverse::hasSourceUniverse
* @param src
* @return
*/
bool ArbitratingUniverse::hasSourceUniverse(const DATA::data_header& src) const
{
return (sources_.count(src));
}
std::shared_ptr<Universe> ArbitratingUniverse::addNewSource(const DATA::data_header &src)
{
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);
doListChangeCallbacks();
auto status_token = sources_.at(src)->onStatusChange(std::bind(&sACN::ArbitratingUniverse::doStatusCallbacks,
this));
source_status_tokens.push_back(status_token);
doStatusCallbacks();
return sources_.at(src);
}
/**
* @brief ArbitratingUniverse::deleteSourceUniverse
* @param src
*/
void ArbitratingUniverse::deleteSourceUniverse(const DATA::data_header& src)
{
if (!hasSourceUniverse(src))
return;
if (sources_.size() == 1 && HoldLastLook)
return;
sources_.erase(src);
doListChangeCallbacks();
}
/**
* @brief ArbitratingUniverse::dataChangedNotifier
* @param dmx
*/
void ArbitratingUniverse::dataChangedNotifier(DMX::Universe* dmx)
{
auto sacn = static_cast<sACN::Universe*>(dmx);
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)
{
// 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()
{
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
it = cb_sourceListChange.erase(it);
}
}
/**
* @brief ArbitratingUniverse::isSyncronized
* @return
*/
bool ArbitratingUniverse::isSyncronized()
{
auto universe = dominant_();
if (!universe)
return false;
return universe->isSyncronized();
}
/**
* @brief ArbitratingUniverse::provenance
* @return
*/
std::shared_ptr<DATA::data_header> ArbitratingUniverse::provenance()
{
auto universe = dominant_();
if (universe)
return universe->metadata();
auto prov = std::make_shared<DATA::data_header>();
prov->universe = expectedUniverse;
prov->priority = 255; // invalid
return prov;
}
/**
* @brief ArbitratingUniverse::set
* @param pdu
* @param src
*/
void ArbitratingUniverse::set(std::shared_ptr<ACN::DMP::Pdu> pdu,
std::shared_ptr<DATA::data_header> src)
{
if (expectedUniverse != 0 &&
src->universe != expectedUniverse)
return;
if (!sources_.count(*src))
addNewSource(*src);
sources_.at(*src)->set(pdu, src);
}
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)
{
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 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;
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;
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
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;
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 && HoldLastLook)
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
sources_.erase(key);
}
// if zombies were erased, notify about the changes
if (sources_.size() != by_age.size())
doListChangeCallbacks();
}
} // namespace SACN