Refactoring the WritingManager

This commit is contained in:
2019-04-02 11:14:42 +02:00
parent e6d4f0fa04
commit 2a7ef227fb
2 changed files with 33 additions and 38 deletions
+11 -27
View File
@@ -49,12 +49,13 @@ void writer_utils::create_destination_folder(const string& output_file)
}
WriterManager::WriterManager(const unordered_map<string, DATA_TYPE>& parameters_type):
parameters_type(parameters_type), running_flag(true), killed_flag(false),
n_received_frames(0), n_written_frames(0), n_lost_frames(0)
parameters_type(parameters_type), logs(10),
receiving_flag(false), writing_flag(false), killed_flag(false),
n_received_frames(0), n_written_frames(0), n_expected_frames(0)
{
#ifdef DEBUG_OUTPUT
using namespace date;
cout << "[" << std::chrono::system_clock::now() << "]";
cout << "[" << std::chrono::system_clock::now() <<
cout << "[WriterManager::WriterManager] Writer manager initialized." << endl;
#endif
}
@@ -87,14 +88,12 @@ void WriterManager::kill()
string WriterManager::get_status()
{
if (running_flag) {
return "receiving";
} else if (n_received_frames.load() > n_written_frames) {
return "writing";
} else if (!are_all_parameters_set()) {
return "waiting for parameters";
if (writing_flag) {
return "writing"
} else if (receiving_flag) {
return "receiving"
} else {
return "finished";
return "ready";
}
}
@@ -102,22 +101,18 @@ unordered_map<string, uint64_t> WriterManager::get_statistics() const
{
unordered_map<string, uint64_t> result = {{"n_received_frames", n_received_frames.load()},
{"n_written_frames", n_written_frames.load()},
{"n_lost_frames", n_lost_frames.load()},
{"total_expected_frames", n_frames}};
{"n_lost_frames", n_lost_frames.load()}};
return result;
}
unordered_map<string, boost::any> WriterManager::get_parameters()
{
lock_guard<mutex> lock(parameters_mutex);
return parameters;
}
void WriterManager::set_parameters(const unordered_map<string, boost::any>& new_parameters)
void WriterManager::start(const unordered_map<string, boost::any>& new_parameters)
{
lock_guard<mutex> lock(parameters_mutex);
#ifdef DEBUG_OUTPUT
stringstream output_message;
@@ -149,11 +144,6 @@ const unordered_map<string, DATA_TYPE>& WriterManager::get_parameters_type() con
bool WriterManager::is_running()
{
// Take into account n_frames only if it is <> 0.
if (n_frames && n_received_frames.load() >= n_frames) {
running_flag = false;
}
return running_flag.load();
}
@@ -179,8 +169,6 @@ void WriterManager::lost_frame(size_t frame_index)
bool WriterManager::are_all_parameters_set()
{
lock_guard<mutex> lock(parameters_mutex);
for (const auto& parameter : parameters_type) {
const auto& parameter_name = parameter.first;
@@ -198,7 +186,3 @@ bool WriterManager::are_all_parameters_set()
return true;
}
size_t WriterManager::get_n_frames()
{
return n_frames;
}
+22 -11
View File
@@ -8,7 +8,7 @@
#include <boost/any.hpp>
#include <chrono>
#include "date.h"
#include <deque>
#include "H5Format.hpp"
namespace writer_utils {
@@ -16,38 +16,49 @@ namespace writer_utils {
void create_destination_folder(const std::string& output_file);
}
struct WriterManagerLog
{
std::string filename;
uint64_t n_requested_frames;
uint64_t n_received_frames;
uint64_t n_written_frames;
};
class WriterManager
{
std::unordered_map<std::string, boost::any> parameters = {};
// Initialize in constructor.
const std::unordered_map<std::string, DATA_TYPE>& parameters_type;
std::atomic_bool running_flag;
std::atomic_bool killed_flag;
const std::deque<WriterManagerLog> logs;
std::atomic<bool> receiving_flag;
std::atomic<bool> writing_flag;
std::atomic<bool> killed_flag;
std::atomic<uint64_t> n_received_frames;
std::atomic<uint64_t> n_written_frames;
std::atomic<uint64_t> n_lost_frames;
std::atomic<int64_t> n_expected_frames;
public:
WriterManager(const std::unordered_map<std::string, DATA_TYPE>& parameters_type);
virtual ~WriterManager();
void start(const std::unordered_map<std::string, boost:any>& parameters);
const std::unordered_map<std::string, DATA_TYPE>& get_parameters_type() const;
void start(const std::unordered_map<std::string, boost::any>& new_parameters);
void stop();
void kill();
std::string get_status();
std::unordered_map<std::string, uint64_t> get_statistics() const;
std::unordered_map<std::string, boost::any> get_parameters();
const std::unordered_map<std::string, DATA_TYPE>& get_parameters_type() const;
bool is_running();
bool is_killed() const;
bool are_all_parameters_set();
std::string get_output_file() const;
std::unordered_map<std::string, boost::any> get_parameters();
void received_frame(size_t frame_index);
void written_frame(size_t frame_index);
void lost_frame(size_t frame_index);