diff --git a/test/pitytest11/src/PityModel.cc b/test/pitytest11/src/PityModel.cc index 87f42ed..ba468bf 100644 --- a/test/pitytest11/src/PityModel.cc +++ b/test/pitytest11/src/PityModel.cc @@ -4,17 +4,19 @@ #include "../../../src/std_utils.hh" #include #include +#include namespace pEp { namespace PityTest11 { - bool PityModel::debug_log_enabled = true; + bool PityModel::debug_log_enabled = false; PityModel::PityModel(const std::string& name, int nodeCount) : - _name{ name }, _root_unit{ nullptr, name, nullptr, this } + _name{ name }, _unit{ nullptr, name, nullptr, this } { for (int i = 0; i < nodeCount; i++) { _nodes.emplace_back(std::make_shared(*this, i)); } + } std::string PityModel::getName() const @@ -27,19 +29,29 @@ namespace pEp { _name = name; } - std::vector> PityModel::getNodes() const + std::vector> PityModel::nodes() const { return _nodes; } - PityUnit& PityModel::rootUnit() + PityUnit& PityModel::unit() + { + return _unit; + } + + PityUnit* PityModel::unitOfNodeNr(int nr) const + { + return nodes().at(nr)->unit().get(); + } + + PityNode* PityModel::nodeNr(int nr) const { - return _root_unit; + return nodes().at(nr).get(); } - PityUnit* PityModel::getNodeUnit(int nr) const + void PityModel::run() { - return getNodes().at(nr)->getProcessUnit().get(); + unit().run(); } void PityModel::sendMsg(const std::string nodename, const std::string& msg) const @@ -64,6 +76,15 @@ namespace pEp { } } + bool PityModel::hasMsg() const{ + bool ret = false; + pEpLogClass("called"); + Utils::dir_ensure(own_node->inboxDir()); + auto msg_filenames = Utils::dir_list_files(own_node->inboxDir()); + ret = msg_filenames.size() > 0; + return ret; + } + // Non-blocking // throws underflow_error if inbox empty std::string PityModel::pollMsg() const @@ -94,7 +115,7 @@ namespace pEp { try { ret = pollMsg(); retry = false; - } catch (const std::underflow_error &){ + } catch (const std::underflow_error&) { pEpLogClass("polling again in [ms]: " + std::to_string(timeout_msec) + "..."); Utils::sleep_millis(timeout_msec); retry = true; diff --git a/test/pitytest11/src/PityModel.hh b/test/pitytest11/src/PityModel.hh index 480771d..aa9cb8a 100644 --- a/test/pitytest11/src/PityModel.hh +++ b/test/pitytest11/src/PityModel.hh @@ -31,27 +31,38 @@ namespace pEp { namespace PityTest11 { class PityModel { public: + // Constructors PityModel() = delete; PityModel(const std::string& name, int nodeCount); + + // Getters std::string getName() const; + std::vector> nodes() const; + PityUnit& unit(); + PityUnit* unitOfNodeNr(int nr) const; + PityNode* nodeNr(int nr) const; + + // Setter void setName(std::string name); - std::vector> getNodes() const; - PityUnit& rootUnit(); - PityUnit* getNodeUnit(int nr) const; + + // Perspective + PityNode* own_node = nullptr; + + //Run + void run(); //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; - PityNode* own_node = nullptr; - //internal logging static bool debug_log_enabled; Adapter::pEpLog::pEpLogger logger_debug{ "PityModel", debug_log_enabled }; private: - PityUnit _root_unit; + PityUnit _unit; std::vector> _nodes; std::string _name; diff --git a/test/pitytest11/src/PityNode.cc b/test/pitytest11/src/PityNode.cc index 3f3a580..03434e6 100644 --- a/test/pitytest11/src/PityNode.cc +++ b/test/pitytest11/src/PityNode.cc @@ -16,22 +16,33 @@ namespace pEp { logger_debug.set_instancename(getName()); std::stringstream ss{}; ss << this; - pEpLogClass(std::string("called with: " + std::to_string(_node_nr) + "AT: " +ss.str())); + pEpLogClass(std::string("called with: " + std::to_string(_node_nr) + "AT: " + ss.str())); - _process_unit = std::make_shared>( - &(model.rootUnit()), + _unit = std::make_shared>( + &(model.unit()), getName(), - std::bind(&PityNode::_init,this, std::placeholders::_1), + std::bind(&PityNode::_init, this, std::placeholders::_1), &model, PityUnit::ExecutionMode::PROCESS_PARALLEL); - } + // We NEED to customize (perspective) the model here + // This will be executed in the new process void PityNode::_init(const PityUnit& unit) { unit.log("NODE INIT - " + getName()); unit.getModel()->own_node = this; unit.getModel()->setName("Copy for:" + getName()); + + _partnerAlgo_NextCircle(); + + // Create peers, everyone but me + auto nodes = _unit->getModel()->nodes(); + for (int i = 0; i < nodes.size(); i++) { + if (i != _node_nr) { + peers.push_back(nodes.at(i)->getName()); + } + } unit.log("NODE INIT DONE"); } @@ -45,18 +56,24 @@ namespace pEp { std::string PityNode::to_string() const { std::string ret{}; - ret += "name: " +getName(); + ret += "name: " + getName(); return ret; } - const std::shared_ptr>& PityNode::getProcessUnit() const + const std::shared_ptr>& PityNode::unit() const { - return _process_unit; + return _unit; } - std::string PityNode::inboxDir() const { - return getProcessUnit()->processDir() + "inbox/"; + std::string PityNode::inboxDir() const + { + return unit()->processDir() + "inbox/"; } + void PityNode::_partnerAlgo_NextCircle() { + // Default partner is next node, its a circle + int partner_node_index = (_node_nr+1) % _unit->getModel()->nodes().size(); + partner = unit()->getModel()->nodes().at(partner_node_index)->getName(); + } } // namespace PityTest11 } // namespace pEp diff --git a/test/pitytest11/src/PityNode.hh b/test/pitytest11/src/PityNode.hh index a9b79a0..5d7ce50 100644 --- a/test/pitytest11/src/PityNode.hh +++ b/test/pitytest11/src/PityNode.hh @@ -13,20 +13,34 @@ namespace pEp { class PityModel; class PityNode { public: + // Constructors PityNode() = delete; explicit PityNode(PityModel& model, int nodeNr); + + // Getters std::string getName() const; std::string to_string() const; - const std::shared_ptr>& getProcessUnit() const; + const std::shared_ptr>& unit() const; std::string inboxDir() const; + + // Perspective + std::string partner; + std::vector peers; + + //internal logging static bool debug_log_enabled; Adapter::pEpLog::pEpLogger logger_debug{ "PityNode", debug_log_enabled }; private: - void _init(const PityUnit& unit); + //fields const int _node_nr; - std::shared_ptr> _process_unit; + std::shared_ptr> _unit; + + // methods + void _init(const PityUnit& unit); + void _partnerAlgo_NextCircle(); + //internal logging Adapter::pEpLog::pEpLogger& m4gic_logger_n4me = logger_debug; diff --git a/test/pitytest11/src/PityUnit.hxx b/test/pitytest11/src/PityUnit.hxx index c0307de..00fb93c 100644 --- a/test/pitytest11/src/PityUnit.hxx +++ b/test/pitytest11/src/PityUnit.hxx @@ -432,7 +432,8 @@ namespace pEp { template Utils::Color PityUnit::_colForProcUnitNr(int procUnitNr) const { - switch (procUnitNr) { + int nrColors = 7; + switch (procUnitNr % nrColors) { case 0: return Utils::Color::WHITE; case 1: diff --git a/test/pitytest11/test/test_transport.cc b/test/pitytest11/test/test_transport.cc index 71c8d0e..790c7e5 100644 --- a/test/pitytest11/test/test_transport.cc +++ b/test/pitytest11/test/test_transport.cc @@ -1,59 +1,59 @@ #include "../src/PityUnit.hh" +#include "../src/PityModel.hh" #include "../../../src/std_utils.hh" -#include -#include -#include -using namespace std; using namespace pEp; +using namespace pEp::Adapter; using namespace pEp::PityTest11; -void send(const PityUnit<>& myself) +void test_node1(const PityUnit& unit) { - setenv("HOME", myself.processDir().c_str(), 1); - myself.log("HOME=" + string(getenv("HOME"))); - ofstream msgfile = Utils::file_create(myself.processDir() + "/transport.msg"); - msgfile << "G4rbage" << endl; - msgfile.close(); - Utils::sleep_millis(400000); -} - -void receive(const PityUnit<>& myself) -{ - setenv("HOME", myself.processDir().c_str(), 1); - myself.log("HOME=" + string(getenv("HOME"))); -// Utils::dir_list_files() - Utils::sleep_millis(400000); + unit.log("ModelName:" + unit.getModel()->getName()); + unit.log("own_node:" + unit.getModel()->own_node->getName()); + unit.log("partner:" + unit.getModel()->own_node->partner); + unit.log("peers:\n" + Utils::to_string(unit.getModel()->own_node->peers)); + + std::string msg = "Message from: " + unit.getPathShort(); + int throttle = 2000; + while (true) { + Utils::sleep_millis(throttle); + for (auto peer : unit.getModel()->own_node->peers) { + unit.log("sending to:" + peer); + unit.getModel()->sendMsg(peer, msg); + Utils::sleep_millis(throttle); + } + + while (unit.getModel()->hasMsg()) { + unit.log("MSG RX:" + unit.getModel()->receiveMsg()); + Utils::sleep_millis(throttle); + } + } } int main(int argc, char* argv[]) { + // debug log per class + PityModel::debug_log_enabled = false; + PityNode::debug_log_enabled = false; PityUnit<>::debug_log_enabled = false; - PityUnit<> root = PityUnit<>{ nullptr, "test_transport" }; - - // 1 - PityUnit<> node1 = PityUnit<>{ &root, - "node 1", - [](const PityUnit<>& mynode) { - - }, - nullptr, - pEp::PityTest11::PityUnit<>::ExecutionMode::PROCESS_PARALLEL }; - - PityUnit<> node1_send = PityUnit<>{ &node1, "send", &send }; - - - // 2 - PityUnit<> node2 = PityUnit<>{ &root, - "node 2", - [](const PityUnit<>& mynode) { - - }, - nullptr, - pEp::PityTest11::PityUnit<>::ExecutionMode::PROCESS_PARALLEL }; - - PityUnit<> node2_receive = PityUnit<>{ &node2, "receive", &receive }; - // root._init(); - root.run(); + // Create model with 3 nodes + PityModel model{ "test_transport", 3 }; + + // //Configure model + // model.nodeNr(0)->partner = model.nodeNr(1)->getName(); + // model.nodeNr(1)->partner = model.nodeNr(2)->getName(); + // model.nodeNr(2)->partner = model.nodeNr(0)->getName(); + + PityUnit node1_test1 = PityUnit{ model.unitOfNodeNr(0), + "test1", + &test_node1 }; + PityUnit node2_test1 = PityUnit{ model.unitOfNodeNr(1), + "test1", + &test_node1 }; + PityUnit node3_test1 = PityUnit{ model.unitOfNodeNr(2), + "test1", + &test_node1 }; + + model.run(); } \ No newline at end of file