From b7f9711fc7ff545f8fae5a4df929a9e48d98f47d Mon Sep 17 00:00:00 2001 From: astri Date: Thu, 18 May 2023 19:20:53 +0200 Subject: [PATCH 1/7] refactoring configuration --- CMakeLists.txt | 1 - deps/Base-DAQ | 2 +- include/File_Archiver.h | 21 ++++++---- src/File_Archiver.cpp | 91 +++++++++++++++++++++++++++++++++-------- 4 files changed, 87 insertions(+), 28 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fe8a227..c4436c6 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,6 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall ") option(FILEARCHIVER_BUILD_SHARED "Build filearchiver as a shared library." OFF) set(SOURCES - src/File_Archiver_Configurator.cpp src/File_Archiver.cpp ) diff --git a/deps/Base-DAQ b/deps/Base-DAQ index a2095e5..f118b02 160000 --- a/deps/Base-DAQ +++ b/deps/Base-DAQ @@ -1 +1 @@ -Subproject commit a2095e59d57271e3bef3422a67c6905fd237467c +Subproject commit f118b029ff73dc23989c5b648cd6175b968a9a14 diff --git a/include/File_Archiver.h b/include/File_Archiver.h index ed8ea2a..776e09c 100755 --- a/include/File_Archiver.h +++ b/include/File_Archiver.h @@ -9,6 +9,7 @@ #include #include +#include namespace inaf::oasbo::Archivers{ @@ -16,19 +17,21 @@ class FileArchiver : public BaseArchiver{ protected: std::ofstream *myfile = nullptr; std::string computeFileName(std::string fileName); - -public: FileArchiver(std::string dest); + FileArchiver(std::string parent, std::string dest); FileArchiver(); + class Builder; +public: + std::string parentPath; - int write(PacketLib::BasePacket &); - int write(PacketLib::BasePacket &, std::string dest); + int write(PacketLib::BasePacket &) override; + int write(PacketLib::BasePacket &, std::string dest) override; - int close(); - int open(); - void setDest(std::string); - void updateDest(); - std::string getDest(); + int close() override; + int open() override; + void setDest(std::string) override; + void updateDest() override; + std::string getDest() override; ~FileArchiver(){ close(); } diff --git a/src/File_Archiver.cpp b/src/File_Archiver.cpp index 19d0d2a..087a2d7 100755 --- a/src/File_Archiver.cpp +++ b/src/File_Archiver.cpp @@ -1,20 +1,26 @@ #include #include -#define OUT_ROOT std::string(std::getenv("BIAS_PREFIX")).append("/ArchiveRAW/") - using namespace inaf::oasbo::Archivers; +FileArchiver::FileArchiver(std::string parent, std::string dest) { + this->parentPath = parent; + this->dest = std::string(parentPath).append(dest).append(".raw"); +} + FileArchiver::FileArchiver(std::string dest) { - this->dest = OUT_ROOT.append(dest).append(".raw"); + std::string parent = std::string(std::getenv("HOME")).append("/ArchiveRAW/"); + FileArchiver(parent,dest); } FileArchiver::FileArchiver() { - FileArchiver(".tmp.raw"); + std::string parent = std::string(std::getenv("HOME")).append("/ArchiveRAW/"); + std::string dest = std::string(".tmp.raw"); + FileArchiver(parent,dest); } int FileArchiver::write(PacketLib::BasePacket &pack) { - if( myfile == nullptr || !myfile->is_open()){ + if (myfile == nullptr || !myfile->is_open()) { std::cerr << "Archiver err: File not opened" << std::endl; return -1; } @@ -26,12 +32,12 @@ int FileArchiver::write(PacketLib::BasePacket &pack) { int FileArchiver::write(PacketLib::BasePacket &pack, std::string dest) { namespace fs = std::filesystem; fs::path dest_path(dest); - if(!fs::exists(dest_path.parent_path()) || std::filesystem::exists(dest_path)){ - this->dest = computeFileName(dest); + if (!fs::exists(dest_path.parent_path()) + || std::filesystem::exists(dest_path)) { + this->dest = computeFileName(dest); } std::ofstream *file = new std::ofstream(dest, std::ios::binary); - file->write( - (char*) pack.getBinaryPointer(), + file->write((char*) pack.getBinaryPointer(), pack.getHeaderSize() + pack.getPayloadSize() + pack.getTailSize()); file->close(); delete file; @@ -41,19 +47,20 @@ int FileArchiver::write(PacketLib::BasePacket &pack, std::string dest) { int FileArchiver::open() { namespace fs = std::filesystem; fs::path dest_path(this->dest); - if(!fs::exists(dest_path.parent_path()) || std::filesystem::exists(dest_path)){ + if (!fs::exists(dest_path.parent_path()) + || std::filesystem::exists(dest_path)) { this->dest = computeFileName(this->dest); } - if(myfile != nullptr) + if (myfile != nullptr) delete myfile; this->myfile = new std::ofstream(this->dest, std::ios::binary); return myfile->good(); } int FileArchiver::close() { - if(myfile != nullptr && myfile->is_open()) + if (myfile != nullptr && myfile->is_open()) myfile->close(); - if(myfile != nullptr) + if (myfile != nullptr) delete myfile; myfile = nullptr; return 1; @@ -64,7 +71,7 @@ std::string FileArchiver::getDest() { } void FileArchiver::setDest(std::string dest) { - this->dest = OUT_ROOT.append(dest).append(".raw"); + this->dest = std::string(this->parentPath).append(dest).append(".raw"); } std::string FileArchiver::computeFileName(std::string fileName) { @@ -77,12 +84,15 @@ std::string FileArchiver::computeFileName(std::string fileName) { return dest_path.string(); } // Check if the the file already exists and append an incremental suffix - if(fs::exists(dest_path)){ + if (fs::exists(dest_path)) { std::string extension = dest_path.extension().string(); dest_path.replace_extension(""); int count = 0; - while(fs::exists(dest_path.string()+"_(" + std::to_string(++count)+")" + extension)); - dest_path +="_(" + std::to_string(count)+")" + extension; + while (fs::exists( + dest_path.string() + "_(" + std::to_string(++count) + ")" + + extension)) + ; + dest_path += "_(" + std::to_string(count) + ")" + extension; } return dest_path.string(); } @@ -90,3 +100,50 @@ std::string FileArchiver::computeFileName(std::string fileName) { void FileArchiver::updateDest() { } + +class FileArchiver::Builder { +private: + FileArchiver *arch; + +public: + Builder() { + this->reset(); + } + Builder(std::string dest) { + this->reset(); + setDest(dest); + } + ~Builder() { + delete arch; + } + + void reset() { + this->arch = new FileArchiver(); + } + + Builder* configFrom(Configurators::BaseConfigurator *conf) { + std::map params = + conf->readConfig(); + if (params.count("archive_path") > 0) + arch->parentPath = params["archive_path"]; + if (params.count("file_name") > 0) + arch->setDest(params["file_name"]); + return this; + } + + Builder* setParent(std::string parent) { + arch->parentPath = parent; + return this; + } + + Builder* setDest(std::string dest) { + arch->setDest(dest); + return this; + } + + FileArchiver* getArchiver() { + FileArchiver *result = this->arch; + this->reset(); + return result; + } +}; -- GitLab From 1b0f48bd45f85e5149887419f8a72d0e027b55a3 Mon Sep 17 00:00:00 2001 From: astri Date: Thu, 18 May 2023 19:24:43 +0200 Subject: [PATCH 2/7] fix public builder --- include/File_Archiver.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/File_Archiver.h b/include/File_Archiver.h index 776e09c..260ee72 100755 --- a/include/File_Archiver.h +++ b/include/File_Archiver.h @@ -20,9 +20,9 @@ protected: FileArchiver(std::string dest); FileArchiver(std::string parent, std::string dest); FileArchiver(); - class Builder; public: std::string parentPath; + class Builder; int write(PacketLib::BasePacket &) override; int write(PacketLib::BasePacket &, std::string dest) override; -- GitLab From 54627c2a2b46c9069975f2705f3eaeea892edf5d Mon Sep 17 00:00:00 2001 From: astri Date: Thu, 18 May 2023 19:40:30 +0200 Subject: [PATCH 3/7] fix builder constructors --- src/File_Archiver.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/File_Archiver.cpp b/src/File_Archiver.cpp index 087a2d7..fe5a83b 100755 --- a/src/File_Archiver.cpp +++ b/src/File_Archiver.cpp @@ -110,8 +110,10 @@ public: this->reset(); } Builder(std::string dest) { - this->reset(); - setDest(dest); + this->arch = new FileArchiver(dest); + } + Builder(std::string parent, std::string dest) { + this->arch = new FileArchiver(parent, dest); } ~Builder() { delete arch; -- GitLab From 4a179b996d250eb5a23707b08b871f7e4d25b1b8 Mon Sep 17 00:00:00 2001 From: astri Date: Thu, 18 May 2023 19:50:42 +0200 Subject: [PATCH 4/7] fix "magic word" --- src/File_Archiver.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/File_Archiver.cpp b/src/File_Archiver.cpp index fe5a83b..1c5ce89 100755 --- a/src/File_Archiver.cpp +++ b/src/File_Archiver.cpp @@ -102,10 +102,12 @@ void FileArchiver::updateDest() { } class FileArchiver::Builder { -private: +protected: FileArchiver *arch; - public: + std::string file_name_key{"file_name"}; + std::string archive_path_key{"archive_path"}; + Builder() { this->reset(); } @@ -126,10 +128,10 @@ public: Builder* configFrom(Configurators::BaseConfigurator *conf) { std::map params = conf->readConfig(); - if (params.count("archive_path") > 0) - arch->parentPath = params["archive_path"]; - if (params.count("file_name") > 0) - arch->setDest(params["file_name"]); + if (params.count(file_name_key) > 0) + arch->parentPath = params[file_name_key]; + if (params.count(archive_path_key) > 0) + arch->setDest(params[archive_path_key]); return this; } -- GitLab From ebf59405aafcd9c250b7f79e8f791d9355a86de0 Mon Sep 17 00:00:00 2001 From: astri Date: Thu, 25 May 2023 12:56:43 +0200 Subject: [PATCH 5/7] builder refactor --- include/File_Archiver.h | 35 +++++++++++++--- src/File_Archiver.cpp | 91 +++++++++++++++++++---------------------- 2 files changed, 72 insertions(+), 54 deletions(-) diff --git a/include/File_Archiver.h b/include/File_Archiver.h index 260ee72..5d9426b 100755 --- a/include/File_Archiver.h +++ b/include/File_Archiver.h @@ -11,9 +11,9 @@ #include #include -namespace inaf::oasbo::Archivers{ +namespace inaf::oasbo::Archivers { -class FileArchiver : public BaseArchiver{ +class FileArchiver: public BaseArchiver { protected: std::ofstream *myfile = nullptr; std::string computeFileName(std::string fileName); @@ -22,20 +22,43 @@ protected: FileArchiver(); public: std::string parentPath; - class Builder; - int write(PacketLib::BasePacket &) override; - int write(PacketLib::BasePacket &, std::string dest) override; + int write(PacketLib::BasePacket&) override; + int write(PacketLib::BasePacket&, std::string dest) override; int close() override; int open() override; void setDest(std::string) override; void updateDest() override; std::string getDest() override; - ~FileArchiver(){ + ~FileArchiver() { close(); } + friend class Builder; + class Builder { + protected: + FileArchiver *arch; + public: + std::string file_name_key { "file_name" }; + std::string archive_path_key { "archive_path" }; + + Builder(); + Builder(std::string dest); + Builder(std::string parent, std::string dest); + ~Builder(); + + void reset(); + + Builder* configFrom(Configurators::BaseConfigurator *conf); + + Builder* setParent(std::string parent); + + Builder* setDest(std::string dest); + + FileArchiver* getArchiver(); + }; + }; } #endif /* FILEARCHIVER_H_ */ diff --git a/src/File_Archiver.cpp b/src/File_Archiver.cpp index 1c5ce89..de960fc 100755 --- a/src/File_Archiver.cpp +++ b/src/File_Archiver.cpp @@ -9,14 +9,16 @@ FileArchiver::FileArchiver(std::string parent, std::string dest) { } FileArchiver::FileArchiver(std::string dest) { - std::string parent = std::string(std::getenv("HOME")).append("/ArchiveRAW/"); - FileArchiver(parent,dest); + std::string parent = std::string(std::getenv("HOME")).append( + "/ArchiveRAW/"); + FileArchiver(parent, dest); } FileArchiver::FileArchiver() { - std::string parent = std::string(std::getenv("HOME")).append("/ArchiveRAW/"); + std::string parent = std::string(std::getenv("HOME")).append( + "/ArchiveRAW/"); std::string dest = std::string(".tmp.raw"); - FileArchiver(parent,dest); + FileArchiver(parent, dest); } int FileArchiver::write(PacketLib::BasePacket &pack) { @@ -101,53 +103,46 @@ void FileArchiver::updateDest() { } -class FileArchiver::Builder { -protected: - FileArchiver *arch; -public: - std::string file_name_key{"file_name"}; - std::string archive_path_key{"archive_path"}; +using F_B = FileArchiver::Builder; - Builder() { - this->reset(); - } - Builder(std::string dest) { - this->arch = new FileArchiver(dest); - } - Builder(std::string parent, std::string dest) { - this->arch = new FileArchiver(parent, dest); - } - ~Builder() { - delete arch; - } +F_B::Builder() { + this->reset(); +} +F_B::Builder(std::string dest) { + this->arch = new FileArchiver(dest); +} +F_B::Builder(std::string parent, std::string dest) { + this->arch = new FileArchiver(parent, dest); +} +F_B::~Builder() { + delete arch; +} - void reset() { - this->arch = new FileArchiver(); - } +void F_B::reset() { + this->arch = new FileArchiver(); +} - Builder* configFrom(Configurators::BaseConfigurator *conf) { - std::map params = - conf->readConfig(); - if (params.count(file_name_key) > 0) - arch->parentPath = params[file_name_key]; - if (params.count(archive_path_key) > 0) - arch->setDest(params[archive_path_key]); - return this; - } +F_B* F_B::configFrom(Configurators::BaseConfigurator *conf) { + std::map params = conf->readConfig(); + if (params.count(file_name_key) > 0) + arch->parentPath = params[file_name_key]; + if (params.count(archive_path_key) > 0) + arch->setDest(params[archive_path_key]); + return this; +} - Builder* setParent(std::string parent) { - arch->parentPath = parent; - return this; - } +F_B* F_B::setParent(std::string parent) { + arch->parentPath = parent; + return this; +} - Builder* setDest(std::string dest) { - arch->setDest(dest); - return this; - } +F_B* F_B::setDest(std::string dest) { + arch->setDest(dest); + return this; +} - FileArchiver* getArchiver() { - FileArchiver *result = this->arch; - this->reset(); - return result; - } -}; +FileArchiver* F_B::getArchiver() { + FileArchiver *result = this->arch; + this->reset(); + return result; +} -- GitLab From 9e13603b8c6cbb86000a4622702e0322b2947c99 Mon Sep 17 00:00:00 2001 From: astri Date: Sat, 3 Jun 2023 18:46:53 +0200 Subject: [PATCH 6/7] config refactorig ok --- deps/Base-DAQ | 2 +- src/File_Archiver.cpp | 39 +++++++++++++++++++++------------------ 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/deps/Base-DAQ b/deps/Base-DAQ index f118b02..2b4e8b0 160000 --- a/deps/Base-DAQ +++ b/deps/Base-DAQ @@ -1 +1 @@ -Subproject commit f118b029ff73dc23989c5b648cd6175b968a9a14 +Subproject commit 2b4e8b039ec5da9ddd53720991c1b95f034e86fb diff --git a/src/File_Archiver.cpp b/src/File_Archiver.cpp index de960fc..0278f41 100755 --- a/src/File_Archiver.cpp +++ b/src/File_Archiver.cpp @@ -3,22 +3,19 @@ using namespace inaf::oasbo::Archivers; -FileArchiver::FileArchiver(std::string parent, std::string dest) { - this->parentPath = parent; +FileArchiver::FileArchiver(std::string parent, std::string dest) : + parentPath(parent) { this->dest = std::string(parentPath).append(dest).append(".raw"); } -FileArchiver::FileArchiver(std::string dest) { - std::string parent = std::string(std::getenv("HOME")).append( - "/ArchiveRAW/"); - FileArchiver(parent, dest); +FileArchiver::FileArchiver(std::string dest) : + FileArchiver(std::string(std::getenv("HOME")).append("/ArchiveRAW/"), + dest) { } -FileArchiver::FileArchiver() { - std::string parent = std::string(std::getenv("HOME")).append( - "/ArchiveRAW/"); - std::string dest = std::string(".tmp.raw"); - FileArchiver(parent, dest); +FileArchiver::FileArchiver() : + FileArchiver(std::string(std::getenv("HOME")).append("/ArchiveRAW/"), + ".tmp") { } int FileArchiver::write(PacketLib::BasePacket &pack) { @@ -78,14 +75,14 @@ void FileArchiver::setDest(std::string dest) { std::string FileArchiver::computeFileName(std::string fileName) { namespace fs = std::filesystem; - // Check if the directory of the file path exists +// Check if the directory of the file path exists fs::path dest_path(fileName); if (!fs::exists(dest_path.parent_path())) { // Create the directory and any missing parent directories fs::create_directories(dest_path.parent_path()); return dest_path.string(); } - // Check if the the file already exists and append an incremental suffix +// Check if the the file already exists and append an incremental suffix if (fs::exists(dest_path)) { std::string extension = dest_path.extension().string(); dest_path.replace_extension(""); @@ -123,11 +120,17 @@ void F_B::reset() { } F_B* F_B::configFrom(Configurators::BaseConfigurator *conf) { - std::map params = conf->readConfig(); - if (params.count(file_name_key) > 0) - arch->parentPath = params[file_name_key]; - if (params.count(archive_path_key) > 0) - arch->setDest(params[archive_path_key]); + std::string target = std::string("FileArchiver"); + conf->readConfigFromSource(target); + std::map params = conf->getConfig(); + + std::string key = target+"_"+file_name_key; + if (params.count(key) > 0) + arch->parentPath = params[key]; + + key = target+"_"+archive_path_key; + if (params.count(key) > 0) + arch->setDest(params[key]); return this; } -- GitLab From 60517861e3a1e1e8a55db0db07f1930436bb527a Mon Sep 17 00:00:00 2001 From: valerio Date: Sat, 3 Jun 2023 19:27:13 +0200 Subject: [PATCH 7/7] deps removed --- deps/Base-DAQ | 1 - 1 file changed, 1 deletion(-) delete mode 160000 deps/Base-DAQ diff --git a/deps/Base-DAQ b/deps/Base-DAQ deleted file mode 160000 index 2b4e8b0..0000000 --- a/deps/Base-DAQ +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 2b4e8b039ec5da9ddd53720991c1b95f034e86fb -- GitLab