
6 changed files with 221 additions and 155 deletions
@ -0,0 +1,83 @@ |
|||||
|
#include "PityModel.hh" |
||||
|
#include "PityUnit.hh" |
||||
|
#include "PitySwarm.hh" |
||||
|
|
||||
|
#include <vector> |
||||
|
#include <functional> |
||||
|
#include <memory> |
||||
|
|
||||
|
namespace pEp { |
||||
|
namespace PityTest11 { |
||||
|
bool PitySwarm::debug_log_enabled = false; |
||||
|
|
||||
|
void createPerspective(const PityModel& model, PityPerspective* psp, int node_nr) |
||||
|
{ |
||||
|
psp->name = model.nodeNr(node_nr)->getName(); |
||||
|
|
||||
|
// Default partner is next node, its a circle
|
||||
|
int partner_node_index = (node_nr + 1) % model.nodes().size(); |
||||
|
psp->partner = model.nodes().at(partner_node_index)->getName(); |
||||
|
|
||||
|
// Create peers, everyone but me
|
||||
|
auto nodes = model.nodes(); |
||||
|
for (int i = 0; i < nodes.size(); i++) { |
||||
|
if (i != node_nr) { |
||||
|
psp->peers.push_back(nodes.at(i)->getName()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
PitySwarm::PitySwarm(PityModel& model) : _model{ model } |
||||
|
{ |
||||
|
pEpLogClass("called"); |
||||
|
// Create perspective
|
||||
|
for (auto n : _model.nodes()) { |
||||
|
auto tmp = std::make_shared<PityPerspective>(); |
||||
|
createPerspective(_model, tmp.get(), n->getNr()); |
||||
|
_perspectives.push_back(tmp); |
||||
|
} |
||||
|
|
||||
|
// Construct Tree
|
||||
|
_rootUnit = std::make_shared<PityUnit<PityModel, PityPerspective>>( |
||||
|
nullptr, |
||||
|
_model.getName(), |
||||
|
nullptr, |
||||
|
&_model); |
||||
|
|
||||
|
for (auto n : _model.nodes()) { |
||||
|
_nodeUnits.push_back(std::make_shared<PityUnit<>>( |
||||
|
_rootUnit.get(), |
||||
|
n->getName(), |
||||
|
nullptr, |
||||
|
// std::bind(
|
||||
|
// &PityNode::_init,
|
||||
|
// this,
|
||||
|
// std::placeholders::_1,
|
||||
|
// std::placeholders::_2,
|
||||
|
// std::placeholders::_3),
|
||||
|
&_model, |
||||
|
_perspectives.at(n->getNr()).get(), |
||||
|
PityUnit<>::ExecutionMode::PROCESS_PARALLEL)); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void PitySwarm::addTestUnit( |
||||
|
int nodeNr, |
||||
|
const std::string& name, |
||||
|
std::function<void(PityUnit<>&, PityModel*, PityPerspective*)> test_func) |
||||
|
{ |
||||
|
auto tmp = std::make_shared <PityUnit<>>(_nodeUnits.at(nodeNr).get(), name, test_func); |
||||
|
_testUnits.push_back(tmp); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
void PitySwarm::run() |
||||
|
{ |
||||
|
|
||||
|
_rootUnit->run(); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} // namespace PityTest11
|
||||
|
} // namespace pEp
|
@ -0,0 +1,46 @@ |
|||||
|
// This file is under GNU General Public License 3.0
|
||||
|
// see LICENSE.txt
|
||||
|
|
||||
|
#ifndef PITYTEST_PITYSWARM_HH |
||||
|
#define PITYTEST_PITYSWARM_HH |
||||
|
|
||||
|
#include "PityModel.hh" |
||||
|
#include "PityUnit.hh" |
||||
|
#include "../../../src/pEpLog.hh" |
||||
|
#include <vector> |
||||
|
#include <memory> |
||||
|
#include <functional> |
||||
|
|
||||
|
namespace pEp { |
||||
|
namespace PityTest11 { |
||||
|
class PitySwarm { |
||||
|
public: |
||||
|
// Constructors
|
||||
|
PitySwarm(PityModel& model); |
||||
|
|
||||
|
void addTestUnit( |
||||
|
int nodeNr, |
||||
|
const std::string& name, |
||||
|
std::function<void(PityUnit<>&, PityModel*, PityPerspective*)> test_func); |
||||
|
|
||||
|
//Run
|
||||
|
void run(); |
||||
|
|
||||
|
//internal logging
|
||||
|
static bool debug_log_enabled; |
||||
|
Adapter::pEpLog::pEpLogger logger_debug{ "PityNode", debug_log_enabled }; |
||||
|
|
||||
|
private: |
||||
|
PityModel& _model; |
||||
|
std::shared_ptr<PityUnit<>> _rootUnit; |
||||
|
std::vector<std::shared_ptr<PityUnit<>>> _nodeUnits; |
||||
|
std::vector<std::shared_ptr<PityUnit<>>> _testUnits; |
||||
|
std::vector<std::shared_ptr<PityPerspective>> _perspectives; |
||||
|
|
||||
|
//internal logging
|
||||
|
Adapter::pEpLog::pEpLogger& m4gic_logger_n4me = logger_debug; |
||||
|
}; |
||||
|
}; // namespace PityTest11
|
||||
|
}; // namespace pEp
|
||||
|
|
||||
|
#endif // PITYTEST_PITYSWARM_HH
|
Loading…
Reference in new issue