Line data Source code
1 : /**
2 : * @file group_metadata.cpp GroupMetadata class header, used to store the metadata of a group of transfers metadata (one
3 : * uploader to multiple downloaders)
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/group_metadata.hpp"
11 :
12 : #include <filesystem>
13 : #include <iostream>
14 : #include <stdexcept>
15 : #include <string>
16 : #include <utility>
17 : #include <vector>
18 :
19 : namespace dunedaq::snbmodules {
20 :
21 : const std::string GroupMetadata::m_file_extension = ".gmetadata"; // NOLINT
22 :
23 : TransferMetadata&
24 0 : GroupMetadata::get_transfer_meta_from_file_path(const std::string& file_path)
25 : {
26 0 : for (std::shared_ptr<TransferMetadata> meta : get_transfers_meta()) {
27 0 : if (meta->get_file_path() == file_path) {
28 0 : return *meta;
29 : }
30 0 : }
31 0 : ers::fatal(MetadataNotFoundInGroupError(ERS_HERE, m_group_id, file_path));
32 0 : return *m_transfers_meta[0]; // To avoid warning
33 : }
34 :
35 : TransferMetadata&
36 0 : GroupMetadata::add_file(std::shared_ptr<TransferMetadata> meta)
37 : {
38 0 : if (m_expected_files.find(meta->get_file_path()) != m_expected_files.end()) {
39 0 : m_expected_files.erase(meta->get_file_path());
40 0 : meta->set_group_id(m_group_id);
41 :
42 0 : return *m_transfers_meta.emplace_back(meta);
43 0 : } else if (meta->get_group_id() == m_group_id) {
44 0 : int pos = -1;
45 0 : int i = 0;
46 0 : for (const auto& m : m_transfers_meta) {
47 0 : if (m == meta) {
48 : // Already inserted, update the transfer
49 : pos = i;
50 : break;
51 : }
52 0 : i++;
53 : }
54 0 : if (pos != -1) {
55 0 : m_transfers_meta.erase(m_transfers_meta.begin() + pos);
56 : }
57 0 : return *m_transfers_meta.emplace_back(std::move(meta));
58 : } else {
59 0 : ers::fatal(MetadataNotExpectedInGroupError(ERS_HERE, m_group_id, meta->get_file_name()));
60 0 : return *m_transfers_meta[0]; // To avoid warning
61 : }
62 : }
63 :
64 : std::string
65 0 : GroupMetadata::export_to_string()
66 : {
67 0 : nlohmann::json j;
68 0 : j["transfer_id"] = get_group_id();
69 0 : j["source_id"] = get_source_id();
70 0 : j["source_ip"] = get_source_ip().get_ip_port();
71 0 : j["protocol"] = protocol_type::protocols_to_string(get_protocol());
72 0 : j["protocol_options"] = get_protocol_options().dump();
73 :
74 0 : std::vector<std::string> files;
75 0 : for (const auto& file : get_transfers_meta()) {
76 0 : files.push_back(file->get_file_path().string());
77 : }
78 0 : j["files"] = files;
79 :
80 0 : return j.dump();
81 0 : }
82 :
83 : void
84 0 : GroupMetadata::from_string(const std::string& str)
85 : {
86 0 : nlohmann::json j = nlohmann::json::parse(str);
87 :
88 0 : TLOG() << "debug : Loading metadata from string " << str;
89 :
90 0 : if (j.contains("transfer_id")) {
91 0 : set_group_id(j["transfer_id"].get<std::string>());
92 : }
93 0 : if (j.contains("source_id")) {
94 0 : set_source_id(j["source_id"].get<std::string>());
95 : }
96 0 : if (j.contains("source_ip")) {
97 0 : m_source_ip = IPFormat(j["source_ip"].get<std::string>());
98 : }
99 0 : if (j.contains("protocol")) {
100 0 : set_protocol(protocol_type::string_to_protocols(j["protocol"].get<std::string>()).value());
101 : }
102 0 : if (j.contains("protocol_options")) {
103 0 : set_protocol_options(nlohmann::json::parse(j["protocol_options"].get<std::string>()));
104 : }
105 0 : if (j.contains("files")) {
106 0 : auto files = j["files"].get<std::vector<std::filesystem::path>>();
107 :
108 0 : for (const auto& file : files) {
109 0 : add_expected_file(file);
110 : }
111 0 : }
112 0 : }
113 :
114 : void
115 0 : GroupMetadata::generate_metadata_file(std::filesystem::path dest)
116 : {
117 0 : std::ofstream metadata_file;
118 0 : metadata_file.open(dest.append(get_group_id()).string() + m_file_extension);
119 :
120 0 : metadata_file << export_to_string();
121 :
122 0 : metadata_file.close();
123 0 : }
124 :
125 : void
126 0 : GroupMetadata::load_metadata_from_meta_file(std::filesystem::path src)
127 : {
128 0 : std::ifstream metadata_file;
129 0 : try {
130 0 : metadata_file.open(src.string());
131 0 : } catch (const std::exception& e) {
132 0 : ers::error(MetadataFileNotFoundError(ERS_HERE, e.what()));
133 0 : return;
134 0 : }
135 :
136 0 : std::stringstream buffer;
137 0 : buffer << metadata_file.rdbuf();
138 :
139 0 : from_string(buffer.str());
140 :
141 0 : metadata_file.close();
142 0 : }
143 :
144 : std::string
145 0 : GroupMetadata::to_string() const
146 : {
147 0 : std::string str;
148 0 : str += "transfer_id " + get_group_id() + " ";
149 0 : str += "protocol " + protocol_type::protocols_to_string(get_protocol()) + "\n";
150 :
151 0 : for (const auto& file : get_transfers_meta()) {
152 0 : str += "*file " + file->get_file_name() + "\n";
153 : }
154 0 : for (const auto& file : get_expected_files()) {
155 0 : str += "*expectedfile " + file + "\n";
156 : }
157 0 : return str;
158 0 : }
159 : } // namespace dunedaq::snbmodules
|