-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathclient.cpp
369 lines (301 loc) · 12.3 KB
/
client.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
// SPDX-FileCopyrightText: Copyright (c) 2024 Cisco Systems
// SPDX-License-Identifier: BSD-2-Clause
#include <oss/cxxopts.hpp>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <quicr/client.h>
#include "helper_functions.h"
#include "signal_handler.h"
namespace qclient_vars {
bool publish_clock{ false };
}
/**
* @brief Subscribe track handler
* @details Subscribe track handler used for the subscribe command line option.
*/
class MySubscribeTrackHandler : public quicr::SubscribeTrackHandler
{
public:
MySubscribeTrackHandler(const quicr::FullTrackName& full_track_name)
: SubscribeTrackHandler(full_track_name)
{
}
void ObjectReceived(const quicr::ObjectHeaders&, quicr::BytesSpan data) override
{
std::string msg(data.begin(), data.end());
SPDLOG_INFO("Received message: {0}", msg);
}
void StatusChanged(Status status) override
{
switch (status) {
case Status::kOk: {
if (auto track_alias = GetTrackAlias(); track_alias.has_value()) {
SPDLOG_INFO("Track alias: {0} is ready to read", track_alias.value());
}
} break;
default:
break;
}
}
};
/**
* @brief Publish track handler
* @details Publish track handler used for the publish command line option
*/
class MyPublishTrackHandler : public quicr::PublishTrackHandler
{
public:
MyPublishTrackHandler(const quicr::FullTrackName& full_track_name,
quicr::TrackMode track_mode,
uint8_t default_priority,
uint32_t default_ttl)
: quicr::PublishTrackHandler(full_track_name, track_mode, default_priority, default_ttl)
{
}
void StatusChanged(Status status) override
{
switch (status) {
case Status::kOk: {
if (auto track_alias = GetTrackAlias(); track_alias.has_value()) {
SPDLOG_INFO("Track alias: {0} is ready to read", track_alias.value());
}
} break;
default:
break;
}
}
};
/**
* @brief MoQ client
* @details Implementation of the MoQ Client
*/
class MyClient : public quicr::Client
{
public:
MyClient(const quicr::ClientConfig& cfg, bool& stop_threads)
: quicr::Client(cfg)
, stop_threads_(stop_threads)
{
}
void StatusChanged(Status status) override
{
switch (status) {
case Status::kReady:
SPDLOG_INFO("Connection ready");
break;
case Status::kConnecting:
break;
default:
SPDLOG_INFO("Connection failed {0}", static_cast<int>(status));
stop_threads_ = true;
moq_example::terminate = true;
moq_example::termination_reason = "Connection failed";
moq_example::cv.notify_all();
break;
}
}
private:
bool& stop_threads_;
};
/* -------------------------------------------------------------------------------------------------
* Publisher Thread to perform publishing
* -------------------------------------------------------------------------------------------------
*/
void
DoPublisher(const quicr::FullTrackName& full_track_name, const std::shared_ptr<quicr::Client>& client, const bool& stop)
{
auto track_handler = std::make_shared<MyPublishTrackHandler>(
full_track_name, quicr::TrackMode::kStreamPerGroup /*mode*/, 2 /*prirority*/, 3000 /*ttl*/);
SPDLOG_INFO("Started publisher track");
bool published_track{ false };
bool sending{ false };
uint64_t group_id{ 0 };
uint64_t object_id{ 0 };
while (not stop) {
if ((!published_track) && (client->GetStatus() == MyClient::Status::kReady)) {
SPDLOG_INFO("Publish track ");
client->PublishTrack(track_handler);
published_track = true;
}
if (track_handler->GetStatus() != MyPublishTrackHandler::Status::kOk) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
continue;
}
if (!sending) {
SPDLOG_INFO("--------------------------------------------------------------------------");
if (qclient_vars::publish_clock) {
SPDLOG_INFO(" Publishing clock timestamp every second");
} else {
SPDLOG_INFO(" Type message and press enter to send");
}
SPDLOG_INFO("--------------------------------------------------------------------------");
sending = true;
}
std::string msg;
if (qclient_vars::publish_clock) {
std::this_thread::sleep_for(std::chrono::milliseconds(999));
msg = quicr::example::GetTimeStr();
SPDLOG_INFO(msg);
} else { // stdin
getline(std::cin, msg);
SPDLOG_INFO("Send message: {0}", msg);
}
if (object_id % 10 == 0) { // Set new group
object_id = 0;
group_id++;
}
quicr::ObjectHeaders obj_headers = { group_id, object_id++, msg.size(), 2 /*priority*/,
3000 /* ttl */, std::nullopt, std::nullopt };
track_handler->PublishObject(obj_headers, { reinterpret_cast<uint8_t*>(msg.data()), msg.size() });
}
client->UnpublishTrack(track_handler);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
SPDLOG_INFO("Publisher done track");
}
/* -------------------------------------------------------------------------------------------------
* Subscriber thread to perform subscribe
* -------------------------------------------------------------------------------------------------
*/
void
DoSubscriber(const quicr::FullTrackName& full_track_name,
const std::shared_ptr<quicr::Client>& client,
const bool& stop)
{
auto track_handler = std::make_shared<MySubscribeTrackHandler>(full_track_name);
SPDLOG_INFO("Started subscriber");
bool subscribe_track{ false };
while (not stop) {
if ((!subscribe_track) && (client->GetStatus() == MyClient::Status::kReady)) {
SPDLOG_INFO("Subscribing to track");
client->SubscribeTrack(track_handler);
subscribe_track = true;
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
client->UnsubscribeTrack(track_handler);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
SPDLOG_INFO("Subscriber done track");
}
/* -------------------------------------------------------------------------------------------------
* Main program
* -------------------------------------------------------------------------------------------------
*/
quicr::ClientConfig
InitConfig(cxxopts::ParseResult& cli_opts, bool& enable_pub, bool& enable_sub)
{
quicr::ClientConfig config;
std::string qlog_path;
if (cli_opts.count("qlog")) {
qlog_path = cli_opts["qlog"].as<std::string>();
}
if (cli_opts.count("debug") && cli_opts["debug"].as<bool>() == true) {
SPDLOG_INFO("setting debug level");
spdlog::set_level(spdlog::level::debug);
}
if (cli_opts.count("version") && cli_opts["version"].as<bool>() == true) {
SPDLOG_INFO("QuicR library version: {}", QUICR_VERSION);
exit(0);
}
if (cli_opts.count("pub_namespace") && cli_opts.count("pub_name")) {
enable_pub = true;
SPDLOG_INFO("Publisher enabled using track namespace: {0} name: {1}",
cli_opts["pub_namespace"].as<std::string>(),
cli_opts["pub_name"].as<std::string>());
}
if (cli_opts.count("clock") && cli_opts["clock"].as<bool>() == true) {
SPDLOG_INFO("Running in clock publish mode");
qclient_vars::publish_clock = true;
}
if (cli_opts.count("sub_namespace") && cli_opts.count("sub_name")) {
enable_sub = true;
SPDLOG_INFO("Subscriber enabled using track namespace: {0} name: {1}",
cli_opts["sub_namespace"].as<std::string>(),
cli_opts["sub_name"].as<std::string>());
}
config.endpoint_id = cli_opts["endpoint_id"].as<std::string>();
config.connect_uri = cli_opts["url"].as<std::string>();
config.transport_config.debug = cli_opts["debug"].as<bool>();
config.transport_config.use_reset_wait_strategy = false;
config.transport_config.time_queue_max_duration = 5000;
config.transport_config.tls_cert_filename = "";
config.transport_config.tls_key_filename = "";
config.transport_config.quic_qlog_path = qlog_path;
return config;
}
int
main(int argc, char* argv[])
{
int result_code = EXIT_SUCCESS;
cxxopts::Options options("qclient",
std::string("MOQ Example Client using QuicR Version: ") + std::string(QUICR_VERSION));
options.set_width(75)
.set_tab_expansion()
//.allow_unrecognised_options()
.add_options()("h,help", "Print help")("d,debug", "Enable debugging") // a bool parameter
("v,version", "QuicR Version") // a bool parameter
("r,url", "Relay URL", cxxopts::value<std::string>()->default_value("moq://localhost:1234"))(
"e,endpoint_id", "This client endpoint ID", cxxopts::value<std::string>()->default_value("moq-client"))(
"q,qlog", "Enable qlog using path", cxxopts::value<std::string>());
options.add_options("Publisher")("pub_namespace", "Track namespace", cxxopts::value<std::string>())(
"pub_name", "Track name", cxxopts::value<std::string>())(
"clock", "Publish clock timestamp every second instead of using STDIN chat");
options.add_options("Subscriber")("sub_namespace", "Track namespace", cxxopts::value<std::string>())(
"sub_name", "Track name", cxxopts::value<std::string>());
auto result = options.parse(argc, argv);
if (result.count("help")) {
std::cout << options.help({ "", "Publisher", "Subscriber" }) << std::endl;
return EXIT_SUCCESS;
}
// Install a signal handlers to catch operating system signals
installSignalHandlers();
// Lock the mutex so that main can then wait on it
std::unique_lock lock(moq_example::main_mutex);
bool enable_pub{ false };
bool enable_sub{ false };
quicr::ClientConfig config = InitConfig(result, enable_pub, enable_sub);
try {
bool stop_threads{ false };
auto client = std::make_shared<MyClient>(config, stop_threads);
if (client->Connect() != quicr::Transport::Status::kConnecting) {
SPDLOG_ERROR("Failed to connect to server due to invalid params, check URI");
exit(-1);
}
std::thread pub_thread, sub_thread;
if (enable_pub) {
const auto& pub_track_name = quicr::example::MakeFullTrackName(
result["pub_namespace"].as<std::string>(), result["pub_name"].as<std::string>(), 1001);
pub_thread = std::thread(DoPublisher, pub_track_name, client, std::ref(stop_threads));
}
if (enable_sub) {
const auto& sub_track_name = quicr::example::MakeFullTrackName(
result["sub_namespace"].as<std::string>(), result["sub_name"].as<std::string>(), 2001);
sub_thread = std::thread(DoSubscriber, sub_track_name, client, std::ref(stop_threads));
}
// Wait until told to terminate
moq_example::cv.wait(lock, [&]() { return moq_example::terminate; });
stop_threads = true;
SPDLOG_INFO("Stopping threads...");
if (pub_thread.joinable()) {
pub_thread.join();
}
if (sub_thread.joinable()) {
sub_thread.join();
}
client->Disconnect();
SPDLOG_INFO("Client done");
std::this_thread::sleep_for(std::chrono::milliseconds(3000));
// Unlock the mutex
lock.unlock();
} catch (const std::invalid_argument& e) {
std::cerr << "Invalid argument: " << e.what() << std::endl;
result_code = EXIT_FAILURE;
} catch (const std::exception& e) {
std::cerr << "Unexpected exception: " << e.what() << std::endl;
result_code = EXIT_FAILURE;
} catch (...) {
std::cerr << "Unexpected exception" << std::endl;
result_code = EXIT_FAILURE;
}
SPDLOG_INFO("Exit");
return result_code;
}