Test: PitySwarm - Adjust ownership model for new PityTree

LIB-11
heck 2 years ago
parent 199581f4d4
commit d1d4194646

@ -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<PityPerspective>(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<TestUnit>(
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<int, TestUnit*>(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<PityPerspective>& 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<PityPerspective>(model);
_createPerspective(_model, tmp.get(), n->getNr());
_perspectives.push_back(tmp);
}
// Construct Tree
_rootUnit = std::make_shared<PityUnit<PityPerspective>>(
_model.getName(),
nullptr,
nullptr);
for (auto n : _model.nodes()) {
auto tmp = std::make_shared<PityUnit<PityPerspective>>(
*_rootUnit.get(),
n->getName(),
std::bind(&PitySwarm::_init_process,this, std::placeholders::_1, std::placeholders::_2),
_perspectives.at(n->getNr()).get(),
PityUnit<PityPerspective>::ExecutionMode::PROCESS_PARALLEL);
_nodeUnits.push_back(tmp);
}
}
PityUnit<PityPerspective>* PitySwarm::addTestUnit(
int nodeNr,
const std::string& name,
PityUnit<PityPerspective>::TestFunction test_func)
{
std::shared_ptr<PityUnit<PityPerspective>> tmp = std::make_shared<PityUnit<PityPerspective>>(
*_nodeUnits.at(nodeNr).get(),
name,
test_func);
_testUnits.push_back(tmp);
return tmp.get();
}
void PitySwarm::run()
{
_rootUnit->run();
}
} // namespace PityTest11
} // namespace pEp

@ -12,21 +12,25 @@
#include <memory>
#include <functional>
// 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<PityPerspective>;
// Constructors
PitySwarm(PityModel& model);
PityUnit<PityPerspective>* addTestUnit(
int nodeNr,
const std::string& name,
PityUnit<PityPerspective>::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<PityPerspective>& unit, PityPerspective* ctx);
int _init_process(TestUnit& unit, PityPerspective* ctx);
// fields
PityModel& _model;
std::shared_ptr<PityUnit<PityPerspective>> _rootUnit;
std::vector<std::shared_ptr<PityUnit<PityPerspective>>> _nodeUnits;
std::vector<std::shared_ptr<PityUnit<PityPerspective>>> _testUnits;
TestUnit _swarmUnit;
// each node has
std::vector<std::shared_ptr<PityPerspective>> _perspectives;
std::map<int, TestUnit*> _leafunit;
//internal logging
Adapter::pEpLog::pEpLogger& m4gic_logger_n4me = logger_debug;
};

Loading…
Cancel
Save