mirror of
https://github.com/paulscherrerinstitute/sf_daq_buffer.git
synced 2026-04-21 23:34:35 +02:00
Add write implementation
This commit is contained in:
@@ -5,16 +5,17 @@
|
||||
#include <cerrno>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <buffer_utils.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
BinaryWriter::BinaryWriter(
|
||||
const string& device_name,
|
||||
const string& root_folder) :
|
||||
device_name_(device_name),
|
||||
root_folder_(root_folder),
|
||||
current_filename_(""),
|
||||
output_fd_(-1)
|
||||
device_name_(device_name),
|
||||
root_folder_(root_folder),
|
||||
current_output_filename_(""),
|
||||
output_file_fd_(-1)
|
||||
{
|
||||
latest_filename_ = root_folder + "/" + device_name + "/LATEST";
|
||||
|
||||
@@ -31,15 +32,51 @@ BinaryWriter::BinaryWriter(
|
||||
#endif
|
||||
}
|
||||
|
||||
void BinaryWriter::write(uint64_t pulse_id, const JFFileFormat& buffer)
|
||||
void BinaryWriter::write(uint64_t pulse_id, const JFFileFormat* buffer)
|
||||
{
|
||||
auto current_frame_file =
|
||||
get_filename(root_folder_, device_name_, pulse_id);
|
||||
|
||||
if (current_frame_file != current_output_filename_) {
|
||||
close_current_file();
|
||||
open_file(current_frame_file);
|
||||
}
|
||||
|
||||
auto n_bytes = ::write(output_file_fd_, buffer, sizeof(JFFileFormat));
|
||||
if (n_bytes < sizeof(JFFileFormat)) {
|
||||
stringstream err_msg;
|
||||
|
||||
using namespace date;
|
||||
using namespace chrono;
|
||||
err_msg << "[" << system_clock::now() << "]";
|
||||
err_msg << "[BinaryWriter::write";
|
||||
err_msg << " Error while writing to file ";
|
||||
err_msg << current_output_filename_ << ": ";
|
||||
err_msg << strerror(errno) << endl;
|
||||
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
|
||||
auto sync_result = ::fdatasync(output_file_fd_);
|
||||
if (sync_result < 0) {
|
||||
stringstream err_msg;
|
||||
|
||||
using namespace date;
|
||||
using namespace chrono;
|
||||
err_msg << "[" << system_clock::now() << "]";
|
||||
err_msg << "[BinaryWriter::write";
|
||||
err_msg << " Error while fdatasync on file ";
|
||||
err_msg << current_output_filename_ << ": ";
|
||||
err_msg << strerror(errno) << endl;
|
||||
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
}
|
||||
|
||||
void BinaryWriter::close_current_file()
|
||||
{
|
||||
if (output_fd_ != -1) {
|
||||
if (close(output_fd_) < 0) {
|
||||
if (output_file_fd_ != -1) {
|
||||
if (close(output_file_fd_) < 0) {
|
||||
stringstream err_msg;
|
||||
|
||||
using namespace date;
|
||||
@@ -47,18 +84,22 @@ void BinaryWriter::close_current_file()
|
||||
err_msg << "[" << system_clock::now() << "]";
|
||||
err_msg << "[BinaryWriter::close_current_file]";
|
||||
err_msg << " Error while closing file ";
|
||||
err_msg << current_filename_ << ": ";
|
||||
err_msg << current_output_filename_ << ": ";
|
||||
err_msg << strerror(errno) << endl;
|
||||
|
||||
throw runtime_error(err_msg.str());
|
||||
}
|
||||
|
||||
output_file_fd_ = -1;
|
||||
|
||||
// TODO: Ugly hack, please please fix it.
|
||||
stringstream latest_command;
|
||||
latest_command << "echo " << current_filename_;
|
||||
latest_command << "echo " << current_output_filename_;
|
||||
latest_command << " > " << latest_filename_;
|
||||
auto str_latest_command = latest_command.str();
|
||||
|
||||
system(str_latest_command.c_str());
|
||||
}
|
||||
|
||||
current_output_filename_ = "";
|
||||
}
|
||||
Reference in New Issue
Block a user