You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
2.2 KiB
110 lines
2.2 KiB
#include "pathManager.h"
|
|
|
|
namespace mtx {
|
|
|
|
PathManager::PathManager(const Conf & global) : LogParent("[path manager] "), m_global(global) {}
|
|
|
|
PathManager::~PathManager()
|
|
{
|
|
closeAll();
|
|
}
|
|
|
|
void PathManager::applyPaths(const std::vector<PathConf> & paths)
|
|
{
|
|
std::vector<std::shared_ptr<Path>> tStop;
|
|
std::vector<std::shared_ptr<Path>> tStart;
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
for (auto it = m_paths.begin(); it != m_paths.end();)
|
|
{
|
|
const PathConf * tNew = nullptr;
|
|
for (const PathConf & p : paths)
|
|
{
|
|
if (p.name == it->first)
|
|
{
|
|
tNew = &p;
|
|
}
|
|
}
|
|
if (tNew == nullptr || *tNew != it->second->conf())
|
|
{
|
|
tStop.push_back(it->second);
|
|
it = m_paths.erase(it);
|
|
}
|
|
else
|
|
{
|
|
++it;
|
|
}
|
|
}
|
|
for (const PathConf & p : paths)
|
|
{
|
|
if (m_paths.find(p.name) == m_paths.end())
|
|
{
|
|
auto tPath = std::make_shared<Path>(p, m_global);
|
|
m_paths[p.name] = tPath;
|
|
tStart.push_back(tPath);
|
|
}
|
|
}
|
|
}
|
|
// outside the lock: stopping joins the source thread
|
|
for (auto & p : tStop)
|
|
{
|
|
log(LogLevel::Info, "removing path %s", p->conf().name.c_str());
|
|
p->stop();
|
|
}
|
|
for (auto & p : tStart)
|
|
{
|
|
log(LogLevel::Info, "adding path %s (source %s%s)", p->conf().name.c_str(), hideCredentials(p->conf().source).c_str(), p->conf().record ? ", recording" : "");
|
|
p->start();
|
|
}
|
|
}
|
|
|
|
void PathManager::closeAll()
|
|
{
|
|
std::map<std::string, std::shared_ptr<Path>> tAll;
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
tAll.swap(m_paths);
|
|
}
|
|
for (auto & p : tAll)
|
|
{
|
|
p.second->stop();
|
|
}
|
|
}
|
|
|
|
std::shared_ptr<Path> PathManager::find(const std::string & name) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
auto it = m_paths.find(name);
|
|
return it == m_paths.end() ? nullptr : it->second;
|
|
}
|
|
|
|
std::vector<PathInfo> PathManager::list() const
|
|
{
|
|
std::vector<std::shared_ptr<Path>> tAll;
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
for (auto & p : m_paths)
|
|
{
|
|
tAll.push_back(p.second);
|
|
}
|
|
}
|
|
std::vector<PathInfo> tOut;
|
|
for (auto & p : tAll)
|
|
{
|
|
tOut.push_back(p->info());
|
|
}
|
|
return tOut;
|
|
}
|
|
|
|
std::vector<PathConf> PathManager::confs() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(m_mutex);
|
|
std::vector<PathConf> tOut;
|
|
for (auto & p : m_paths)
|
|
{
|
|
tOut.push_back(p.second->conf());
|
|
}
|
|
return tOut;
|
|
}
|
|
|
|
} // namespace mtx
|