accumulator: refactor entropy_src,add files
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
* add event_scheduler_impl.h and properly override its base class * add urandom_entropy_src.cpp, move there logic from header * add urandom_entropy_src.cpp to CMakeLists.txt * add unique_ptr per impl: one for both EventSchedulerImpl and EventAdderImpl each * wrap a call in reinterpret_cast and reformat code * add missing includes (best effort)
This commit is contained in:
parent
8c3aee1b07
commit
03578f9016
|
@ -225,6 +225,7 @@ set(FORTUNA_SOURCES
|
|||
accumulator.cpp
|
||||
pool.cpp
|
||||
do_task.cpp
|
||||
urandom_entropy_src.cpp
|
||||
seed_file_management.cpp)
|
||||
set(FORTUNA_HEADERS
|
||||
fortuna.h
|
||||
|
@ -235,6 +236,7 @@ set(FORTUNA_HEADERS
|
|||
event_adder.h
|
||||
event_adder_impl.h
|
||||
event_scheduler.h
|
||||
event_scheduler_impl.h
|
||||
entropy_src.h
|
||||
urandom_entropy_src.h
|
||||
util.h
|
||||
|
|
|
@ -2,17 +2,20 @@
|
|||
#define FORTUNA_ENTROPY_SRC_H
|
||||
|
||||
#include "event_adder_impl.h"
|
||||
#include "event_scheduler.h"
|
||||
#include "event_scheduler_impl.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace fortuna {
|
||||
namespace accumulator {
|
||||
|
||||
class EntropySrc {
|
||||
private:
|
||||
std::unique_ptr<EventAdderImpl> impl;
|
||||
std::unique_ptr<EventAdderImpl> ea_impl;
|
||||
std::unique_ptr<EventSchedulerImpl> es_impl;
|
||||
|
||||
public:
|
||||
virtual void schedule(accumulator::EventScheduler scheduler) = 0;
|
||||
virtual void schedule(accumulator::EventSchedulerImpl scheduler) = 0;
|
||||
virtual void event(accumulator::EventAdderImpl adder) = 0;
|
||||
|
||||
static constexpr const std::size_t max_event_length{32};
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef FORTUNA_EVENT_SCHEDULER_IMPL_H
|
||||
#define FORTUNA_EVENT_SCHEDULER_IMPL_H
|
||||
|
||||
#include "event_scheduler.h"
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace fortuna {
|
||||
namespace accumulator {
|
||||
|
||||
class EventSchedulerImpl final : public EventScheduler {
|
||||
private:
|
||||
std::chrono::milliseconds delay;
|
||||
|
||||
public:
|
||||
EventSchedulerImpl();
|
||||
|
||||
void schedule(std::chrono::milliseconds delay_ms) override {}
|
||||
};
|
||||
|
||||
} // namespace accumulator
|
||||
} // namespace fortuna
|
||||
|
||||
#endif // FORTUNA_EVENT_SCHEDULER_IMPL_H
|
|
@ -0,0 +1,43 @@
|
|||
#ifndef FORTUNA_URANDOM_ENTROPY_SRC_CPP
|
||||
#define FORTUNA_URANDOM_ENTROPY_SRC_CPP
|
||||
|
||||
#include "urandom_entropy_src.h"
|
||||
|
||||
namespace fortuna {
|
||||
namespace accumulator {
|
||||
|
||||
std::vector<char> UrandomEntropySrc::bytes{};
|
||||
|
||||
auto UrandomEntropySrc::schedule(accumulator::EventSchedulerImpl scheduler)
|
||||
-> void {
|
||||
scheduler.schedule(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
auto UrandomEntropySrc::event(accumulator::EventAdderImpl adder) -> void {
|
||||
std::ifstream file;
|
||||
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
try {
|
||||
std::ifstream urandom("/dev/urandom", std::ios::in | std::ios::binary);
|
||||
// check if stream is open
|
||||
if (urandom) {
|
||||
urandom.read(
|
||||
reinterpret_cast<char*>(&UrandomEntropySrc::bytes),
|
||||
reinterpret_cast<uint64_t>(UrandomEntropySrc::bytes.size()));
|
||||
urandom.close();
|
||||
}
|
||||
else {
|
||||
// open failed
|
||||
fmt::print(stderr, "Failed to open /dev/urandom\n");
|
||||
}
|
||||
}
|
||||
catch (std::ifstream::failure& e) {
|
||||
fmt::print("io exception caugth: {}\n", e.what());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} // namespace accumulator
|
||||
} // namespace fortuna
|
||||
|
||||
#endif // FORTUNA_URANDOM_ENTROPY_SRC_CPP
|
|
@ -2,50 +2,31 @@
|
|||
#define FORTUNA_URANDOM_ENTROPY_SRC_H
|
||||
|
||||
#include "entropy_src.h"
|
||||
#include "event_adder_impl.h"
|
||||
#include "event_scheduler_impl.h"
|
||||
|
||||
#include <fmt/core.h>
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
namespace fortuna {
|
||||
namespace accumulator {
|
||||
|
||||
class UrandomEntropySrc : public EntropySrc {
|
||||
private:
|
||||
static std::vector<char> bytes;
|
||||
|
||||
public:
|
||||
static constexpr const std::size_t max_event_length{32};
|
||||
auto schedule(accumulator::EventSchedulerImpl scheduler) -> void;
|
||||
|
||||
auto schedule(accumulator::EventScheduler scheduler) -> void {
|
||||
scheduler.schedule(std::chrono::milliseconds(100));
|
||||
}
|
||||
|
||||
auto event(accumulator::EventAdder adder) -> void {
|
||||
std::ifstream file;
|
||||
file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
|
||||
try {
|
||||
std::ifstream urandom("/dev/urandom",
|
||||
std::ios::in | std::ios::binary);
|
||||
// check if stream is open
|
||||
if (urandom) {
|
||||
urandom.read(reinterpret_cast<char*>(&bytes), bytes.length());
|
||||
urandom.close();
|
||||
}
|
||||
else {
|
||||
// open failed
|
||||
fmt::print(stderr, "Failed to open /dev/urandom\n");
|
||||
}
|
||||
}
|
||||
catch (std::ifstream::failure& e) {
|
||||
fmt::print("io exception caugth: {}\n", e.what());
|
||||
}
|
||||
}
|
||||
auto event(accumulator::EventAdderImpl adder) -> void;
|
||||
|
||||
auto urandom_entropy_src_service() -> int;
|
||||
|
||||
private:
|
||||
static std::vector<char> bytes[max_event_length];
|
||||
};
|
||||
|
||||
} // namespace accumulator
|
||||
} // namespace fortuna
|
||||
#endif // FORTUNA_URANDOM_ENTROPY_SRC_H
|
||||
|
||||
|
|
Reference in New Issue