From e2a20378b2a113f4954cec948ec916aba6a48a47 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 10 Jun 2021 05:16:03 +0200 Subject: [PATCH] Tests: PityTest - extract transport into class --- src/PityTransport.cc | 90 ++++++++++++++++++++++++++++++++++++++++++++ src/PityTransport.hh | 45 ++++++++++++++++++++++ 2 files changed, 135 insertions(+) create mode 100644 src/PityTransport.cc create mode 100644 src/PityTransport.hh diff --git a/src/PityTransport.cc b/src/PityTransport.cc new file mode 100644 index 0000000..e323169 --- /dev/null +++ b/src/PityTransport.cc @@ -0,0 +1,90 @@ +#include "PityTransport.hh" +#include "iostream" +#include "../../../src/std_utils.hh" +#include +#include +#include +#include + +namespace pEp { + namespace PityTest11 { + bool PityTransport::debug_log_enabled = false; + + PityTransport::PityTransport(const std::string& inboxDir, Endpoints& endpoints) : + _inboxDir{ inboxDir }, _endpoints{ endpoints } + { + } + + void PityTransport::sendMsg(const std::string nodename, const std::string& msg) const + { + pEpLogClass("Address: " + nodename + " msg: " + msg); + bool found = false; + std::string dir; + try { + dir = _endpoints.at(nodename); + } catch (std::out_of_range&) { + throw std::runtime_error("no such nodename: " + nodename); + } + Utils::dir_ensure(dir); + std::stringstream filename; + // collision detect + do { + filename << dir << Utils::random_string(97, 122, 16) << ".pitymsg"; + } while (Utils::path_exists(filename.str())); + // create + std::ofstream msgfile = Utils::file_create(filename.str()); + // write + msgfile << msg; + } + + bool PityTransport::hasMsg() const + { + bool ret = false; + pEpLogClass("called"); + Utils::dir_ensure(_inboxDir); + auto msg_filenames = Utils::dir_list_files(_inboxDir); + ret = msg_filenames.size() > 0; + return ret; + } + + // Non-blocking + // throws underflow_error if inbox empty + std::string PityTransport::pollMsg() const + { + pEpLogClass("called"); + std::string ret; + Utils::dir_ensure(_inboxDir); + auto msg_filenames = Utils::dir_list_files(_inboxDir); + if (!msg_filenames.empty()) { + std::string msg_filename = msg_filenames.at(0); + std::string msg_path = _inboxDir + "/" + msg_filename; + pEpLogClass("Reading file: " + msg_filename); + ret = Utils::file_read(msg_path); + Utils::path_delete(msg_path); + } else { + throw std::underflow_error("inbox empty: " + _inboxDir); + } + + return ret; + } + + std::string PityTransport::receiveMsg(int timeout_msec) const + { + pEpLogClass("called"); + std::string ret; + bool retry = false; + do { + try { + ret = pollMsg(); + retry = false; + } catch (const std::underflow_error&) { + pEpLogClass("polling again in [ms]: " + std::to_string(timeout_msec) + "..."); + Utils::sleep_millis(timeout_msec); + retry = true; + } + } while (retry); + return ret; + } + + } // namespace PityTest11 +} // namespace pEp diff --git a/src/PityTransport.hh b/src/PityTransport.hh new file mode 100644 index 0000000..130b298 --- /dev/null +++ b/src/PityTransport.hh @@ -0,0 +1,45 @@ +// This file is under GNU General Public License 3.0 +// see LICENSE.txt + +#ifndef PITYTEST_PITYTRANSPORT_HH +#define PITYTEST_PITYTRANSPORT_HH + +#include "../../../src/pEpLog.hh" +#include +#include +#include + +namespace pEp { + namespace PityTest11 { + // Address - Dir + using Endpoints = std::unordered_map; + + class PityTransport { + public: + // Constructors + PityTransport() = delete; + PityTransport(const std::string& inboxDir, Endpoints& endpoints); + + // Getters + //Transport + bool hasMsg() const; + void sendMsg(const std::string nodename, const std::string& msg) const; + std::string pollMsg() const; + std::string receiveMsg(int timeout_msec = 100) const; + + //internal logging + static bool debug_log_enabled; + Adapter::pEpLog::pEpLogger logger_debug{ "PityModel", debug_log_enabled }; + + private: + std::string _inboxDir; + Endpoints& _endpoints; + + //internal logging + Adapter::pEpLog::pEpLogger& m4gic_logger_n4me = logger_debug; + }; + }; // namespace PityTest11 + +}; // namespace pEp + +#endif // PITYTEST_PITYTRANSPORT_HH