diff --git a/src/PitySwarm.cc b/src/PitySwarm.cc index 4f185a2..c9a6d3c 100644 --- a/src/PitySwarm.cc +++ b/src/PitySwarm.cc @@ -13,6 +13,51 @@ namespace pEp { namespace PityTest11 { bool PitySwarm::debug_log_enabled = false; + PitySwarm::PitySwarm(const std::string& name, PityModel& model) : + _model{ model }, _swarmUnit(name) + { + pEpLogClass("called"); + // Create perspective + for (auto n : _model.nodes()) { + auto tmp = std::make_shared(model); + _createPerspective(_model, tmp.get(), n->getNr()); + _perspectives.push_back(tmp); + } + + // Construct swarm + _swarmUnit = TestUnit(_model.getName(), nullptr, nullptr); + + for (auto n : _model.nodes()) { + TestUnit* tmp = &_swarmUnit.addNew( + n->getName(), + std::bind( + &PitySwarm::_init_process, + this, + std::placeholders::_1, + std::placeholders::_2), + _perspectives.at(n->getNr()).get(), + TestUnit::ExecutionMode::PROCESS_PARALLEL); + _leafunit.insert(std::pair(n->getNr(), tmp)); + } + } + + PitySwarm::TestUnit& PitySwarm::getSwarmUnit() + { + return _swarmUnit; + } + + PitySwarm::TestUnit& PitySwarm::addTestUnit(int nodeNr, const TestUnit& unit) + { + TestUnit& ret = _leafunit.at(nodeNr)->addCopy(std::move(unit)); + _leafunit.at(nodeNr) = &ret; + return ret; + } + + void PitySwarm::run() + { + _swarmUnit.run(); + } + // The perspective currently is complete defined by specifying a node, since there is a 1-1 node/ident relationship currently void PitySwarm::_createPerspective(const PityModel& model, PityPerspective* psp, int node_nr) { @@ -41,59 +86,14 @@ namespace pEp { } } - int PitySwarm::_init_process(PityUnit& unit, PityPerspective* ctx) + int PitySwarm::_init_process(TestUnit& unit, PityPerspective* ctx) { - std::cout << "Node _init, setting $HOME" << std::endl; + std::cout << "Node _initProcUnitNrRecurse, setting $HOME" << std::endl; std::string home = unit.processDir(); setenv("HOME", home.c_str(), true); return 0; } - PitySwarm::PitySwarm(PityModel& model) : _model{ model } - { - pEpLogClass("called"); - // Create perspective - for (auto n : _model.nodes()) { - auto tmp = std::make_shared(model); - _createPerspective(_model, tmp.get(), n->getNr()); - _perspectives.push_back(tmp); - } - - // Construct Tree - _rootUnit = std::make_shared>( - _model.getName(), - nullptr, - nullptr); - - for (auto n : _model.nodes()) { - - auto tmp = std::make_shared>( - *_rootUnit.get(), - n->getName(), - std::bind(&PitySwarm::_init_process,this, std::placeholders::_1, std::placeholders::_2), - _perspectives.at(n->getNr()).get(), - PityUnit::ExecutionMode::PROCESS_PARALLEL); - - _nodeUnits.push_back(tmp); - } - } - - PityUnit* PitySwarm::addTestUnit( - int nodeNr, - const std::string& name, - PityUnit::TestFunction test_func) - { - std::shared_ptr> tmp = std::make_shared>( - *_nodeUnits.at(nodeNr).get(), - name, - test_func); - _testUnits.push_back(tmp); - return tmp.get(); - } - void PitySwarm::run() - { - _rootUnit->run(); - } } // namespace PityTest11 } // namespace pEp diff --git a/src/PitySwarm.hh b/src/PitySwarm.hh index cff8cb6..729239e 100644 --- a/src/PitySwarm.hh +++ b/src/PitySwarm.hh @@ -12,21 +12,25 @@ #include #include +// PitySwarm creates a swarm of independent process nodes. +// Each node has its own perspective +// The perspective is a derivate of the model +// The model is the objective reality +// The perspective is the subjective reality + namespace pEp { namespace PityTest11 { class PitySwarm { public: + using TestUnit = PityUnit; // Constructors - PitySwarm(PityModel& model); - - PityUnit* addTestUnit( - int nodeNr, - const std::string& name, - PityUnit::TestFunction test_func); + explicit PitySwarm(const std::string& name, PityModel& model); - //Run + TestUnit& addTestUnit(int nodeNr, const TestUnit& unit); void run(); + TestUnit& getSwarmUnit(); + //internal logging static bool debug_log_enabled; Adapter::pEpLog::pEpLogger logger_debug{ "PityNode", debug_log_enabled }; @@ -34,15 +38,14 @@ namespace pEp { private: // methods void _createPerspective(const PityModel& model, PityPerspective* psp, int node_nr); - int _init_process(PityUnit& unit, PityPerspective* ctx); + int _init_process(TestUnit& unit, PityPerspective* ctx); // fields PityModel& _model; - std::shared_ptr> _rootUnit; - std::vector>> _nodeUnits; - std::vector>> _testUnits; + TestUnit _swarmUnit; + // each node has std::vector> _perspectives; - + std::map _leafunit; //internal logging Adapter::pEpLog::pEpLogger& m4gic_logger_n4me = logger_debug; };