Reorder the repository

This commit is contained in:
2018-07-12 10:51:47 +02:00
parent 7115749da3
commit a481e67428
33 changed files with 2 additions and 2 deletions
+205
View File
@@ -0,0 +1,205 @@
#include <iostream>
#include <sstream>
#include "WriterManager.hpp"
using namespace std;
void writer_utils::set_process_id(int user_id)
{
#ifdef DEBUG_OUTPUT
using namespace date;
cout << "[" << std::chrono::system_clock::now() << "]";
cout << "[writer_utils::set_process_id] Setting process uid to " << user_id << endl;
#endif
if (setgid(user_id)) {
stringstream error_message;
using namespace date;
error_message << "[" << std::chrono::system_clock::now() << "]";
error_message << "[writer_utils::set_process_id] Cannot set group_id to " << user_id << endl;
throw runtime_error(error_message.str());
}
if (setuid(user_id)) {
stringstream error_message;
using namespace date;
error_message << "[" << std::chrono::system_clock::now() << "]";
error_message << "[writer_utils::set_process_id] Cannot set user_id to " << user_id << endl;
throw runtime_error(error_message.str());
}
}
void writer_utils::create_destination_folder(const string& output_file)
{
auto file_separator_index = output_file.rfind('/');
if (file_separator_index != string::npos) {
string output_folder(output_file.substr(0, file_separator_index));
using namespace date;
cout << "[" << std::chrono::system_clock::now() << "]";
cout << "[writer_utils::create_destination_folder] Creating folder " << output_folder << endl;
string create_folder_command("mkdir -p " + output_folder);
system(create_folder_command.c_str());
}
}
WriterManager::WriterManager(const unordered_map<string, DATA_TYPE>& parameters_type,
const string& output_file, uint64_t n_frames):
parameters_type(parameters_type), output_file(output_file), n_frames(n_frames),
running_flag(true), killed_flag(false), n_received_frames(0), n_written_frames(0), n_lost_frames(0)
{
#ifdef DEBUG_OUTPUT
using namespace date;
cout << "[" << std::chrono::system_clock::now() << "]";
cout << "[WriterManager::WriterManager] Writer manager for n_frames " << n_frames << endl;
#endif
}
WriterManager::~WriterManager(){}
void WriterManager::stop()
{
#ifdef DEBUG_OUTPUT
using namespace date;
cout << "[" << std::chrono::system_clock::now() << "]";
cout << "[WriterManager::stop] Stopping the writer manager." << endl;
#endif
running_flag = false;
}
void WriterManager::kill()
{
#ifdef DEBUG_OUTPUT
using namespace date;
cout << "[" << std::chrono::system_clock::now() << "]";
cout << "[WriterManager::kills] Killing writer manager." << endl;
#endif
killed_flag = true;
stop();
}
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";
} else {
return "finished";
}
}
string WriterManager::get_output_file() const
{
return output_file;
}
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}};
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)
{
lock_guard<mutex> lock(parameters_mutex);
#ifdef DEBUG_OUTPUT
stringstream output_message;
using namespace date;
output_message << "[" << std::chrono::system_clock::now() << "]";
output_message << "[WriterManager::set_parameters] Setting parameters: ";
#endif
for (const auto& parameter : new_parameters) {
auto& parameter_name = parameter.first;
auto& parameter_value = parameter.second;
parameters[parameter_name] = parameter_value;
#ifdef DEBUG_OUTPUT
output_message << parameter_name << ", ";
#endif
}
#ifdef DEBUG_OUTPUT
cout << output_message.str() << endl;
#endif
}
const unordered_map<string, DATA_TYPE>& WriterManager::get_parameters_type() const
{
return parameters_type;
}
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();
}
bool WriterManager::is_killed() const
{
return killed_flag.load();
}
void WriterManager::received_frame(size_t frame_index)
{
n_received_frames++;
}
void WriterManager::written_frame(size_t frame_index)
{
n_written_frames++;
}
void WriterManager::lost_frame(size_t frame_index)
{
n_lost_frames++;
}
bool WriterManager::are_all_parameters_set()
{
lock_guard<mutex> lock(parameters_mutex);
for (const auto& parameter : parameters_type) {
const auto& parameter_name = parameter.first;
if (parameters.count(parameter_name) == 0) {
#ifdef DEBUG_OUTPUT
using namespace date;
cout << "[" << std::chrono::system_clock::now() << "]";
cout << "[WriterManager::are_all_parameters_set] Parameter " << parameter_name << " not set." << endl;
#endif
return false;
}
}
return true;
}