#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 & paths) { std::vector> tStop; std::vector> tStart; { std::lock_guard 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(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> tAll; { std::lock_guard lock(m_mutex); tAll.swap(m_paths); } for (auto & p : tAll) { p.second->stop(); } } std::shared_ptr PathManager::find(const std::string & name) const { std::lock_guard lock(m_mutex); auto it = m_paths.find(name); return it == m_paths.end() ? nullptr : it->second; } std::vector PathManager::list() const { std::vector> tAll; { std::lock_guard lock(m_mutex); for (auto & p : m_paths) { tAll.push_back(p.second); } } std::vector tOut; for (auto & p : tAll) { tOut.push_back(p->info()); } return tOut; } std::vector PathManager::confs() const { std::lock_guard lock(m_mutex); std::vector tOut; for (auto & p : m_paths) { tOut.push_back(p.second->conf()); } return tOut; } } // namespace mtx