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 5fda7ef..53b35c0 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 @@ -39,6 +39,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 561bf53..f4b37cf 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 @@ -40,6 +40,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 45956f6..1be2afd 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 @@ -68,3 +68,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; +} 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 bb60ed5..eaee561 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 @@ -24,7 +24,7 @@ namespace ossrf { class resource_map_t { - std::mutex mutex_; + mutable std::mutex mutex_; std::unordered_map> map_; using lock_t = std::unique_lock; @@ -34,6 +34,8 @@ 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 679585f..3bf9fe4 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/nmos_client.cpp +++ b/cpp/libs/ossrf_nmos_api/lib/src/nmos_client.cpp @@ -28,6 +28,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_; @@ -75,6 +86,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 {}; } @@ -90,6 +102,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 14112cb..4edd837 100644 --- a/cpp/libs/ossrf_nmos_api/lib/src/nmos_impl.cpp +++ b/cpp/libs/ossrf_nmos_api/lib/src/nmos_impl.cpp @@ -130,6 +130,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 e77740a..c9a97ad 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 @@ -19,6 +19,7 @@ #include #include #include +#include namespace ossrf { @@ -35,6 +36,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 272bacd..55927f9 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 @@ -118,3 +118,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 914fbf5..265c514 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 @@ -34,6 +34,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 89437fb..7442af0 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 @@ -53,3 +53,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::sender; +} 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 41df24f..34eb153 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 @@ -34,6 +34,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_;