mirror of
https://github.com/paulscherrerinstitute/sf_daq_buffer.git
synced 2026-04-23 07:02:41 +02:00
Add BinaryReader for live writer
The binary reader reads 1 frame at a time from a module - the difference being loading 1 image instead of 1 block of images. This will allow to set the offset and pulse_id increment easily without complicated internal buffer calculations.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
#ifndef SF_DAQ_BUFFER_BINARYREADER_HPP
|
||||
#define SF_DAQ_BUFFER_BINARYREADER_HPP
|
||||
|
||||
|
||||
#include <formats.hpp>
|
||||
|
||||
class BinaryReader {
|
||||
|
||||
const std::string root_folder_;
|
||||
const std::string device_name_;
|
||||
|
||||
std::string current_input_file_;
|
||||
int input_file_fd_;
|
||||
|
||||
void open_file(const std::string& filename);
|
||||
void close_current_file();
|
||||
|
||||
public:
|
||||
BinaryReader(const std::string &root_folder,
|
||||
const std::string &device_name);
|
||||
|
||||
~BinaryReader();
|
||||
|
||||
void get_frame(const uint64_t pulse_id, BufferBinaryFormat *buffer);
|
||||
};
|
||||
|
||||
|
||||
#endif //SF_DAQ_BUFFER_BINARYREADER_HPP
|
||||
@@ -0,0 +1,104 @@
|
||||
#include "BinaryReader.hpp"
|
||||
|
||||
#include <unistd.h>
|
||||
#include <sstream>
|
||||
#include <cstring>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "BufferUtils.hpp"
|
||||
#include "writer_config.hpp"
|
||||
#include "buffer_config.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace writer_config;
|
||||
using namespace buffer_config;
|
||||
|
||||
BinaryReader::BinaryReader(
|
||||
const std::string &root_folder,
|
||||
const std::string &device_name) :
|
||||
root_folder_(root_folder),
|
||||
device_name_(device_name),
|
||||
current_input_file_(""),
|
||||
input_file_fd_(-1)
|
||||
{}
|
||||
|
||||
BinaryReader::~BinaryReader()
|
||||
{
|
||||
close_current_file();
|
||||
}
|
||||
|
||||
void BinaryReader::get_frame(
|
||||
const uint64_t pulse_id, BufferBinaryFormat* buffer)
|
||||
{
|
||||
|
||||
auto current_frame_file = BufferUtils::get_filename(
|
||||
root_folder_, device_name_, pulse_id);
|
||||
|
||||
if (current_frame_file != current_input_file_) {
|
||||
open_file(current_frame_file);
|
||||
}
|
||||
|
||||
size_t file_index = BufferUtils::get_file_frame_index(pulse_id);
|
||||
size_t n_bytes_offset = file_index * sizeof(BufferBinaryFormat);
|
||||
|
||||
auto lseek_result = lseek(input_file_fd_, n_bytes_offset, SEEK_SET);
|
||||
if (lseek_result < 0) {
|
||||
stringstream err_msg;
|
||||
|
||||
err_msg << "[BinaryReader::get_frame]";
|
||||
err_msg << " Error while lseek on file ";
|
||||
err_msg << current_input_file_ << " for n_bytes_offset ";
|
||||
err_msg << n_bytes_offset << ": " << strerror(errno) << endl;
|
||||
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
|
||||
auto n_bytes = ::read(input_file_fd_, buffer, sizeof(BufferBinaryFormat));
|
||||
|
||||
if (n_bytes < sizeof(BufferBinaryFormat)) {
|
||||
stringstream err_msg;
|
||||
|
||||
err_msg << "[BinaryReader::get_block]";
|
||||
err_msg << " Error while reading from file ";
|
||||
err_msg << current_input_file_ << ": " << strerror(errno) << endl;
|
||||
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
}
|
||||
|
||||
void BinaryReader::open_file(const std::string& filename)
|
||||
{
|
||||
close_current_file();
|
||||
|
||||
input_file_fd_ = open(filename.c_str(), O_RDONLY);
|
||||
|
||||
if (input_file_fd_ < 0) {
|
||||
stringstream err_msg;
|
||||
|
||||
err_msg << "[BinaryReader::open_file]";
|
||||
err_msg << " Cannot open file " << filename << ": ";
|
||||
err_msg << strerror(errno) << endl;
|
||||
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
|
||||
current_input_file_ = filename;
|
||||
}
|
||||
|
||||
void BinaryReader::close_current_file()
|
||||
{
|
||||
if (input_file_fd_ != -1) {
|
||||
if (close(input_file_fd_) < 0) {
|
||||
stringstream err_msg;
|
||||
|
||||
err_msg << "[BinaryWriter::close_current_file]";
|
||||
err_msg << " Error while closing file " << current_input_file_;
|
||||
err_msg << ": " << strerror(errno) << endl;
|
||||
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
|
||||
input_file_fd_ = -1;
|
||||
current_input_file_ = "";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user