DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
transfer_metadata.cpp
Go to the documentation of this file.
1
11
12#include <string>
13
14namespace dunedaq::snbmodules {
15const std::string TransferMetadata::m_file_extension = ".tmetadata"; // NOLINT
16
17std::string
19{
20 nlohmann::json j;
21 j["file_path"] = get_file_path().string();
22 j["source"] = get_src().get_ip_port();
23 j["destination"] = get_dest().get_ip_port();
24 j["group_id"] = get_group_id();
25
26 // not mandatory : to be refreshed when needed
27 if (force_all || m_modified_fields["hash"] == true) {
28 j["hash"] = get_hash();
29 }
30 if (force_all || m_modified_fields["size"] == true) {
31 j["size"] = get_size();
32 }
33 if (force_all || m_modified_fields["bytes_transferred"] == true) {
34 j["transfered"] = get_bytes_transferred();
35 }
36 if (force_all || m_modified_fields["transmission_speed"] == true) {
37 j["speed"] = get_transmission_speed();
38 }
39 if (force_all || m_modified_fields["status"] == true) {
41 }
42 if (force_all || m_modified_fields["magnet_link"] == true) {
43 j["magnet_link"] = get_magnet_link();
44 }
45 if (force_all || m_modified_fields["error_code"] == true) {
46 j["error_code"] = get_error_code();
47 }
48 if (force_all || m_modified_fields["start_time"] == true) {
49 j["start_t"] = get_start_time();
50 }
51 if (force_all || m_modified_fields["end_time"] == true) {
52 j["end_t"] = get_end_time();
53 }
54 if (force_all || m_modified_fields["duration"] == true) {
55 j["duration"] = m_duration;
56 }
57
58 m_modified_fields.clear();
59
60 return j.dump();
61}
62
63void
64TransferMetadata::from_string(const std::string& str)
65{
66 nlohmann::json j = nlohmann::json::parse(str);
67
68 TLOG() << "debug : Loading metadata from string " << str;
69
70 if (j.contains("file_path")) {
71 set_file_path(j["file_path"].get<std::filesystem::path>());
72 }
73 if (j.contains("hash")) {
74 set_hash(j["hash"].get<std::string>());
75 }
76 if (j.contains("size")) {
77 set_size(j["size"].get<uint64_t>()); // NOLINT
78 }
79 if (j.contains("transfered")) {
80 set_bytes_transferred(j["transfered"].get<uint64_t>()); // NOLINT
81 }
82 if (j.contains("speed")) {
83 set_transmission_speed(j["speed"].get<int32_t>());
84 }
85 if (j.contains("source")) {
86 set_src(IPFormat(j["source"].get<std::string>()));
87 }
88 if (j.contains("destination")) {
89 set_dest(IPFormat(j["destination"].get<std::string>()));
90 }
91 if (j.contains("status")) {
92 auto status = status_type::string_to_status(j["status"].get<std::string>());
93 if (status.has_value()) {
94 set_status(status.value());
95 } else {
96 ers::error(InvalidProtocolError(ERS_HERE, "reading metadata string process", j["status"].get<std::string>()));
97 }
98 }
99 if (j.contains("magnet_link")) {
100 set_magnet_link(j["magnet_link"].get<std::string>());
101 }
102 if (j.contains("group_id")) {
103 set_group_id(j["group_id"].get<std::string>());
104 }
105 if (j.contains("error_code")) {
106 set_error_code(j["error_code"].get<std::string>());
107 }
108 if (j.contains("start_t")) {
109 set_start_time(j["start_t"].get<int64_t>());
110 }
111 if (j.contains("end_t")) {
112 set_end_time(j["end_t"].get<int64_t>());
113 }
114 if (j.contains("duration")) {
115 set_duration(j["duration"].get<int64_t>());
116 }
117}
118
119void
121{
122 std::ofstream metadata_file;
123 metadata_file.open(dest.append(get_file_name()).string() + TransferMetadata::m_file_extension);
124
125 metadata_file << export_to_string();
126
127 metadata_file.close();
128}
129
130void
132{
133 std::ifstream metadata_file;
134 try {
135 metadata_file.open(src.string());
136 } catch (const std::exception& e) {
137 ers::error(MetadataFileNotFoundError(ERS_HERE, e.what()));
138 return;
139 }
140
141 std::stringstream buffer;
142 buffer << metadata_file.rdbuf();
143
144 from_string(buffer.str());
145
146 metadata_file.close();
147}
148} // namespace dunedaq::snbmodules
#define ERS_HERE
Class that represents an IP address and a port TODO: should be replaced by something better ?
Definition ip_format.hpp:26
std::string get_ip_port() const
Get the IP address and the port in the format "ip:port".
Definition ip_format.hpp:57
void set_error_code(std::string error_code)
status_type::e_status get_status() const
std::map< std::string, bool > m_modified_fields
Vector of modified fields in order : to send only the modified fields.
int64_t m_duration
Duration of the transfer.
virtual std::string export_to_string_partial(bool force_all)
Export only the modified fields to a string.
void from_string(const std::string &) override
Import metadata from string (json format)
std::filesystem::path get_file_path() const
void set_transmission_speed(int32_t transmission_speed)
void generate_metadata_file(std::filesystem::path dest) override
Generaete metadata file to dest.
void set_bytes_transferred(uint64_t bytes_transferred)
void set_magnet_link(std::string magnet_link)
std::string export_to_string() override
Export metadata to string (json format)
void set_file_path(const std::filesystem::path &file_path)
void set_status(status_type::e_status status)
void load_metadata_from_meta_file(std::filesystem::path src) override
Load metadata file from src.
#define TLOG(...)
Definition macro.hpp:22
void error(const Issue &issue)
Definition ers.hpp:81
static std::string status_to_string(e_status e)
static std::optional< e_status > string_to_status(std::string s)