DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
transfer_client.hpp
Go to the documentation of this file.
1
9#ifndef SNBMODULES_INCLUDE_SNBMODULES_TRANSFER_CLIENT_HPP_
10#define SNBMODULES_INCLUDE_SNBMODULES_TRANSFER_CLIENT_HPP_
11
14
17
18// errors handling
19#include "logging/Logging.hpp"
21
22#include <filesystem>
23#include <iostream>
24#include <map>
25#include <set>
26#include <string>
27#include <unistd.h>
28#include <utility>
29
30namespace dunedaq::snbmodules {
34{
35
36public:
43 TransferClient(const IPFormat& listening_ip,
44 const std::string& client_id,
45 const std::filesystem::path& listening_dir,
46 const std::string& connection_prefix = "snbmodules",
47 int timeout_send = 10,
48 int timeout_receive = 100);
49
51
55 bool start(int timeout);
56
60 bool do_work(std::atomic<bool>& running_flag);
61
68 void create_new_transfer(const std::string& transfer_id,
69 const std::string& protocol,
70 const std::set<std::string>& dest_clients,
71 const std::set<std::filesystem::path>& files,
72 const nlohmann::json& protocol_options = nlohmann::json());
73
76 void start_transfer(const std::string& transfer_id);
77 void pause_transfer(const std::string& transfer_id);
78 void resume_transfer(const std::string& transfer_id);
79 void cancel_transfer(const std::string& transfer_id);
80
89 e_session_type type,
90 std::string id,
91 const std::filesystem::path& work_dir,
92 IPFormat ip = IPFormat(),
93 const std::set<std::string>& dest_clients = std::set<std::string>());
94
99 void scan_available_files(std::set<std::filesystem::path>& previous_scan,
100 bool nested = false,
101 std::filesystem::path folder = std::filesystem::path());
102
103 // Getters
104 inline IPFormat get_ip() const { return m_listening_ip; }
105 inline std::string get_client_id() const { return m_client_id; }
106 inline std::filesystem::path get_listening_dir() const { return m_listening_dir; }
107 TransferSession* get_session(std::string transfer_id);
108 inline std::vector<TransferSession>& get_sessions() { return m_sessions; }
109 inline const std::vector<TransferSession>& get_sessions() const { return m_sessions; }
110 std::string get_my_conn();
111
112 // Setters
113 inline void set_ip(const std::string& ip) { m_listening_ip.set_ip(ip); }
114 inline void set_port(int port) { m_listening_ip.set_port(port); }
115 inline void set_client_id(std::string client_id) { m_client_id = std::move(client_id); }
116 inline void set_listening_dir(const std::filesystem::path& listening_dir)
117 {
118 // remove all occurences of ./ in the file path
119 std::string file_path_str = listening_dir.string();
120 std::string x = "./";
121
122 size_t pos = 0;
123 while (true) {
124 pos = file_path_str.find(x, pos);
125 if (pos == std::string::npos) {
126 break;
127 }
128
129 file_path_str.replace(pos, x.length(), "");
130 }
131
132 m_listening_dir = std::filesystem::absolute(file_path_str);
133 }
134
135private:
138
140 std::string m_client_id;
141
143 std::filesystem::path m_listening_dir;
144
146 std::vector<TransferSession> m_sessions;
147
149 std::map<std::string, std::shared_ptr<TransferMetadata>> m_available_files;
150
152 std::string m_my_conn = "";
153
155 std::string generate_session_id(const std::string& transferid, const std::string& dest = "");
156
159 // TransferSession &add_session(TransferSession session);
160
163 void remove_session(const std::string& session_id);
164
169
173 void share_available_files(const std::set<std::filesystem::path>& to_share, const std::string& dest);
174
178 std::shared_ptr<TransferMetadata> create_metadata_from_file(const std::filesystem::path& src);
179};
180} // namespace dunedaq::snbmodules
181#endif // SNBMODULES_INCLUDE_SNBMODULES_TRANSFER_CLIENT_HPP_
Class that represents an IP address and a port TODO: should be replaced by something better ?
Definition ip_format.hpp:26
void set_ip(const std::string &ip)
Set the IP address, must be called after set_port() if the port is specified in the IP address ex 0....
Definition ip_format.hpp:80
TransferClient class, represent a client that can create session and communicate with Bookkeepers....
const std::vector< TransferSession > & get_sessions() const
void create_new_transfer(const std::string &transfer_id, const std::string &protocol, const std::set< std::string > &dest_clients, const std::set< std::filesystem::path > &files, const nlohmann::json &protocol_options=nlohmann::json())
Create a new transfer.
std::string m_client_id
TransferClient ID, unique identifier of the client.
void cancel_transfer(const std::string &transfer_id)
TransferSession & create_session(GroupMetadata transfer_options, e_session_type type, std::string id, const std::filesystem::path &work_dir, IPFormat ip=IPFormat(), const std::set< std::string > &dest_clients=std::set< std::string >())
Create a new session, you can precise the IP address of the session forcing to use a specific network...
void start_transfer(const std::string &transfer_id)
Start, pause, resume or cancel a transfer.
TransferClient(const IPFormat &listening_ip, const std::string &client_id, const std::filesystem::path &listening_dir, const std::string &connection_prefix="snbmodules", int timeout_send=10, int timeout_receive=100)
TransferClient constructor.
TransferSession * get_session(std::string transfer_id)
std::filesystem::path m_listening_dir
Listening directory, directory where the client will listen for incoming files and files to share.
void set_ip(const std::string &ip)
void remove_session(const std::string &session_id)
Add a session to the client.
std::map< std::string, std::shared_ptr< TransferMetadata > > m_available_files
Map of available files (key = file path, value = file metadata)
std::filesystem::path get_listening_dir() const
void set_client_id(std::string client_id)
void scan_available_files(std::set< std::filesystem::path > &previous_scan, bool nested=false, std::filesystem::path folder=std::filesystem::path())
Scan available files in the listening directory.
bool start(int timeout)
Start the client in curent thread.
std::string m_my_conn
Connection uuid of the client, retrieved using the notification interface and calling get_my_conn()
std::vector< TransferSession > m_sessions
Map of active sessions (key = session ID, value = session)
void share_available_files(const std::set< std::filesystem::path > &to_share, const std::string &dest)
Share available files (in m_listening_dir)
void set_listening_dir(const std::filesystem::path &listening_dir)
void pause_transfer(const std::string &transfer_id)
std::shared_ptr< TransferMetadata > create_metadata_from_file(const std::filesystem::path &src)
Create a metadata from a file.
std::vector< TransferSession > & get_sessions()
std::string generate_session_id(const std::string &transferid, const std::string &dest="")
Function to generate session ID.
void resume_transfer(const std::string &transfer_id)
IPFormat m_listening_ip
IP address of the client.
bool do_work(std::atomic< bool > &running_flag)
Start function to use in a thread.
bool action_on_receive_notification(NotificationData notif) override
Action to do when the client receive a notification.
TransferSession class contained in a client, is a wrapper for a transfer. extend notification interfa...
e_session_type
Different type of session.
NotificationData class, represent a notification.