Line data Source code
1 : /**
2 : * @file transfer_metadata.cpp TransferMetadata class, used to store metadata of a transfer (one uploader to one
3 : * downloader)
4 : *
5 : * This is part of the DUNE DAQ , copyright 2020.
6 : * Licensing/copyright details are in the COPYING file that you should have
7 : * received with this code.
8 : */
9 :
10 : #include "snbmodules/transfer_metadata.hpp"
11 :
12 : #include <string>
13 :
14 : namespace dunedaq::snbmodules {
15 : const std::string TransferMetadata::m_file_extension = ".tmetadata"; // NOLINT
16 :
17 : std::string
18 0 : TransferMetadata::export_to_string_partial(bool force_all)
19 : {
20 0 : nlohmann::json j;
21 0 : j["file_path"] = get_file_path().string();
22 0 : j["source"] = get_src().get_ip_port();
23 0 : j["destination"] = get_dest().get_ip_port();
24 0 : j["group_id"] = get_group_id();
25 :
26 : // not mandatory : to be refreshed when needed
27 0 : if (force_all || m_modified_fields["hash"] == true) {
28 0 : j["hash"] = get_hash();
29 : }
30 0 : if (force_all || m_modified_fields["size"] == true) {
31 0 : j["size"] = get_size();
32 : }
33 0 : if (force_all || m_modified_fields["bytes_transferred"] == true) {
34 0 : j["transfered"] = get_bytes_transferred();
35 : }
36 0 : if (force_all || m_modified_fields["transmission_speed"] == true) {
37 0 : j["speed"] = get_transmission_speed();
38 : }
39 0 : if (force_all || m_modified_fields["status"] == true) {
40 0 : j["status"] = status_type::status_to_string(get_status());
41 : }
42 0 : if (force_all || m_modified_fields["magnet_link"] == true) {
43 0 : j["magnet_link"] = get_magnet_link();
44 : }
45 0 : if (force_all || m_modified_fields["error_code"] == true) {
46 0 : j["error_code"] = get_error_code();
47 : }
48 0 : if (force_all || m_modified_fields["start_time"] == true) {
49 0 : j["start_t"] = get_start_time();
50 : }
51 0 : if (force_all || m_modified_fields["end_time"] == true) {
52 0 : j["end_t"] = get_end_time();
53 : }
54 0 : if (force_all || m_modified_fields["duration"] == true) {
55 0 : j["duration"] = m_duration;
56 : }
57 :
58 0 : m_modified_fields.clear();
59 :
60 0 : return j.dump();
61 0 : }
62 :
63 : void
64 0 : TransferMetadata::from_string(const std::string& str)
65 : {
66 0 : nlohmann::json j = nlohmann::json::parse(str);
67 :
68 0 : TLOG() << "debug : Loading metadata from string " << str;
69 :
70 0 : if (j.contains("file_path")) {
71 0 : set_file_path(j["file_path"].get<std::filesystem::path>());
72 : }
73 0 : if (j.contains("hash")) {
74 0 : set_hash(j["hash"].get<std::string>());
75 : }
76 0 : if (j.contains("size")) {
77 0 : set_size(j["size"].get<uint64_t>()); // NOLINT
78 : }
79 0 : if (j.contains("transfered")) {
80 0 : set_bytes_transferred(j["transfered"].get<uint64_t>()); // NOLINT
81 : }
82 0 : if (j.contains("speed")) {
83 0 : set_transmission_speed(j["speed"].get<int32_t>());
84 : }
85 0 : if (j.contains("source")) {
86 0 : set_src(IPFormat(j["source"].get<std::string>()));
87 : }
88 0 : if (j.contains("destination")) {
89 0 : set_dest(IPFormat(j["destination"].get<std::string>()));
90 : }
91 0 : if (j.contains("status")) {
92 0 : auto status = status_type::string_to_status(j["status"].get<std::string>());
93 0 : if (status.has_value()) {
94 0 : set_status(status.value());
95 : } else {
96 0 : ers::error(InvalidProtocolError(ERS_HERE, "reading metadata string process", j["status"].get<std::string>()));
97 : }
98 : }
99 0 : if (j.contains("magnet_link")) {
100 0 : set_magnet_link(j["magnet_link"].get<std::string>());
101 : }
102 0 : if (j.contains("group_id")) {
103 0 : set_group_id(j["group_id"].get<std::string>());
104 : }
105 0 : if (j.contains("error_code")) {
106 0 : set_error_code(j["error_code"].get<std::string>());
107 : }
108 0 : if (j.contains("start_t")) {
109 0 : set_start_time(j["start_t"].get<int64_t>());
110 : }
111 0 : if (j.contains("end_t")) {
112 0 : set_end_time(j["end_t"].get<int64_t>());
113 : }
114 0 : if (j.contains("duration")) {
115 0 : set_duration(j["duration"].get<int64_t>());
116 : }
117 0 : }
118 :
119 : void
120 0 : TransferMetadata::generate_metadata_file(std::filesystem::path dest)
121 : {
122 0 : std::ofstream metadata_file;
123 0 : metadata_file.open(dest.append(get_file_name()).string() + TransferMetadata::m_file_extension);
124 :
125 0 : metadata_file << export_to_string();
126 :
127 0 : metadata_file.close();
128 0 : }
129 :
130 : void
131 0 : TransferMetadata::load_metadata_from_meta_file(std::filesystem::path src)
132 : {
133 0 : std::ifstream metadata_file;
134 0 : try {
135 0 : metadata_file.open(src.string());
136 0 : } catch (const std::exception& e) {
137 0 : ers::error(MetadataFileNotFoundError(ERS_HERE, e.what()));
138 0 : return;
139 0 : }
140 :
141 0 : std::stringstream buffer;
142 0 : buffer << metadata_file.rdbuf();
143 :
144 0 : from_string(buffer.str());
145 :
146 0 : metadata_file.close();
147 0 : }
148 : } // namespace dunedaq::snbmodules
|