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