Browse Source

Tests: PityTest - PitySwarm singletyped tree

master
heck 4 years ago
parent
commit
8688fcfe46
  1. 24
      src/PitySwarm.cc
  2. 9
      src/PitySwarm.hh
  3. 33
      test/test_swarm.cc

24
src/PitySwarm.cc

@ -1,6 +1,7 @@
#include "PitySwarm.hh"
#include "PityModel.hh" #include "PityModel.hh"
#include "PityPerspective.hh"
#include "PityUnit.hh" #include "PityUnit.hh"
#include "PitySwarm.hh"
#include <vector> #include <vector>
#include <functional> #include <functional>
@ -10,6 +11,7 @@ namespace pEp {
namespace PityTest11 { namespace PityTest11 {
bool PitySwarm::debug_log_enabled = false; bool PitySwarm::debug_log_enabled = false;
// The perspective currently is complete defined by specifying a node, since there is a 1-1 node/ident relationship currently
void createPerspective(const PityModel& model, PityPerspective* psp, int node_nr) void createPerspective(const PityModel& model, PityPerspective* psp, int node_nr)
{ {
psp->name = model.nodeNr(node_nr)->getName(); psp->name = model.nodeNr(node_nr)->getName();
@ -32,20 +34,20 @@ namespace pEp {
pEpLogClass("called"); pEpLogClass("called");
// Create perspective // Create perspective
for (auto n : _model.nodes()) { for (auto n : _model.nodes()) {
auto tmp = std::make_shared<PityPerspective>(); auto tmp = std::make_shared<PityPerspective>(model);
createPerspective(_model, tmp.get(), n->getNr()); createPerspective(_model, tmp.get(), n->getNr());
_perspectives.push_back(tmp); _perspectives.push_back(tmp);
} }
// Construct Tree // Construct Tree
_rootUnit = std::make_shared<PityUnit<PityModel, PityPerspective>>( _rootUnit = std::make_shared<PityUnit<PityPerspective>>(
nullptr, nullptr,
_model.getName(), _model.getName(),
nullptr, nullptr,
&_model); nullptr);
for (auto n : _model.nodes()) { for (auto n : _model.nodes()) {
_nodeUnits.push_back(std::make_shared<PityUnit<>>( _nodeUnits.push_back(std::make_shared<PityUnit<PityPerspective>>(
_rootUnit.get(), _rootUnit.get(),
n->getName(), n->getName(),
nullptr, nullptr,
@ -55,29 +57,23 @@ namespace pEp {
// std::placeholders::_1, // std::placeholders::_1,
// std::placeholders::_2, // std::placeholders::_2,
// std::placeholders::_3), // std::placeholders::_3),
&_model,
_perspectives.at(n->getNr()).get(), _perspectives.at(n->getNr()).get(),
PityUnit<>::ExecutionMode::PROCESS_PARALLEL)); PityUnit<PityPerspective>::ExecutionMode::PROCESS_PARALLEL));
} }
} }
void PitySwarm::addTestUnit( void PitySwarm::addTestUnit(
int nodeNr, int nodeNr,
const std::string& name, const std::string& name,
std::function<void(PityUnit<>&, PityModel*, PityPerspective*)> test_func) PityUnit<PityPerspective>::TestFunction test_func)
{ {
auto tmp = std::make_shared <PityUnit<>>(_nodeUnits.at(nodeNr).get(), name, test_func); auto tmp = std::make_shared<PityUnit<PityPerspective>>(_nodeUnits.at(nodeNr).get(), name, test_func);
_testUnits.push_back(tmp); _testUnits.push_back(tmp);
} }
void PitySwarm::run() void PitySwarm::run()
{ {
_rootUnit->run(); _rootUnit->run();
} }
} // namespace PityTest11 } // namespace PityTest11
} // namespace pEp } // namespace pEp

9
src/PitySwarm.hh

@ -6,6 +6,7 @@
#include "PityModel.hh" #include "PityModel.hh"
#include "PityUnit.hh" #include "PityUnit.hh"
#include "PityPerspective.hh"
#include "../../../src/pEpLog.hh" #include "../../../src/pEpLog.hh"
#include <vector> #include <vector>
#include <memory> #include <memory>
@ -21,7 +22,7 @@ namespace pEp {
void addTestUnit( void addTestUnit(
int nodeNr, int nodeNr,
const std::string& name, const std::string& name,
std::function<void(PityUnit<>&, PityModel*, PityPerspective*)> test_func); PityUnit<PityPerspective>::TestFunction test_func);
//Run //Run
void run(); void run();
@ -32,9 +33,9 @@ namespace pEp {
private: private:
PityModel& _model; PityModel& _model;
std::shared_ptr<PityUnit<>> _rootUnit; std::shared_ptr<PityUnit<PityPerspective>> _rootUnit;
std::vector<std::shared_ptr<PityUnit<>>> _nodeUnits; std::vector<std::shared_ptr<PityUnit<PityPerspective>>> _nodeUnits;
std::vector<std::shared_ptr<PityUnit<>>> _testUnits; std::vector<std::shared_ptr<PityUnit<PityPerspective>>> _testUnits;
std::vector<std::shared_ptr<PityPerspective>> _perspectives; std::vector<std::shared_ptr<PityPerspective>> _perspectives;
//internal logging //internal logging

33
test/test_swarm.cc

@ -1,42 +1,41 @@
#include "../src/PityUnit.hh" #include "../src/PityUnit.hh"
#include "../src/PityModel.hh" #include "../src/PityModel.hh"
#include "../../../src/std_utils.hh" #include "../src/PitySwarm.hh"
#include "../src/PityPerspective.hh"
using namespace pEp; using namespace pEp;
using namespace pEp::Adapter; using namespace pEp::Adapter;
using namespace pEp::PityTest11; using namespace pEp::PityTest11;
void test_node1(const PityUnit<PityModel>& unit) void test_node1(PityUnit<PityPerspective>& unit, PityPerspective* psp)
{ {
unit.log("ModelName:" + unit.getModel()->getName()); unit.log("ModelName:" + psp->model.getName());
unit.log("own_node:" + unit.getModel()->own_node->getName()); unit.log("perspective name:" + psp->name);
unit.log("partner:" + unit.getModel()->own_node->partner); unit.log("perspective partner:" + psp->partner);
std::string msg = "Message from: " + unit.getPathShort(); std::string msg = "Message from: " + unit.getPathShort();
int throttle = 10; int throttle = 1000;
while (true) { while (true) {
Utils::sleep_millis(throttle); Utils::sleep_millis(throttle);
for (auto peer : unit.getModel()->own_node->peers) { for (auto peer : unit.transportEndpoints()) {
unit.getModel()->sendMsg(peer, msg); unit.transport()->sendMsg(peer.first,msg);
Utils::sleep_millis(throttle);
} }
while (unit.getModel()->hasMsg()) { while (unit.transport()->hasMsg()) {
unit.log("MSG RX:" + unit.getModel()->receiveMsg()); unit.log("MSG RX:" + unit.transport()->receiveMsg());
Utils::sleep_millis(throttle);
} }
} }
} }
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
int nodesCount = 64; int nodesCount = 3;
PityModel model{ "test_swarm", nodesCount }; PityModel model{ "test_swarm", nodesCount };
PitySwarm swarm{model};
std::vector<std::shared_ptr<PityUnit<PityModel>>> nodeUnits; for(int i = 0; i < nodesCount; i++) {
for (int i = 0; i < nodesCount; i++) { swarm.addTestUnit(i,"test1",&test_node1);
nodeUnits.emplace_back(std::make_shared<PityUnit<PityModel>>(model.unitOfNodeNr(i), "test1", &test_node1 ));
} }
model.run(); swarm.run();
} }
Loading…
Cancel
Save