MADARA  3.4.1
CheckpointPlayer.h
Go to the documentation of this file.
1 #ifndef MADARA_KNOWLEDGE_CHECKPOINT_PLAYER_H
2 #define MADARA_KNOWLEDGE_CHECKPOINT_PLAYER_H
3 
4 #include <fstream>
5 #include <thread>
6 #include <memory>
7 
8 #include "madara/MadaraExport.h"
14 
22 namespace madara
23 {
24 namespace knowledge
25 {
29 class MADARA_EXPORT CheckpointReader
30 {
31 public:
36  CheckpointReader(CheckpointSettings& in_checkpoint_settings)
37  : checkpoint_settings(in_checkpoint_settings),
38  logger_(logger::global_logger.get())
39  {
40  }
41 
48  void start();
49 
54  std::pair<std::string, KnowledgeRecord> next();
55 
59  int64_t get_total_read() const
60  {
61  return total_read;
62  }
63 
68  const FileHeader* get_file_header() const
69  {
70  if (stage == 0)
71  {
72  return nullptr;
73  }
74 
75  return &meta;
76  }
77 
82  bool is_open() const
83  {
84  return file.is_open();
85  }
86 
91  {
92  return checkpoint_settings;
93  }
94 
95 private:
97 
99  int stage = 0;
100  std::ifstream file;
101  int64_t total_read = 0;
103  int64_t max_buffer;
106  char* current;
108  uint64_t state;
109  uint64_t checkpoint_size;
111  uint64_t update;
112 };
113 
117 class MADARA_EXPORT CheckpointPlayer
118 {
119 public:
129  KnowledgeUpdateSettings update_settings = {})
130  : context_(&context),
131  settings_(std::move(settings)),
132  update_settings_(update_settings)
133  {
134  }
135 
145  KnowledgeUpdateSettings update_settings = {})
146  : context_(&context),
147  settings_(reader.get_checkpoint_settings()),
148  update_settings_(update_settings),
149  reader_(utility::mk_unique<CheckpointReader>(std::move(reader)))
150  {
151  }
152 
154  {
155  stop();
156  }
157 
158  // This object spawns a thread which holds a pointer back to this object,
159  // so it cannot be safely copied or moved.
164 
171  void start()
172  {
173  keep_running_.test_and_set();
174  init_reader();
175  thread_ = std::thread(thread_main, this);
176  }
177 
181  void stop()
182  {
183  keep_running_.clear();
184  if (thread_.joinable())
185  {
186  thread_.join();
187  }
188  reader_.reset();
189  }
190 
198  bool play_until(uint64_t target_toi);
199 
200 private:
201  static void thread_main(CheckpointPlayer* self);
202 
203  void init_reader()
204  {
205  if (!reader_)
206  {
207  reader_ = utility::mk_unique<CheckpointReader>(settings_);
208  }
209  }
210 
214 
215  std::atomic_flag keep_running_;
216  std::thread thread_;
217  std::unique_ptr<CheckpointReader> reader_;
218 };
219 }
220 } // namespace madara::knowledge
221 
222 #endif // MADARA_KNOWLEDGE_CHECKPOINT_PLAYER_H
const ThreadSafeContext * context_
Plays back a checkpoint over time, based on recorded TOI.
void stop()
Stop playback, killing the playback thread.
CheckpointPlayer & operator=(CheckpointPlayer &&)=delete
KnowledgeUpdateSettings update_settings_
std::unique_ptr< CheckpointReader > reader_
CheckpointPlayer(const CheckpointPlayer &)=delete
CheckpointPlayer(ThreadSafeContext &context, CheckpointReader reader, KnowledgeUpdateSettings update_settings={})
Constructor from CheckpointReader.
CheckpointPlayer & operator=(const CheckpointPlayer &)=delete
CheckpointPlayer(CheckpointPlayer &&)=delete
CheckpointPlayer(ThreadSafeContext &context, CheckpointSettings settings, KnowledgeUpdateSettings update_settings={})
Constructor.
Class for iterating binary checkpoint files.
const FileHeader * get_file_header() const
Get the file header information.
CheckpointSettings & checkpoint_settings
transport::MessageHeader checkpoint_header
const CheckpointSettings & get_checkpoint_settings() const
Returns CheckpointSettings this reader is using.
CheckpointReader(CheckpointSettings &in_checkpoint_settings)
Construct using the given CheckpointSettings.
bool is_open() const
Check if underlying file is open.
utility::ScopedArray< char > buffer
int64_t get_total_read() const
Get total number of bytes read so far during iteration.
Holds settings for checkpoints to load or save.
Defines a file header which is the default for KaRL checkpointing.
Definition: FileHeader.h:37
Settings for applying knowledge updates.
This class stores variables and their values for use by any entity needing state information in a thr...
A multi-threaded logger for logging to one or more destinations.
Definition: Logger.h:165
Defines a robust message header which is the default for KaRL messages.
Definition: MessageHeader.h:57
Provides functions and classes for the distributed knowledge base.
T get(const KnowledgeRecord &kr)
Get the value of a KnowlegeRecord.
Definition: GetRecord.h:121
Provides knowledge logging services to files and terminals.
Definition: GlobalLogger.h:12
MADARA_EXPORT utility::Refcounter< logger::Logger > global_logger
Copyright(c) 2020 Galois.