MADARA  3.4.1
FileRequester.h
Go to the documentation of this file.
1 #ifndef _MADARA_KNOWLEDGE_FILE_REQUESTER_H_
2 #define _MADARA_KNOWLEDGE_FILE_REQUESTER_H_
3 
4 #include <string>
5 #include <fstream>
6 #include <vector>
7 #include <stdio.h>
10 #include "madara/utility/Utility.h"
14 
15 namespace madara
16 {
17 namespace knowledge
18 {
25 {
26 public:
31 
43  FileRequester(const std::string& prefix, const std::string& sync_key,
44  const std::string& filename, KnowledgeBase kb,
45  int max_request_fragments = -1)
46  {
47  init(prefix, sync_key, filename, kb, max_request_fragments);
48  }
49 
61  inline void init(const std::string& prefix, const std::string& sync_key,
62  const std::string& filename, KnowledgeBase kb,
63  int max_request_fragments = -1)
64  {
65  // set the sync key
66  sync_.set_name(sync_key, kb);
67 
68  // records that contain the file fragments
69  containers::FlexMap file_space;
70 
71  // setup containers
72  file_space.set_name(prefix, kb);
73  file_space["size"].to_container(file_size_);
74  file_space["crc"].to_container(file_crc_);
75 
76  // set the filename
77  filename_ = filename;
78 
79  // set the max fragments
80  max_fragments = max_request_fragments;
81 
82  kb_ = kb;
83  }
84 
89  inline uint32_t get_crc(void)
90  {
91  return (uint32_t)file_crc_.to_integer();
92  }
93 
98  inline size_t get_size(void)
99  {
100  return (size_t)file_size_.to_integer();
101  }
102 
108  {
109  return filename_;
110  }
111 
116  inline double get_percent_complete(void)
117  {
118  double result = 0;
119 
120  size_t expected_size = get_size();
121  size_t num_fragments = expected_size / 60000;
122 
123  if (expected_size % 60000 > 0)
124  {
125  ++num_fragments;
126  }
127 
128  if (num_fragments > 0)
129  {
130  result = (double)utility::get_file_progress(
132  (double)get_size();
133 
134  result *= 100;
135  }
136 
137  return result;
138  }
139 
144  inline std::vector<int64_t> build_fragment_request(void)
145  {
148  }
149 
155  inline bool needs_request(void)
156  {
157  if (get_crc() != 0 && get_size() != 0)
158  {
159  std::vector<int64_t> fragments = build_fragment_request();
160  sync_.set(fragments);
161 
162  return fragments.size() != 0;
163  }
164  else
165  {
166  std::vector<int64_t> fragments;
167 
168  if (max_fragments > 0)
169  {
170  fragments.resize(max_fragments);
171 
172  for (size_t i = 0; i < fragments.size(); ++i)
173  {
174  fragments[i] = i;
175  }
176  }
177 
178  sync_.set(fragments);
179 
180  return true;
181  }
182  }
183 
187  inline void clear_fragments(void)
188  {
189  std::string str_crc = std::to_string((unsigned long)get_crc());
190  std::string frag_suffix = "." + str_crc + ".frag";
191  std::string frag_file = filename_ + ".0" + frag_suffix;
192 
193  size_t expected_size = get_size();
194  size_t num_fragments = expected_size / 60000;
195 
196  if (expected_size % 60000 > 0)
197  {
198  ++num_fragments;
199  }
200 
201  for (int i = 0; i < (int)num_fragments;
202  ++i, frag_file = filename_ + "." + std::to_string(i) + frag_suffix)
203  {
204  if (utility::file_exists(frag_file))
205  {
207  "FileRequester::clear_fragments: removing %s\n", frag_file.c_str());
208 
209  remove(frag_file.c_str());
210  }
211  }
212  }
213 
217  inline void modify(void)
218  {
219  sync_.modify();
220  }
221 
224 
225 private:
230 
233 
236 
239 
242 };
243 }
244 }
245 
246 #endif // _MADARA_KNOWLEDGE_FILE_REQUESTER_H_
#define madara_logger_ptr_log(loggering, level,...)
Fast version of the madara::logger::log method for Logger pointers.
Definition: Logger.h:41
A helper class that can reconstruct files or request files be transferred in fragments from a Madara ...
Definition: FileRequester.h:25
void modify(void)
Remodifies the file size and crc.
knowledge::KnowledgeBase kb_
saves the kb for general usage
containers::Integer file_crc_
the crc of the file
FileRequester(const std::string &prefix, const std::string &sync_key, const std::string &filename, KnowledgeBase kb, int max_request_fragments=-1)
Constructor.
Definition: FileRequester.h:43
size_t get_size(void)
Returns the size of the file.
Definition: FileRequester.h:98
containers::Integer file_size_
the size of the file contents
containers::NativeIntegerVector sync_
The key that is being synced to.
std::string filename_
the name of the file on the hard drive being reconstructed
uint32_t get_crc(void)
Returns the crc of the file.
Definition: FileRequester.h:89
std::vector< int64_t > build_fragment_request(void)
Builds fragment request to send.
std::string get_filename(void)
Returns the name of the file.
void clear_fragments(void)
Clears any lingering file fragments.
double get_percent_complete(void)
Returns the percentage of transfer that is completed.
bool needs_request(void)
Builds fragment request to send.
int max_fragments
the maximum fragments allowed in a resend request
void init(const std::string &prefix, const std::string &sync_key, const std::string &filename, KnowledgeBase kb, int max_request_fragments=-1)
Initializes the requester.
Definition: FileRequester.h:61
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
This class stores a flexible map of strings and ints to KaRL variables FlexMap differs from Map in th...
Definition: FlexMap.h:54
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: FlexMap.cpp:573
void to_container(BufferVector &target) const
Fills a BufferVector container with all subkeys.
Definition: FlexMap.cpp:280
This class stores an integer within a variable context.
Definition: Integer.h:32
knowledge::KnowledgeRecord::Integer to_integer(void) const
Returns the value as an integer (same as *)
Definition: Integer.inl:282
This class stores a vector of doubles inside of KaRL.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
void modify(void)
Mark the value as modified.
int set(size_t index, type value)
Sets a knowledge variable to a specified value.
constexpr string_t string
Provides functions and classes for the distributed knowledge base.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
bool file_exists(const std::string &filename)
Checks if a file exists.
Definition: Utility.inl:304
MADARA_EXPORT size_t get_file_progress(const std::string &filename, uint32_t crc, size_t expected_size, size_t fragment_size=60000)
Builds a file from fragments that have the format: filename.
Definition: Utility.inl:528
MADARA_EXPORT std::vector< int64_t > get_file_missing_fragments(const std::string &filename, uint32_t crc, size_t expected_size, int max_fragments=-1, size_t fragment_size=60000)
Attempts to builds a file from fragments that have the format: filename.
Definition: Utility.inl:572
Copyright(c) 2020 Galois.