diff --git a/cpp/libs/ossrf_nmos_api/lib/include/ossrf/nmos/api/nmos.h b/cpp/libs/ossrf_nmos_api/lib/include/ossrf/nmos/api/nmos.h index 05dbaaa..3cf7637 100644 --- a/cpp/libs/ossrf_nmos_api/lib/include/ossrf/nmos/api/nmos.h +++ b/cpp/libs/ossrf_nmos_api/lib/include/ossrf/nmos/api/nmos.h @@ -25,6 +25,10 @@ namespace ossrf [[nodiscard]] virtual bisect::maybe_ok modify_device(const bisect::nmoscpp::nmos_device_t& device) = 0; + [[nodiscard]] virtual bisect::maybe_ok modify_device_sub_resources(const std::string& device_id, + const std::vector& receivers, + const std::vector& senders) = 0; + [[nodiscard]] virtual bisect::maybe_ok modify_receiver(const std::string& device_id, const bisect::nmoscpp::nmos_receiver_t& config) = 0; diff --git a/cpp/libs/ossrf_nmos_api/lib/include/ossrf/nmos/api/nmos_impl.h b/cpp/libs/ossrf_nmos_api/lib/include/ossrf/nmos/api/nmos_impl.h index 7977956..ae5c55a 100644 --- a/cpp/libs/ossrf_nmos_api/lib/include/ossrf/nmos/api/nmos_impl.h +++ b/cpp/libs/ossrf_nmos_api/lib/include/ossrf/nmos/api/nmos_impl.h @@ -26,6 +26,10 @@ namespace ossrf [[nodiscard]] bisect::maybe_ok modify_device(const bisect::nmoscpp::nmos_device_t& config) noexcept override; + [[nodiscard]] bisect::maybe_ok + modify_device_sub_resources(const std::string& device_id, const std::vector& receivers, + const std::vector& senders) noexcept override; + [[nodiscard]] bisect::maybe_ok modify_receiver(const std::string& device_id, const bisect::nmoscpp::nmos_receiver_t& config) noexcept override; diff --git a/cpp/libs/ossrf_nmos_api/lib/src/context/resource_map.cpp b/cpp/libs/ossrf_nmos_api/lib/src/context/resource_map.cpp index 193d577..4fe8e12 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/context/resource_map.cpp +++ b/cpp/libs/ossrf_nmos_api/lib/src/context/resource_map.cpp @@ -54,3 +54,39 @@ expected resource_map_t::find_resource(const std::string& res BST_FAIL("didn't find any resource with ID {}", resource_id); return {}; } + +std::vector resource_map_t::get_sender_ids() const +{ + std::vector ids; + lock_t lock(mutex_); + for(const auto& [id, resources] : map_) + { + for(const auto& resource : resources) + { + if(resource->get_resource_type() == nmos::types::sender) + { + ids.push_back(id); + } + } + } + + return ids; +} + +std::vector resource_map_t::get_receiver_ids() const +{ + std::vector ids; + lock_t lock(mutex_); + for(const auto& [id, resources] : map_) + { + for(const auto& resource : resources) + { + if(resource->get_resource_type() == nmos::types::receiver) + { + ids.push_back(id); + } + } + } + + return ids; +} \ No newline at end of file diff --git a/cpp/libs/ossrf_nmos_api/lib/src/context/resource_map.h b/cpp/libs/ossrf_nmos_api/lib/src/context/resource_map.h index a1edc4a..fa25b44 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/context/resource_map.h +++ b/cpp/libs/ossrf_nmos_api/lib/src/context/resource_map.h @@ -10,7 +10,7 @@ namespace ossrf { class resource_map_t { - std::mutex mutex_; + mutable std::mutex mutex_; std::unordered_map> map_; using lock_t = std::unique_lock; @@ -20,6 +20,9 @@ namespace ossrf void erase(std::string); bisect::expected find_resource(const std::string& resource_id); + std::vector get_sender_ids() const; + std::vector get_receiver_ids() const; + }; using resource_map_ptr = std::shared_ptr; diff --git a/cpp/libs/ossrf_nmos_api/lib/src/nmos_client.cpp b/cpp/libs/ossrf_nmos_api/lib/src/nmos_client.cpp index 4e13d2a..105eaba 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/nmos_client.cpp +++ b/cpp/libs/ossrf_nmos_api/lib/src/nmos_client.cpp @@ -14,6 +14,17 @@ using namespace bisect; using namespace ossrf; using json = nlohmann::json; +namespace +{ + maybe_ok update_device_sub_resources(nmos_context_ptr context, const std::string& device_id) + { + const auto senders_ids = context->resources().get_sender_ids(); + const auto receiver_ids = context->resources().get_receiver_ids(); + BST_CHECK(context->nmos().modify_device_sub_resources(utility::s2us(device_id), receiver_ids, senders_ids)); + return {}; + } +} // namespace + struct nmos_client_t::impl { std::string node_id_; @@ -34,7 +45,7 @@ expected nmos_client_t::create(const std::string& node_id, return nmos_client_uptr(new nmos_client_t{std::move(i)}); } -nmos_client_t::nmos_client_t(std::unique_ptr&& i) noexcept : impl_(std::move(i)){}; +nmos_client_t::nmos_client_t(std::unique_ptr&& i) noexcept : impl_(std::move(i)) {}; nmos_client_t::~nmos_client_t() { @@ -61,6 +72,7 @@ maybe_ok nmos_client_t::add_receiver(const std::string& device_id, const std::st auto r = std::make_shared(device_id, receiver_config, callback); impl_->context_->resources().insert(receiver_config.id, std::move(r)); + BST_CHECK(update_device_sub_resources(impl_->context_, device_id)); return {}; } @@ -76,6 +88,7 @@ maybe_ok nmos_client_t::add_sender(const std::string& device_id, const std::stri auto r = std::make_shared(device_id, sender_config, callback); impl_->context_->resources().insert(sender_config.id, std::move(r)); + BST_CHECK(update_device_sub_resources(impl_->context_, device_id)); return {}; } diff --git a/cpp/libs/ossrf_nmos_api/lib/src/nmos_impl.cpp b/cpp/libs/ossrf_nmos_api/lib/src/nmos_impl.cpp index b603f25..5ee888e 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/nmos_impl.cpp +++ b/cpp/libs/ossrf_nmos_api/lib/src/nmos_impl.cpp @@ -24,7 +24,7 @@ nmos_uptr nmos_impl::create(const std::string& node_id) return nmos_uptr(new nmos_impl{std::move(i)}); } -nmos_impl::nmos_impl(std::unique_ptr&& i) noexcept : impl_(std::move(i)){}; +nmos_impl::nmos_impl(std::unique_ptr&& i) noexcept : impl_(std::move(i)) {}; nmos_impl::~nmos_impl() { @@ -116,6 +116,15 @@ maybe_ok nmos_impl::modify_device(const nmos_device_t& config) noexcept [&](nmos::resource& resource) { resource = device_resource; }); } +maybe_ok nmos_impl::modify_device_sub_resources(const std::string& device_id, const std::vector& receivers, + const std::vector& senders) noexcept +{ + return impl_->controller_->modify_resource(device_id, [&](nmos::resource& resource) { + resource.data[nmos::fields::receivers] = web::json::value_from_elements(receivers); + resource.data[nmos::fields::senders] = web::json::value_from_elements(senders); + }); +} + maybe_ok nmos_impl::modify_receiver(const std::string& device_id, const nmos_receiver_t& config) noexcept { auto receiver = impl_->controller_->make_receiver(utility::s2us(device_id), config); diff --git a/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource.h b/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource.h index 213bd91..40be9ba 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource.h +++ b/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource.h @@ -5,6 +5,7 @@ #include #include #include +#include namespace ossrf { @@ -21,6 +22,8 @@ namespace ossrf [[nodiscard]] virtual const std::string& get_id() const = 0; [[nodiscard]] virtual const std::string& get_device_id() const = 0; + + [[nodiscard]] virtual nmos::type get_resource_type() const = 0; }; using nmos_resource_ptr = std::shared_ptr; diff --git a/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_receiver.cpp b/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_receiver.cpp index 6554bf1..c59e180 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_receiver.cpp +++ b/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_receiver.cpp @@ -104,3 +104,8 @@ maybe_ok nmos_resource_receiver_t::handle_activation(bool master_enable, json& t return {}; } + +nmos::type nmos_resource_receiver_t::get_resource_type() const +{ + return nmos::types::receiver; +} diff --git a/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_receiver.h b/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_receiver.h index dfbed06..4ae145d 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_receiver.h +++ b/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_receiver.h @@ -20,6 +20,8 @@ namespace ossrf const std::string& get_device_id() const override; + nmos::type get_resource_type() const override; + private: const bisect::nmoscpp::nmos_receiver_t config_; bisect::nmoscpp::receiver_activation_callback_t activation_callback_; diff --git a/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_sender.cpp b/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_sender.cpp index e8e39fe..0668ac6 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_sender.cpp +++ b/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_sender.cpp @@ -39,3 +39,8 @@ maybe_ok nmos_resource_sender_t::handle_patch(bool master_enable, const json& co { return {}; } + +nmos::type nmos_resource_sender_t::get_resource_type() const +{ + return nmos::types::receiver; +} diff --git a/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_sender.h b/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_sender.h index d12e90e..8379e6d 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_sender.h +++ b/cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource_sender.h @@ -20,6 +20,8 @@ namespace ossrf const std::string& get_device_id() const override; + nmos::type get_resource_type() const override; + private: const bisect::nmoscpp::nmos_sender_t config_; bisect::nmoscpp::sender_activation_callback_t activation_callback_;