Skip to content

Commit

Permalink
applied transfer id map
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Jan 31, 2025
1 parent f4ca418 commit 0fccc4f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
1 change: 1 addition & 0 deletions libcyphal_demo/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "application.hpp"

#include <libcyphal/application/registry/registry_impl.hpp>
#include <libcyphal/platform/storage.hpp>

#include <cetl/pf17/cetlpf.hpp>
Expand Down
8 changes: 5 additions & 3 deletions libcyphal_demo/src/file_downloader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include <chrono>
#include <cstddef>
#include <iomanip>
#include <iostream>
#include <utility>

Expand Down Expand Up @@ -74,6 +75,7 @@ class FileDownloader final
}

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

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

void complete()
{
const auto duration = time_provider_.now() - file_stats_.start_time;
const auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
const auto duration = time_provider_.now() - file_stats_.start_time;
std::cout << "\nDownload completed (err=" << file_stats_.file_error.value //
<< ", time=" << duration_ms << "ms).\n"
<< ", time=" << std::fixed << std::setprecision(6) // NOLINT
<< std::chrono::duration_cast<std::chrono::duration<double>>(duration).count() << "s).\n"
<< std::flush;

get_info_promise_.reset();
Expand Down
36 changes: 36 additions & 0 deletions libcyphal_demo/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <libcyphal/application/node.hpp>
#include <libcyphal/presentation/presentation.hpp>
#include <libcyphal/time_provider.hpp>
#include <libcyphal/transport/transfer_id_map.hpp>
#include <libcyphal/transport/transport.hpp>
#include <libcyphal/transport/types.hpp>
#include <libcyphal/types.hpp>
Expand All @@ -24,10 +25,12 @@
#include <algorithm>
#include <array>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <ios>
#include <iostream>
#include <unistd.h>
#include <unordered_map>
#include <utility>

using namespace std::chrono_literals;
Expand Down Expand Up @@ -131,6 +134,36 @@ enum class ExitCode : std::uint8_t

}; // ExitCode

class TransferIdMap final : public libcyphal::transport::ITransferIdMap
{
public:
explicit TransferIdMap(cetl::pmr::memory_resource& memory) noexcept
: session_spec_to_transfer_id_{&memory}
{
}

private:
using TransferId = libcyphal::transport::TransferId;
using PmvAlloc = cetl::pmr::polymorphic_allocator<std::pair<const SessionSpec, TransferId>>;
using PmrMap = std::unordered_map<SessionSpec, TransferId, std::hash<SessionSpec>, std::equal_to<>, PmvAlloc>;

// ITransferIdMap

TransferId getIdFor(const SessionSpec& session_spec) const noexcept override
{
const auto it = session_spec_to_transfer_id_.find(session_spec);
return it != session_spec_to_transfer_id_.end() ? it->second : 0;
}

void setIdFor(const SessionSpec& session_spec, const TransferId transfer_id) noexcept override
{
session_spec_to_transfer_id_[session_spec] = transfer_id;
}

PmrMap session_spec_to_transfer_id_;

}; // TransferIdMap

void PrintUniqueIdTo(const std::array<std::uint8_t, 16>& unique_id, std::ostream& os)
{
const auto original_flags = os.flags();
Expand Down Expand Up @@ -169,6 +202,7 @@ libcyphal::Expected<bool, ExitCode> run_application(const char* const root_path)
std::cerr << "❌ Failed to create any transport.\n";
return ExitCode::TransportCreationFailure;
}
TransferIdMap transfer_id_map{general_mr};

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

// 3. Create the node object with name.
//
Expand Down

0 comments on commit 0fccc4f

Please sign in to comment.