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

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "pathManager.h"
  2. namespace mtx {
  3. PathManager::PathManager(const Conf & global) : LogParent("[path manager] "), m_global(global) {}
  4. PathManager::~PathManager()
  5. {
  6. closeAll();
  7. }
  8. void PathManager::applyPaths(const std::vector<PathConf> & paths)
  9. {
  10. std::vector<std::shared_ptr<Path>> tStop;
  11. std::vector<std::shared_ptr<Path>> tStart;
  12. {
  13. std::lock_guard<std::mutex> lock(m_mutex);
  14. for (auto it = m_paths.begin(); it != m_paths.end();)
  15. {
  16. const PathConf * tNew = nullptr;
  17. for (const PathConf & p : paths)
  18. {
  19. if (p.name == it->first)
  20. {
  21. tNew = &p;
  22. }
  23. }
  24. if (tNew == nullptr || *tNew != it->second->conf())
  25. {
  26. tStop.push_back(it->second);
  27. it = m_paths.erase(it);
  28. }
  29. else
  30. {
  31. ++it;
  32. }
  33. }
  34. for (const PathConf & p : paths)
  35. {
  36. if (m_paths.find(p.name) == m_paths.end())
  37. {
  38. auto tPath = std::make_shared<Path>(p, m_global);
  39. m_paths[p.name] = tPath;
  40. tStart.push_back(tPath);
  41. }
  42. }
  43. }
  44. // outside the lock: stopping joins the source thread
  45. for (auto & p : tStop)
  46. {
  47. log(LogLevel::Info, "removing path %s", p->conf().name.c_str());
  48. p->stop();
  49. }
  50. for (auto & p : tStart)
  51. {
  52. log(LogLevel::Info, "adding path %s (source %s%s)", p->conf().name.c_str(), hideCredentials(p->conf().source).c_str(), p->conf().record ? ", recording" : "");
  53. p->start();
  54. }
  55. }
  56. void PathManager::closeAll()
  57. {
  58. std::map<std::string, std::shared_ptr<Path>> tAll;
  59. {
  60. std::lock_guard<std::mutex> lock(m_mutex);
  61. tAll.swap(m_paths);
  62. }
  63. for (auto & p : tAll)
  64. {
  65. p.second->stop();
  66. }
  67. }
  68. std::shared_ptr<Path> PathManager::find(const std::string & name) const
  69. {
  70. std::lock_guard<std::mutex> lock(m_mutex);
  71. auto it = m_paths.find(name);
  72. return it == m_paths.end() ? nullptr : it->second;
  73. }
  74. std::vector<PathInfo> PathManager::list() const
  75. {
  76. std::vector<std::shared_ptr<Path>> tAll;
  77. {
  78. std::lock_guard<std::mutex> lock(m_mutex);
  79. for (auto & p : m_paths)
  80. {
  81. tAll.push_back(p.second);
  82. }
  83. }
  84. std::vector<PathInfo> tOut;
  85. for (auto & p : tAll)
  86. {
  87. tOut.push_back(p->info());
  88. }
  89. return tOut;
  90. }
  91. std::vector<PathConf> PathManager::confs() const
  92. {
  93. std::lock_guard<std::mutex> lock(m_mutex);
  94. std::vector<PathConf> tOut;
  95. for (auto & p : m_paths)
  96. {
  97. tOut.push_back(p.second->conf());
  98. }
  99. return tOut;
  100. }
  101. } // namespace mtx