DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
transfer_interface_SCP.hpp
Go to the documentation of this file.
1
9#ifndef SNBMODULES_INCLUDE_SNBMODULES_INTERFACES_TRANSFER_INTERFACE_SCP_HPP_
10#define SNBMODULES_INCLUDE_SNBMODULES_INTERFACES_TRANSFER_INTERFACE_SCP_HPP_
11
14
15#include <chrono>
16#include <cstdio>
17#include <fstream>
18#include <iostream>
19
20#include <map>
21#include <string>
22
23namespace dunedaq::snbmodules {
24
26{
27
28public:
29 TransferInterfaceSCP(GroupMetadata& config, bool is_uploader)
31 {
32 if (config.get_protocol_options().contains("user")) {
33 m_params.user = config.get_protocol_options()["user"].get<std::string>();
34 } else {
35 ers::fatal(ConfigError(ERS_HERE, "user is mandatory in SCP protocol options"));
36 return;
37 }
38
39 if (config.get_protocol_options().contains("use_password")) {
40 m_params.use_password = config.get_protocol_options()["use_password"].get<bool>();
41 }
42
43 m_is_uploader = is_uploader;
44 }
45 virtual ~TransferInterfaceSCP() = default;
46
47 bool upload_file(TransferMetadata& f_meta) override
48 {
49 TLOG() << "debug : SCP : Uploading file " << f_meta.get_file_name();
51
52 // nothing to do
53 TLOG() << "debug : SCP : Sucess Upload";
55 f_meta.set_bytes_transferred(f_meta.get_size());
56 return true;
57 }
58 bool download_file(TransferMetadata& f_meta, std::filesystem::path dest) override
59 {
60 TLOG() << "debug : SCP : Downloading file " << f_meta.get_file_name();
62
64
65 std::string exec = "";
66 if (m_params.use_password) {
67 exec = "scp " + m_params.user + "@" + f_meta.get_src().get_ip() + ":" + f_meta.get_file_path().string() + " " +
68 dest.string();
69 } else {
70
71 exec = "scp -o PasswordAuthentication='no' " + m_params.user + "@" + f_meta.get_src().get_ip() + ":" +
72 f_meta.get_file_path().string() + " " + dest.string();
73 }
74
75 TLOG() << "debug : executing " << exec;
76 if (system(exec.c_str()) == 0) // NOLINT
77 {
78 TLOG() << "debug : SCP : Sucess Download";
80 f_meta.set_bytes_transferred(f_meta.get_size());
81 } else {
82 ers::error(ErrorSCPDownloadError(ERS_HERE, "Please check the logs for more information."));
84 f_meta.set_error_code("Something went wrong during the download");
85 f_meta.set_bytes_transferred(0);
86 return false;
87 }
88
89 return true;
90 }
91
92 bool pause_file(TransferMetadata& f_meta) override
93 {
94 TLOG() << "debug : SCP : Pausing file " << f_meta.get_file_name();
96 f_meta.set_bytes_transferred(0);
97 return true;
98 }
99
100 bool resume_file(TransferMetadata& f_meta) override
101 {
102 TLOG() << "debug : SCP : Resuming file " << f_meta.get_file_name();
103
104 if (m_is_uploader) {
105 return upload_file(f_meta);
106 } else {
107 return download_file(f_meta, m_files_being_transferred[f_meta.get_file_name()]);
108 }
109 }
110
111 bool hash_file(TransferMetadata& f_meta) override
112 {
113 TLOG() << "debug : SCP : Hashing file " << f_meta.get_file_name();
115 return true;
116 }
117
118 bool cancel_file(TransferMetadata& f_meta) override
119 {
120 TLOG() << "debug : SCP : Cancelling file " << f_meta.get_file_name();
122 return true;
123 }
124
125private:
127 {
128 std::string user;
129 bool use_password = false;
131
133 std::map<std::string, std::filesystem::path> m_files_being_transferred;
134};
135} // namespace dunedaq::snbmodules
136#endif // SNBMODULES_INCLUDE_SNBMODULES_INTERFACES_TRANSFER_INTERFACE_SCP_HPP_
#define ERS_HERE
nlohmann::json get_protocol_options() const
std::string get_ip() const
bool download_file(TransferMetadata &f_meta, std::filesystem::path dest) override
bool upload_file(TransferMetadata &f_meta) override
bool pause_file(TransferMetadata &f_meta) override
std::map< std::string, std::filesystem::path > m_files_being_transferred
bool cancel_file(TransferMetadata &f_meta) override
bool hash_file(TransferMetadata &f_meta) override
bool resume_file(TransferMetadata &f_meta) override
TransferInterfaceSCP(GroupMetadata &config, bool is_uploader)
struct dunedaq::snbmodules::TransferInterfaceSCP::scp_parameters m_params
void set_error_code(std::string error_code)
std::filesystem::path get_file_path() const
void set_bytes_transferred(uint64_t bytes_transferred)
void set_status(status_type::e_status status)
#define TLOG(...)
Definition macro.hpp:22
void error(const Issue &issue)
Definition ers.hpp:81