forked from sony/nmos-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettings.cpp
76 lines (67 loc) · 3.75 KB
/
settings.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "nmos/settings.h"
#include "cpprest/host_utils.h"
#include "nmos/id.h"
namespace nmos
{
namespace details
{
// Inserts run-time default settings for those which are impossible to determine at compile-time
// if not already present in the specified settings
void insert_default_settings(settings& settings, bool registry)
{
web::json::insert(settings, std::make_pair(nmos::experimental::fields::seed_id, web::json::value::string(nmos::make_id())));
// if the "host_addresses" setting was omitted, add all the interface addresses
const auto interface_addresses = web::http::experimental::interface_addresses();
if (!interface_addresses.empty())
{
web::json::insert(settings, std::make_pair(nmos::fields::host_addresses, web::json::value_from_elements(interface_addresses)));
}
// if the "host_address" setting was omitted, use the first of the "host_addresses"
if (settings.has_field(nmos::fields::host_addresses))
{
web::json::insert(settings, std::make_pair(nmos::fields::host_address, nmos::fields::host_addresses(settings)[0]));
}
// if any of the specific "<api>_port" settings were omitted, use "http_port" if present
if (settings.has_field(nmos::fields::http_port))
{
const auto http_port = nmos::fields::http_port(settings);
if (registry) web::json::insert(settings, std::make_pair(nmos::fields::query_port, http_port));
// can't share a port between an http_listener and a websocket_listener, so don't apply this one...
//if (registry) web::json::insert(settings, std::make_pair(nmos::fields::query_ws_port, http_port));
web::json::insert(settings, std::make_pair(nmos::fields::registration_port, http_port));
web::json::insert(settings, std::make_pair(nmos::fields::node_port, http_port));
if (registry) web::json::insert(settings, std::make_pair(nmos::fields::system_port, http_port));
if (!registry) web::json::insert(settings, std::make_pair(nmos::fields::connection_port, http_port));
web::json::insert(settings, std::make_pair(nmos::experimental::fields::settings_port, http_port));
web::json::insert(settings, std::make_pair(nmos::experimental::fields::logging_port, http_port));
if (registry) web::json::insert(settings, std::make_pair(nmos::experimental::fields::admin_port, http_port));
if (registry) web::json::insert(settings, std::make_pair(nmos::experimental::fields::mdns_port, http_port));
}
}
}
void insert_node_default_settings(settings& settings)
{
details::insert_default_settings(settings, false);
}
void insert_registry_default_settings(settings& settings)
{
details::insert_default_settings(settings, true);
}
// Get host name from settings or return the default (system) host name
utility::string_t get_host_name(const settings& settings)
{
return !nmos::fields::host_name(settings).empty()
? nmos::fields::host_name(settings)
: web::http::experimental::host_name();
}
// Get host name or address to be used to construct response headers (e.g. 'Link' or 'Location')
// when a request URL is not available
utility::string_t get_host(const settings& settings)
{
// if secure communications are in use, return a host name
// otherwise, the preferred host address
return nmos::experimental::fields::client_secure(settings)
? get_host_name(settings)
: nmos::fields::host_address(settings);
}
}