Skip to content

Commit 0fccc4f

Browse files
committed
applied transfer id map
1 parent f4ca418 commit 0fccc4f

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

libcyphal_demo/src/application.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "application.hpp"
88

9+
#include <libcyphal/application/registry/registry_impl.hpp>
910
#include <libcyphal/platform/storage.hpp>
1011

1112
#include <cetl/pf17/cetlpf.hpp>

libcyphal_demo/src/file_downloader.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
#include <chrono>
2424
#include <cstddef>
25+
#include <iomanip>
2526
#include <iostream>
2627
#include <utility>
2728

@@ -74,6 +75,7 @@ class FileDownloader final
7475
}
7576

7677
read_request_.offset = 0;
78+
file_stats_.start_time = time_provider_.now();
7779
read_request_.path.path = {file_path.begin(), file_path.end(), &presentation_.memory()};
7880

7981
std::cout << "Getting file info (path='" << file_path << "')...\n";
@@ -275,10 +277,10 @@ class FileDownloader final
275277

276278
void complete()
277279
{
278-
const auto duration = time_provider_.now() - file_stats_.start_time;
279-
const auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
280+
const auto duration = time_provider_.now() - file_stats_.start_time;
280281
std::cout << "\nDownload completed (err=" << file_stats_.file_error.value //
281-
<< ", time=" << duration_ms << "ms).\n"
282+
<< ", time=" << std::fixed << std::setprecision(6) // NOLINT
283+
<< std::chrono::duration_cast<std::chrono::duration<double>>(duration).count() << "s).\n"
282284
<< std::flush;
283285

284286
get_info_promise_.reset();

libcyphal_demo/src/main.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <libcyphal/application/node.hpp>
1515
#include <libcyphal/presentation/presentation.hpp>
1616
#include <libcyphal/time_provider.hpp>
17+
#include <libcyphal/transport/transfer_id_map.hpp>
1718
#include <libcyphal/transport/transport.hpp>
1819
#include <libcyphal/transport/types.hpp>
1920
#include <libcyphal/types.hpp>
@@ -24,10 +25,12 @@
2425
#include <algorithm>
2526
#include <array>
2627
#include <cstdint>
28+
#include <functional>
2729
#include <iomanip>
2830
#include <ios>
2931
#include <iostream>
3032
#include <unistd.h>
33+
#include <unordered_map>
3134
#include <utility>
3235

3336
using namespace std::chrono_literals;
@@ -131,6 +134,36 @@ enum class ExitCode : std::uint8_t
131134

132135
}; // ExitCode
133136

137+
class TransferIdMap final : public libcyphal::transport::ITransferIdMap
138+
{
139+
public:
140+
explicit TransferIdMap(cetl::pmr::memory_resource& memory) noexcept
141+
: session_spec_to_transfer_id_{&memory}
142+
{
143+
}
144+
145+
private:
146+
using TransferId = libcyphal::transport::TransferId;
147+
using PmvAlloc = cetl::pmr::polymorphic_allocator<std::pair<const SessionSpec, TransferId>>;
148+
using PmrMap = std::unordered_map<SessionSpec, TransferId, std::hash<SessionSpec>, std::equal_to<>, PmvAlloc>;
149+
150+
// ITransferIdMap
151+
152+
TransferId getIdFor(const SessionSpec& session_spec) const noexcept override
153+
{
154+
const auto it = session_spec_to_transfer_id_.find(session_spec);
155+
return it != session_spec_to_transfer_id_.end() ? it->second : 0;
156+
}
157+
158+
void setIdFor(const SessionSpec& session_spec, const TransferId transfer_id) noexcept override
159+
{
160+
session_spec_to_transfer_id_[session_spec] = transfer_id;
161+
}
162+
163+
PmrMap session_spec_to_transfer_id_;
164+
165+
}; // TransferIdMap
166+
134167
void PrintUniqueIdTo(const std::array<std::uint8_t, 16>& unique_id, std::ostream& os)
135168
{
136169
const auto original_flags = os.flags();
@@ -169,6 +202,7 @@ libcyphal::Expected<bool, ExitCode> run_application(const char* const root_path)
169202
std::cerr << "❌ Failed to create any transport.\n";
170203
return ExitCode::TransportCreationFailure;
171204
}
205+
TransferIdMap transfer_id_map{general_mr};
172206

173207
// 2. Create the presentation layer object.
174208
//
@@ -179,7 +213,9 @@ libcyphal::Expected<bool, ExitCode> run_application(const char* const root_path)
179213
std::cout << "Unique-ID : ";
180214
PrintUniqueIdTo(unique_id, std::cout);
181215
std::cout << "\n";
216+
//
182217
libcyphal::presentation::Presentation presentation{general_mr, executor, *transport_iface};
218+
presentation.setTransferIdMap(&transfer_id_map);
183219

184220
// 3. Create the node object with name.
185221
//

0 commit comments

Comments
 (0)