Skip to content

Commit fcf4f0e

Browse files
committed
Move UUID generation to streaminfo, add UUID format unit test
1 parent 2ea94b2 commit fcf4f0e

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

src/stream_info_impl.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include "api_config.h"
33
#include "cast.h"
44
#include <algorithm>
5+
#include <boost/uuid/random_generator.hpp>
6+
#include <boost/uuid/uuid_io.hpp>
57
#include <loguru.hpp>
68
#include <sstream>
79
#include <utility>
@@ -254,6 +256,12 @@ void stream_info_impl::uid(const std::string &v) {
254256
doc_.child("info").child("uid").first_child().set_value(uid_.c_str());
255257
}
256258

259+
const std::string& stream_info_impl::reset_uid()
260+
{
261+
uid(lslboost::uuids::to_string(lslboost::uuids::random_generator()()));
262+
return uid_;
263+
}
264+
257265
void stream_info_impl::session_id(const std::string &v) {
258266
session_id_ = v;
259267
doc_.child("info").child("session_id").first_child().set_value(session_id_.c_str());

src/stream_info_impl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ class stream_info_impl {
152152
const std::string &uid() const { return uid_; }
153153
void uid(const std::string &v);
154154

155+
/// Reset the UID to a randomly generated UUID4
156+
const std::string &reset_uid();
157+
155158
/**
156159
* Get/Set the session id for the given stream.
157160
*

src/tcp_server.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
#include <boost/asio/read_until.hpp>
1313
#include <boost/asio/streambuf.hpp>
1414
#include <boost/asio/write.hpp>
15-
#include <boost/uuid/random_generator.hpp>
16-
#include <boost/uuid/uuid_io.hpp>
1715
#include <loguru.hpp>
1816
#include <memory>
1917
#include <thread>
@@ -158,7 +156,7 @@ tcp_server::tcp_server(stream_info_impl_p info, io_context_p io, send_buffer_p s
158156
// and assign connection-dependent fields
159157
// (note: this may be assigned multiple times by multiple TCPs during setup but does not matter)
160158
info_->session_id(api_config::get_instance()->session_id());
161-
info_->uid(lslboost::uuids::to_string(lslboost::uuids::random_generator()()));
159+
info_->reset_uid();
162160
info_->created_at(lsl_clock());
163161
info_->hostname(asio::ip::host_name());
164162
if (protocol == tcp::v4())

testing/test_int_streaminfo.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,32 @@
11
#include "../src/api_config.h"
22
#include "../src/stream_info_impl.h"
3+
#include <cctype>
34
#include <loguru.hpp>
45

56
#include <catch2/catch.hpp>
67

8+
template<typename T, const std::size_t N>
9+
bool contains(const T(&valid)[N], const T target) {
10+
for(T e: valid)
11+
if(e==target) return true;
12+
return false;
13+
}
14+
15+
TEST_CASE("uid", "[basic][streaminfo]") {
16+
lsl::stream_info_impl info;
17+
const std::string uid = info.reset_uid();
18+
REQUIRE(uid.length() == 36);
19+
for(auto i=0u; i<uid.size(); ++i)
20+
if(i==8||i==13||i==18||i==23) REQUIRE(uid[i] == '-');
21+
else REQUIRE(std::isxdigit(uid[i]));
22+
const char version = uid[14];
23+
REQUIRE(contains("1345", version));
24+
25+
const char variant = uid[19];
26+
REQUIRE(contains("89abAB", variant));
27+
REQUIRE(uid != info.reset_uid());
28+
}
29+
730
TEST_CASE("streaminfo matching via XPath", "[basic][streaminfo][xml]") {
831
lsl::stream_info_impl info(
932
"streamname", "streamtype", 8, 500, lsl_channel_format_t::cft_string, "sourceid");

0 commit comments

Comments
 (0)