Skip to content

Commit

Permalink
Associate devices with added senders and receivers
Browse files Browse the repository at this point in the history
  • Loading branch information
joaofigueiredobisect committed Sep 9, 2024
1 parent b597000 commit 34464a6
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 3 deletions.
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 @@ -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<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 @@ -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<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 @@ -54,3 +54,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;
}
5 changes: 4 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 @@ -10,7 +10,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 @@ -20,6 +20,9 @@ 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
15 changes: 14 additions & 1 deletion cpp/libs/ossrf_nmos_api/lib/src/nmos_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand All @@ -34,7 +45,7 @@ expected<nmos_client_uptr> 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<impl>&& i) noexcept : impl_(std::move(i)){};
nmos_client_t::nmos_client_t(std::unique_ptr<impl>&& i) noexcept : impl_(std::move(i)) {};

nmos_client_t::~nmos_client_t()
{
Expand All @@ -61,6 +72,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 @@ -76,6 +88,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
11 changes: 10 additions & 1 deletion cpp/libs/ossrf_nmos_api/lib/src/nmos_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<impl>&& i) noexcept : impl_(std::move(i)){};
nmos_impl::nmos_impl(std::unique_ptr<impl>&& i) noexcept : impl_(std::move(i)) {};

nmos_impl::~nmos_impl()
{
Expand Down Expand Up @@ -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<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 @@ -5,6 +5,7 @@
#include <nmos/id.h>
#include <string>
#include <memory>
#include <nmos/type.h>

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

0 comments on commit 34464a6

Please sign in to comment.