-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathregistry_server.cpp
178 lines (131 loc) · 10.5 KB
/
registry_server.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "nmos/registry_server.h"
#include "cpprest/ws_listener.h"
#include "cpprest/ws_utils.h"
#include "mdns/service_advertiser.h"
#include "nmos/admin_ui.h"
#include "nmos/api_utils.h"
#include "nmos/logging_api.h"
#include "nmos/model.h"
#include "nmos/mdns.h"
#include "nmos/mdns_api.h"
#include "nmos/node_api.h"
#include "nmos/query_api.h"
#include "nmos/query_ws_api.h"
#include "nmos/registration_api.h"
#include "nmos/registry_resources.h"
#include "nmos/schemas_api.h"
#include "nmos/server.h"
#include "nmos/server_utils.h"
#include "nmos/settings_api.h"
#include "nmos/system_api.h"
#include "nmos/system_resources.h"
namespace nmos
{
void advertise_registry_thread(nmos::registry_model& model, slog::base_gate& gate);
namespace experimental
{
// Construct a server instance for an NMOS Registry instance, implementing the IS-04 Registration and Query APIs, the Node API, the IS-09 System API
// and the experimental DNS-SD Browsing API, Logging API and Settings API, according to the specified data models
nmos::server make_registry_server(nmos::registry_model& registry_model, nmos::experimental::registry_implementation registry_implementation, nmos::experimental::log_model& log_model, slog::base_gate& gate)
{
// Log the API addresses we'll be using
slog::log<slog::severities::info>(gate, SLOG_FLF) << "Configuring nmos-cpp registry with its primary Node API at: " << nmos::get_host(registry_model.settings) << ":" << nmos::fields::node_port(registry_model.settings);
slog::log<slog::severities::info>(gate, SLOG_FLF) << "Configuring nmos-cpp registry with its primary Registration API at: " << nmos::get_host(registry_model.settings) << ":" << nmos::fields::registration_port(registry_model.settings);
slog::log<slog::severities::info>(gate, SLOG_FLF) << "Configuring nmos-cpp registry with its primary Query API at: " << nmos::get_host(registry_model.settings) << ":" << nmos::fields::query_port(registry_model.settings);
nmos::server registry_server{ registry_model };
// Set up the APIs, assigning them to the configured ports
const auto server_secure = nmos::experimental::fields::server_secure(registry_model.settings);
const auto hsts = nmos::experimental::get_hsts(registry_model.settings);
const auto server_address = nmos::experimental::fields::server_address(registry_model.settings);
// Configure the DNS-SD Browsing API
const host_port mdns_address(nmos::experimental::fields::mdns_address(registry_model.settings), nmos::experimental::fields::mdns_port(registry_model.settings));
registry_server.api_routers[mdns_address].mount({}, nmos::experimental::make_mdns_api(registry_model, gate));
// Configure the Settings API
const host_port settings_address(nmos::experimental::fields::settings_address(registry_model.settings), nmos::experimental::fields::settings_port(registry_model.settings));
registry_server.api_routers[settings_address].mount({}, nmos::experimental::make_settings_api(registry_model, log_model, gate));
// Configure the Logging API
const host_port logging_address(nmos::experimental::fields::logging_address(registry_model.settings), nmos::experimental::fields::logging_port(registry_model.settings));
registry_server.api_routers[logging_address].mount({}, nmos::experimental::make_logging_api(log_model, gate));
// Configure the Query API
registry_server.api_routers[{ {}, nmos::fields::query_port(registry_model.settings) }].mount({}, nmos::make_query_api(registry_model, gate));
// "Source ID of the Query API instance issuing the data Grain"
// See https://specs.amwa.tv/is-04/releases/v1.2.0/APIs/schemas/with-refs/queryapi-subscriptions-websocket.html
const nmos::id query_id = nmos::make_repeatable_id(nmos::experimental::fields::seed_id(registry_model.settings), U("/x-nmos/query"));
auto& query_ws_api = registry_server.ws_handlers[{ {}, nmos::fields::query_ws_port(registry_model.settings) }];
query_ws_api.first = nmos::make_query_ws_api(query_id, registry_model, query_ws_api.second, gate);
// Configure the Registration API
registry_server.api_routers[{ {}, nmos::fields::registration_port(registry_model.settings) }].mount({}, nmos::make_registration_api(registry_model, gate));
// Configure the Node API
registry_server.api_routers[{ {}, nmos::fields::node_port(registry_model.settings) }].mount({}, nmos::make_node_api(registry_model, {}, gate));
// set up the node resources
auto& self_resources = registry_model.node_resources;
nmos::experimental::insert_registry_resources(self_resources, registry_model.settings);
// add the self resources to the Registration API resources
// (for now just copy them directly, since these resources currently do not change and are configured to never expire)
registry_model.registry_resources.insert(self_resources.begin(), self_resources.end());
// Configure the System API
// set up the system global configuration resource
nmos::experimental::assign_system_global_resource(registry_model.system_global_resource, registry_model.settings);
registry_server.api_routers[{ {}, nmos::fields::system_port(registry_model.settings) }].mount({}, nmos::make_system_api(registry_model, gate));
// Configure the Schemas API
const host_port schemas_address(nmos::experimental::fields::schemas_address(registry_model.settings), nmos::experimental::fields::schemas_port(registry_model.settings));
registry_server.api_routers[schemas_address].mount({}, nmos::experimental::make_schemas_api(gate));
// Configure the Admin UI
const utility::string_t admin_filesystem_root = U("./admin");
const host_port admin_address(nmos::experimental::fields::admin_address(registry_model.settings), nmos::experimental::fields::admin_port(registry_model.settings));
registry_server.api_routers[admin_address].mount({}, nmos::experimental::make_admin_ui(admin_filesystem_root, gate));
// Set up the listeners for each HTTP API port
auto http_config = nmos::make_http_listener_config(registry_model.settings, registry_implementation.load_server_certificates, registry_implementation.load_dh_param, registry_implementation.get_ocsp_response, gate);
for (auto& api_router : registry_server.api_routers)
{
// if IP address isn't specified for this router, use default server address or wildcard address
const auto& host = !api_router.first.first.empty() ? api_router.first.first : !server_address.empty() ? server_address : web::http::experimental::listener::host_wildcard;
// map the configured client port to the server port on which to listen
// hmm, this should probably also take account of the address
registry_server.http_listeners.push_back(nmos::make_api_listener(server_secure, host, nmos::experimental::server_port(api_router.first.second, registry_model.settings), api_router.second, http_config, hsts, gate));
}
// Set up the handlers for each WebSocket API port
auto websocket_config = nmos::make_websocket_listener_config(registry_model.settings, registry_implementation.load_server_certificates, registry_implementation.load_dh_param, registry_implementation.get_ocsp_response, gate);
websocket_config.set_log_callback(nmos::make_slog_logging_callback(gate));
for (auto& ws_handler : registry_server.ws_handlers)
{
// if IP address isn't specified for this router, use default server address or wildcard address
const auto& host = !ws_handler.first.first.empty() ? ws_handler.first.first : !server_address.empty() ? server_address : web::websockets::experimental::listener::host_wildcard;
// map the configured client port to the server port on which to listen
// hmm, this should probably also take account of the address
registry_server.ws_listeners.push_back(nmos::make_ws_api_listener(server_secure, host, nmos::experimental::server_port(ws_handler.first.second, registry_model.settings), ws_handler.second.first, websocket_config, gate));
}
auto& query_ws_listener = registry_server.ws_listeners.back();
// Set up registry management (including the DNS-SD advertisements)
registry_server.thread_functions.assign({
[&] { nmos::send_query_ws_events_thread(query_ws_listener, registry_model, query_ws_api.second, gate); },
[&] { nmos::erase_expired_resources_thread(registry_model, gate); },
[&] { nmos::advertise_registry_thread(registry_model, gate); }
});
return registry_server;
}
// deprecated
nmos::server make_registry_server(nmos::registry_model& registry_model, nmos::experimental::log_model& log_model, slog::base_gate& gate)
{
return make_registry_server(registry_model, registry_implementation(), log_model, gate);
}
}
void advertise_registry_thread(nmos::registry_model& model, slog::base_gate& gate)
{
auto lock = model.read_lock();
// Configure the DNS-SD advertisements for our APIs
mdns::service_advertiser advertiser(gate);
mdns::service_advertiser_guard advertiser_guard(advertiser);
const auto pri = nmos::fields::pri(model.settings);
if (nmos::service_priorities::no_priority != pri) // no_priority allows the registry to run unadvertised
{
nmos::experimental::register_addresses(advertiser, model.settings);
nmos::experimental::register_service(advertiser, nmos::service_types::query, model.settings);
nmos::experimental::register_service(advertiser, nmos::service_types::registration, model.settings);
nmos::experimental::register_service(advertiser, nmos::service_types::node, model.settings);
nmos::experimental::register_service(advertiser, nmos::service_types::system, model.settings);
}
// wait for the thread to be interrupted because the server is being shut down
model.shutdown_condition.wait(lock, [&] { return model.shutdown; });
}
}