From fd30abb783dcd2a254cf3ca70ed5e001f6af3219 Mon Sep 17 00:00:00 2001 From: heck Date: Tue, 8 Jun 2021 20:07:34 +0200 Subject: [PATCH] Test: PityTest - Add transport --- src/PityModel.cc | 67 ++++++++++++++++++++++++++++++++++++++++++++-- src/PityModel.hh | 25 ++++++++++++++++- test/test_model.cc | 16 +++++------ 3 files changed, 96 insertions(+), 12 deletions(-) diff --git a/src/PityModel.cc b/src/PityModel.cc index 01d6b8f..87f42ed 100644 --- a/src/PityModel.cc +++ b/src/PityModel.cc @@ -1,6 +1,9 @@ #include "PityModel.hh" //#include "PityUnit.hh" #include "iostream" +#include "../../../src/std_utils.hh" +#include +#include namespace pEp { namespace PityTest11 { @@ -10,8 +13,7 @@ namespace pEp { _name{ name }, _root_unit{ nullptr, name, nullptr, this } { for (int i = 0; i < nodeCount; i++) { - auto tmp = std::make_shared(*this, i); - _nodes.emplace_back(tmp); + _nodes.emplace_back(std::make_shared(*this, i)); } } @@ -40,5 +42,66 @@ namespace pEp { return getNodes().at(nr)->getProcessUnit().get(); } + void PityModel::sendMsg(const std::string nodename, const std::string& msg) const + { + pEpLogClass("Address: " + nodename + " msg: " + msg); + bool found = false; + for (auto n : _nodes) { + if (n->getName() == nodename) { + found = true; + Utils::dir_ensure(n->inboxDir()); + std::stringstream filename; + // collision detect + do { + filename << n->inboxDir() << Utils::random_string(97, 122, 16) << ".pitymsg"; + } while (Utils::path_exists(filename.str())); + std::ofstream msgfile = Utils::file_create(filename.str()); + msgfile << msg; + } + } + if (!found) { + throw std::runtime_error("no such nodename: " + nodename); + } + } + + // Non-blocking + // throws underflow_error if inbox empty + std::string PityModel::pollMsg() const + { + pEpLogClass("called"); + std::string ret; + Utils::dir_ensure(own_node->inboxDir()); + auto msg_filenames = Utils::dir_list_files(own_node->inboxDir()); + if (!msg_filenames.empty()) { + std::string msg_filename = msg_filenames.at(0); + std::string msg_path = own_node->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: " + own_node->inboxDir()); + } + + return ret; + } + + std::string PityModel::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/PityModel.hh b/src/PityModel.hh index 887ed6a..480771d 100644 --- a/src/PityModel.hh +++ b/src/PityModel.hh @@ -10,6 +10,23 @@ #include #include +// The Model currently is as follows: +// The Model creates the TestTree using PityUnits. +// When creating the model you specify how many nodes you want +// The Model has a PityUnit rootUnit. +// The Model then creates a list of PityNodes, each containing a PityUnit connected to the rootUnit +// The PityNode, when run will modify the model to represent the perspective of the respective node nr. +// The perspective currently is complete by specifying a node, since there is a 1-1 node/ident relationship currently +// +// ATTENTION - TODO: +// Currently there is a strict 1-1 relationship of nodes and identities. +// One Node has exactly one identity, and this identity is only on this node. +// This needs to be enhanced to be a n-n relationship +// The Transport only addresses nodes, not idents, therefore +// If you have one ident on n nodes, the transport needs to check the model for all nodes the +// ident is on and send the message to all these nodes. +// If you have a node that has n identities, the persepective needs to specify node AND ident. + namespace pEp { namespace PityTest11 { class PityModel { @@ -17,16 +34,22 @@ namespace pEp { PityModel() = delete; PityModel(const std::string& name, int nodeCount); std::string getName() const; - void setName(std::string name) ; + void setName(std::string name); std::vector> getNodes() const; PityUnit& rootUnit(); PityUnit* getNodeUnit(int nr) const; + //Transport + void sendMsg(const std::string nodename, const std::string& msg) const; + std::string pollMsg() const; + std::string receiveMsg(int timeout_msec = 100) const; + PityNode* own_node = nullptr; //internal logging static bool debug_log_enabled; Adapter::pEpLog::pEpLogger logger_debug{ "PityModel", debug_log_enabled }; + private: PityUnit _root_unit; std::vector> _nodes; diff --git a/test/test_model.cc b/test/test_model.cc index f52c757..f886bb4 100644 --- a/test/test_model.cc +++ b/test/test_model.cc @@ -1,12 +1,5 @@ #include "../src/PityUnit.hh" #include "../src/PityModel.hh" -#include "../../framework/utils.hh" -#include "../../../src/std_utils.hh" -#include "../../../src/pEpLog.hh" -#include -#include -#include -#include using namespace std; using namespace pEp::Adapter; @@ -16,14 +9,19 @@ using namespace pEp::PityTest11; void test_node1(const PityUnit& unit) { unit.log(unit.getModel()->getName()); - unit.log(unit.getModel()->own_node->); + unit.log(unit.getModel()->own_node->to_string()); + unit.getModel()->sendMsg("node_2@peptest.org", "Fdsfs"); + + while(true) { + unit.log("MSG RX:" + unit.getModel()->receiveMsg()); + } } int main(int argc, char* argv[]) { PityModel::debug_log_enabled = false; - PityNode::debug_log_enabled = true; + PityNode::debug_log_enabled = false; PityModel model{ "test_model", 3 }; for (auto n : model.getNodes()) {