forked from sony/nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry_resources.cpp
61 lines (50 loc) · 2.48 KB
/
registry_resources.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include "nmos/registry_resources.h"
#include "cpprest/host_utils.h"
#include "cpprest/uri_builder.h"
#include "nmos/api_utils.h" // for nmos::http_scheme
#include "nmos/node_resource.h"
namespace nmos
{
namespace experimental
{
nmos::resource make_registry_node(const nmos::id& id, const nmos::settings& settings)
{
using web::json::value;
using web::json::value_of;
auto resource = nmos::make_node(id, settings);
auto& data = resource.data;
const auto at_least_one_host_address = value_of({ value::string(nmos::fields::host_address(settings)) });
const auto& host_addresses = settings.has_field(nmos::fields::host_addresses) ? nmos::fields::host_addresses(settings) : at_least_one_host_address.as_array();
// This is the experimental REST API for mDNS Service Discovery
auto mdns_uri = web::uri_builder()
.set_scheme(nmos::http_scheme(settings))
.set_port(nmos::experimental::fields::mdns_port(settings))
.set_path(U("/x-dns-sd/v1.0"));
auto type = U("urn:x-dns-sd/v1.0");
if (nmos::experimental::fields::client_secure(settings))
{
web::json::push_back(data[U("services")], value_of({
{ U("href"), mdns_uri.set_host(nmos::get_host(settings)).to_uri().to_string() },
{ U("type"), type }
}));
}
else for (const auto& host_address : host_addresses)
{
web::json::push_back(data[U("services")], value_of({
{ U("href"), mdns_uri.set_host(host_address.as_string()).to_uri().to_string() },
{ U("type"), type }
}));
}
resource.health = health_forever;
return resource;
}
// insert a node resource according to the settings; return an iterator to the inserted node resource,
// or to a resource that prevented the insertion, and a bool denoting whether the insertion took place
std::pair<resources::iterator, bool> insert_registry_resources(nmos::resources& resources, const nmos::settings& settings)
{
const auto& seed_id = nmos::experimental::fields::seed_id(settings);
auto node_id = nmos::make_repeatable_id(seed_id, U("/x-nmos/node/self"));
return insert_resource(resources, make_registry_node(node_id, settings));
}
}
}