Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit f7898f5

Browse files
authored
Improve naming and make timeouts unsigned (#43)
When working on Java bindings, I noticed that some names were not completely obvious. At the same time, I also noticed it was possible to have negative timeouts, which I don't want.
1 parent 6bd70e2 commit f7898f5

File tree

4 files changed

+69
-68
lines changed

4 files changed

+69
-68
lines changed

libndt-client.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int main(int, char **argv) {
3333
using namespace measurement_kit;
3434
libndt::Settings settings;
3535
settings.verbosity = libndt::verbosity::quiet;
36-
settings.test_suite = 0; // you need to enable tests explicitly
36+
settings.nettest_flags = 0; // you need to enable tests explicitly
3737

3838
{
3939
argh::parser cmdline;
@@ -42,16 +42,16 @@ int main(int, char **argv) {
4242
cmdline.parse(argv);
4343
for (auto &flag : cmdline.flags()) {
4444
if (flag == "download") {
45-
settings.test_suite |= libndt::nettest::download;
45+
settings.nettest_flags |= libndt::nettest_flag::download;
4646
std::clog << "will run download" << std::endl;
4747
} else if (flag == "download-ext") {
48-
settings.test_suite |= libndt::nettest::download_ext;
48+
settings.nettest_flags |= libndt::nettest_flag::download_ext;
4949
std::clog << "will run download-ext" << std::endl;
5050
} else if (flag == "json") {
51-
settings.proto = libndt::protocol::json;
51+
settings.protocol_flags = libndt::protocol_flag::json;
5252
std::clog << "will use json" << std::endl;
5353
} else if (flag == "upload") {
54-
settings.test_suite |= libndt::nettest::upload;
54+
settings.nettest_flags |= libndt::nettest_flag::upload;
5555
std::clog << "will run upload" << std::endl;
5656
} else if (flag == "verbose") {
5757
settings.verbosity = libndt::verbosity::debug;

libndt.cpp

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ bool Client::query_mlabns() noexcept {
290290
}
291291
std::string body;
292292
if (!query_mlabns_curl( //
293-
impl->settings.mlabns_url, impl->settings.curl_timeout, &body)) {
293+
impl->settings.mlabns_url, impl->settings.timeout, &body)) {
294294
return false;
295295
}
296296
nlohmann::json json;
@@ -383,20 +383,20 @@ bool Client::recv_tests_ids() noexcept {
383383
bool Client::run_tests() noexcept {
384384
for (auto &tid : impl->granted_suite) {
385385
switch (tid) {
386-
case nettest::upload:
386+
case nettest_flag::upload:
387387
EMIT_INFO("running upload test");
388388
if (!run_upload()) {
389389
return false;
390390
}
391391
break;
392-
case nettest::meta:
392+
case nettest_flag::meta:
393393
EMIT_DEBUG("running meta test"); // don't annoy the user with this
394394
if (!run_meta()) {
395395
return false;
396396
}
397397
break;
398-
case nettest::download:
399-
case nettest::download_ext:
398+
case nettest_flag::download:
399+
case nettest_flag::download_ext:
400400
EMIT_INFO("running download test");
401401
if (!run_download()) {
402402
return false;
@@ -543,7 +543,7 @@ bool Client::run_download() noexcept {
543543
std::chrono::duration<double> measurement_interval = now - prev;
544544
std::chrono::duration<double> elapsed = now - begin;
545545
if (measurement_interval.count() > 0.25) {
546-
on_performance(nettest::download, nflows, recent_data,
546+
on_performance(nettest_flag::download, nflows, recent_data,
547547
measurement_interval.count(), elapsed.count(),
548548
impl->settings.max_runtime);
549549
recent_data = 0;
@@ -711,7 +711,7 @@ bool Client::run_upload() noexcept {
711711
std::chrono::duration<double> measurement_interval = now - prev;
712712
std::chrono::duration<double> elapsed = now - begin;
713713
if (measurement_interval.count() > 0.25) {
714-
on_performance(nettest::upload, nflows, recent_data,
714+
on_performance(nettest_flag::upload, nflows, recent_data,
715715
measurement_interval.count(), elapsed.count(),
716716
impl->settings.max_runtime);
717717
recent_data = 0;
@@ -750,32 +750,34 @@ bool Client::run_upload() noexcept {
750750
// Low-level API
751751

752752
bool Client::msg_write_login(const std::string &version) noexcept {
753-
static_assert(sizeof(impl->settings.test_suite) == 1, "test_suite too large");
753+
static_assert(sizeof(impl->settings.nettest_flags) == 1,
754+
"nettest_flags too large");
754755
uint8_t code = 0;
755-
impl->settings.test_suite |= nettest::status | nettest::meta;
756-
if ((impl->settings.test_suite & nettest::middlebox)) {
757-
EMIT_WARNING("msg_write_login(): nettest::middlebox: not implemented");
758-
impl->settings.test_suite &= ~nettest::middlebox;
756+
impl->settings.nettest_flags |= nettest_flag::status | nettest_flag::meta;
757+
if ((impl->settings.nettest_flags & nettest_flag::middlebox)) {
758+
EMIT_WARNING("msg_write_login(): nettest_flag::middlebox: not implemented");
759+
impl->settings.nettest_flags &= ~nettest_flag::middlebox;
759760
}
760-
if ((impl->settings.test_suite & nettest::simple_firewall)) {
761+
if ((impl->settings.nettest_flags & nettest_flag::simple_firewall)) {
761762
EMIT_WARNING(
762-
"msg_write_login(): nettest::simple_firewall: not implemented");
763-
impl->settings.test_suite &= ~nettest::simple_firewall;
763+
"msg_write_login(): nettest_flag::simple_firewall: not implemented");
764+
impl->settings.nettest_flags &= ~nettest_flag::simple_firewall;
764765
}
765-
if ((impl->settings.test_suite & nettest::upload_ext)) {
766-
EMIT_WARNING("msg_write_login(): nettest::upload_ext: not implemented");
767-
impl->settings.test_suite &= ~nettest::upload_ext;
766+
if ((impl->settings.nettest_flags & nettest_flag::upload_ext)) {
767+
EMIT_WARNING(
768+
"msg_write_login(): nettest_flag::upload_ext: not implemented");
769+
impl->settings.nettest_flags &= ~nettest_flag::upload_ext;
768770
}
769771
std::string serio;
770-
if ((impl->settings.proto & protocol::json) == 0) {
771-
serio = std::string{(char *)&impl->settings.test_suite,
772-
sizeof(impl->settings.test_suite)};
772+
if ((impl->settings.protocol_flags & protocol_flag::json) == 0) {
773+
serio = std::string{(char *)&impl->settings.nettest_flags,
774+
sizeof(impl->settings.nettest_flags)};
773775
code = msg_login;
774776
} else {
775777
code = msg_extended_login;
776778
nlohmann::json msg{
777779
{"msg", version},
778-
{"tests", std::to_string((unsigned)impl->settings.test_suite)},
780+
{"tests", std::to_string((unsigned)impl->settings.nettest_flags)},
779781
};
780782
try {
781783
serio = msg.dump();
@@ -785,7 +787,7 @@ bool Client::msg_write_login(const std::string &version) noexcept {
785787
}
786788
}
787789
assert(code != 0);
788-
if ((impl->settings.proto & protocol::websockets) != 0) {
790+
if ((impl->settings.protocol_flags & protocol_flag::websockets) != 0) {
789791
EMIT_WARNING("msg_write_login: websockets not supported");
790792
return false;
791793
}
@@ -800,7 +802,7 @@ bool Client::msg_write_login(const std::string &version) noexcept {
800802
// a different implementation depending on the actual protocol.
801803
bool Client::msg_write(uint8_t code, std::string &&msg) noexcept {
802804
EMIT_DEBUG("msg_write: message to send: " << represent(msg));
803-
if ((impl->settings.proto & protocol::json) != 0) {
805+
if ((impl->settings.protocol_flags & protocol_flag::json) != 0) {
804806
nlohmann::json json;
805807
json["msg"] = msg;
806808
try {
@@ -810,7 +812,7 @@ bool Client::msg_write(uint8_t code, std::string &&msg) noexcept {
810812
return false;
811813
}
812814
}
813-
if ((impl->settings.proto & protocol::websockets) != 0) {
815+
if ((impl->settings.protocol_flags & protocol_flag::websockets) != 0) {
814816
EMIT_WARNING("msg_write: websockets not supported");
815817
return false;
816818
}
@@ -944,14 +946,14 @@ bool Client::msg_expect(uint8_t expected_code, std::string *s) noexcept {
944946
bool Client::msg_read(uint8_t *code, std::string *msg) noexcept {
945947
assert(code != nullptr && msg != nullptr);
946948
std::string s;
947-
if ((impl->settings.proto & protocol::websockets) != 0) {
949+
if ((impl->settings.protocol_flags & protocol_flag::websockets) != 0) {
948950
EMIT_WARNING("msg_read: websockets not supported");
949951
return false;
950952
}
951953
if (!msg_read_legacy(code, &s)) {
952954
return false;
953955
}
954-
if ((impl->settings.proto & protocol::json) == 0) {
956+
if ((impl->settings.protocol_flags & protocol_flag::json) == 0) {
955957
std::swap(s, *msg);
956958
} else {
957959
nlohmann::json json;

libndt.hpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,21 @@ namespace libndt {
5555
namespace version {
5656

5757
/// Major API version number of measurement-kit/libndt.
58-
constexpr uint64_t api_major = 0;
58+
constexpr uint64_t major = 0;
5959

6060
/// Minor API version number of measurement-kit/libndt.
61-
constexpr uint64_t api_minor = 23;
61+
constexpr uint64_t minor = 23;
6262

6363
/// Patch API version number of measurement-kit/libndt.
64-
constexpr uint64_t api_patch = 0;
64+
constexpr uint64_t patch = 0;
6565

6666
} // namespace version
6767

68-
/// Contains nettests identifiers. You can run multiple nettests as part of
68+
/// Contains nettests flags. You can run multiple nettests as part of
6969
/// a single NDT transaction with a NDT server. To specify what nettests you
70-
/// want to run, modify Settings::test_suite accordingly, by using the
71-
/// constants contained inside of this namespace.
72-
namespace nettest {
70+
/// want to run, modify Settings::nettest_flags accordingly, by using the
71+
/// constants flags defined inside of this namespace.
72+
namespace nettest_flag {
7373

7474
constexpr uint8_t middlebox = 1U << 0;
7575

@@ -112,22 +112,21 @@ constexpr uint64_t debug = 3;
112112

113113
constexpr const char *ndt_version_compat = "v3.7.0";
114114

115-
/// Type containing the size of something.
116115
using Size = uint64_t;
117116

118-
/// Type containing the signed size of something.
119117
using Ssize = int64_t;
120118

121-
/// Type wide enough to contain a socket.
122119
using Socket = int64_t;
123120

124-
/// Type wide enough to contain `socklen_t`.
125121
using SockLen = int;
126122

127123
/// Contains flags definiting what protocol to use. Historically NDT used a
128124
/// binary, cleartext protocol for communicating with the server. Historically
129125
/// messages were raw strings framed using the binary framing.
130-
namespace protocol {
126+
namespace protocol_flag {
127+
128+
/// Indicates that no protocol flags have been set.
129+
constexpr uint64_t none = 0;
131130

132131
/// When this flag is set we use JSON messages. This specifically means that
133132
/// we send and receive JSON messages (as opposed to raw strings).
@@ -153,9 +152,9 @@ class Settings {
153152
/// hostname, mlab-ns won't be used.
154153
std::string mlabns_url = "https://mlab-ns.appspot.com/ndt";
155154

156-
/// cURL timeout used when querying mlab-ns. If you specify an explicit
157-
/// hostname, mlab-ns won't be used.
158-
long curl_timeout = 3 /* seconds */;
155+
/// Timeout used for I/O operations. \bug in v0.23.0 this timeout is only
156+
/// used for cURL operations, but this will be fixed in v0.24.0.
157+
uint16_t timeout = 3 /* seconds */;
159158

160159
/// Host name of the NDT server to use. If this is left blank (the default),
161160
/// we will use mlab-ns to discover a nearby server.
@@ -165,7 +164,7 @@ class Settings {
165164
std::string port = "3001";
166165

167166
/// The tests you want to run with the NDT server.
168-
uint8_t test_suite = nettest::download;
167+
uint8_t nettest_flags = nettest_flag::download;
169168

170169
/// Verbosity of the client. By default no message is emitted. Set to other
171170
/// values to get more messages (useful when debugging).
@@ -181,13 +180,13 @@ class Settings {
181180
/// Type of NDT protocol that you want to use. Depending on the requested
182181
/// protocol, you may need to change also the port. By default, NDT listens
183182
/// on port 3001 for in-clear communications and port 3010 for TLS ones.
184-
uint64_t proto = 0;
183+
uint64_t protocol_flags = protocol_flag::none;
185184

186185
/// Maximum time for which a nettest (i.e. download) is allowed to run. After
187186
/// this time has elapsed, the code will stop downloading (or uploading). It
188187
/// is meant as a safeguard to prevent the test for running for much more time
189188
/// than anticipated, due to buffering and/or changing network conditions.
190-
double max_runtime = 14 /* seconds */;
189+
uint16_t max_runtime = 14 /* seconds */;
191190

192191
/// SOCKSv5h port to use for tunnelling traffic using, e.g., Tor. If non
193192
/// empty, all DNS and TCP traffic should be tunnelled over such port.

0 commit comments

Comments
 (0)