Skip to content

Commit

Permalink
Merge pull request #4 from AMWA-TV/jf/dev/add_tags_to_nmos_device
Browse files Browse the repository at this point in the history
Associate devices with added senders and receivers
  • Loading branch information
joaofigueiredobisect authored Sep 10, 2024
2 parents 9c47408 + bc9e229 commit 084162b
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cpp/libs/ossrf_nmos_api/lib/include/ossrf/nmos/api/nmos.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& receivers,
const std::vector<std::string>& senders) = 0;

[[nodiscard]] virtual bisect::maybe_ok modify_receiver(const std::string& device_id,
const bisect::nmoscpp::nmos_receiver_t& config) = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& receivers,
const std::vector<std::string>& senders) noexcept override;

[[nodiscard]] bisect::maybe_ok
modify_receiver(const std::string& device_id, const bisect::nmoscpp::nmos_receiver_t& config) noexcept override;

Expand Down
36 changes: 36 additions & 0 deletions cpp/libs/ossrf_nmos_api/lib/src/context/resource_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,39 @@ expected<nmos_resource_ptr> resource_map_t::find_resource(const std::string& res
BST_FAIL("didn't find any resource with ID {}", resource_id);
return {};
}

std::vector<std::string> resource_map_t::get_sender_ids() const
{
std::vector<std::string> 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<std::string> resource_map_t::get_receiver_ids() const
{
std::vector<std::string> 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;
}
4 changes: 3 additions & 1 deletion cpp/libs/ossrf_nmos_api/lib/src/context/resource_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace ossrf
{
class resource_map_t
{
std::mutex mutex_;
mutable std::mutex mutex_;
std::unordered_map<std::string, std::vector<nmos_resource_ptr>> map_;
using lock_t = std::unique_lock<std::mutex>;

Expand All @@ -34,6 +34,8 @@ namespace ossrf
void erase(std::string);

bisect::expected<nmos_resource_ptr> find_resource(const std::string& resource_id);
std::vector<std::string> get_sender_ids() const;
std::vector<std::string> get_receiver_ids() const;
};

using resource_map_ptr = std::shared_ptr<resource_map_t>;
Expand Down
13 changes: 13 additions & 0 deletions cpp/libs/ossrf_nmos_api/lib/src/nmos_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -75,6 +86,7 @@ maybe_ok nmos_client_t::add_receiver(const std::string& device_id, const std::st

auto r = std::make_shared<nmos_resource_receiver_t>(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 {};
}
Expand All @@ -90,6 +102,7 @@ maybe_ok nmos_client_t::add_sender(const std::string& device_id, const std::stri

auto r = std::make_shared<nmos_resource_sender_t>(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 {};
}
Expand Down
9 changes: 9 additions & 0 deletions cpp/libs/ossrf_nmos_api/lib/src/nmos_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string>& receivers,
const std::vector<std::string>& 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);
Expand Down
3 changes: 3 additions & 0 deletions cpp/libs/ossrf_nmos_api/lib/src/resources/nmos_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <nmos/id.h>
#include <string>
#include <memory>
#include <nmos/type.h>

namespace ossrf
{
Expand All @@ -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<nmos_resource_t>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down

0 comments on commit 084162b

Please sign in to comment.