sfm: improve the job "RUNNING" logic, use static
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-01-13 02:49:45 +01:00
parent 2c3d5b4528
commit 226a5c2c6c
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 23 additions and 11 deletions

View File

@ -6,32 +6,34 @@
#include "util.h"
#include <cryptopp/secblock.h>
#include <fmt/core.h>
#include <fstream>
#include <stdexcept>
#include <utility>
namespace fortuna {
bool SeedFileManager::RUNNING{false};
SeedFileManager::SeedFileManager(
const fortuna::accumulator::Accumulator& accumulator) noexcept {
this->accumulator = accumulator;
}
SeedFileManager::~SeedFileManager() noexcept {
set_job_running(false);
set_job_running(false); // RIP, well, yeah, not running when the obj dies
}
auto SeedFileManager::is_job_running() -> bool {
return running;
return RUNNING;
}
auto SeedFileManager::set_job_running(bool running) -> void {
this->running = running;
SeedFileManager::RUNNING = running;
}
auto SeedFileManager::do_stuff() -> void {
update_seed_file();
do_task.thread_pls(config.write_interval, [this] { write_seed_file(); });
running = true;
}
auto SeedFileManager::update_seed_file() -> void {
@ -42,9 +44,11 @@ auto SeedFileManager::update_seed_file() -> void {
if (!f_stream) {
std::string msg{"error opening seed file"};
fmt::print("{} {}\n", msg, config.seed_f_path);
fortuna::SeedFileManager::RUNNING = false;
// FIXME: perhaps create a seed file instead of bailing...
throw std::runtime_error("error opening seed file");
}
fortuna::SeedFileManager::RUNNING = true;
f_stream.read(reinterpret_cast<char*>(buff.BytePtr()),
config.seed_f_length);
@ -77,10 +81,18 @@ auto SeedFileManager::write_seed_file() -> void {
std::string da_buff{accumulator.get_random_data(seed_file_length_blocks)};
fmt::print("[*] sfm: writing seed file\n");
std::ofstream f_stream{config.seed_f_path,
std::ios::binary | std::ios::trunc};
f_stream.write(reinterpret_cast<const char*>(buff.BytePtr()),
config.seed_f_length);
try {
std::ofstream f_stream{config.seed_f_path,
std::ios::binary | std::ios::trunc};
f_stream.write(reinterpret_cast<const char*>(buff.BytePtr()),
config.seed_f_length);
fortuna::SeedFileManager::RUNNING = true;
}
catch (std::exception& e) {
fmt::print("[!] sfm: error writing to seed file!\n");
fmt::print("{}\n", e.what());
fortuna::SeedFileManager::RUNNING = false;
}
}

View File

@ -21,7 +21,7 @@ public:
conf(){};
};
auto is_job_running() -> bool;
static auto is_job_running() -> bool;
auto do_stuff() -> void;
SeedFileManager(
@ -29,11 +29,11 @@ public:
~SeedFileManager() noexcept;
protected:
auto set_job_running(bool) -> void;
static auto set_job_running(bool) -> void;
private:
const conf config;
bool running = false;
static bool RUNNING;
DoTask do_task;
fortuna::accumulator::Accumulator accumulator;