Browse Source

Test: PityTest - Add Perspective peers/partner etc...

master
heck 4 years ago
parent
commit
84e8300344
  1. 37
      src/PityModel.cc
  2. 23
      src/PityModel.hh
  3. 37
      src/PityNode.cc
  4. 20
      src/PityNode.hh
  5. 3
      src/PityUnit.hxx
  6. 90
      test/test_transport.cc

37
src/PityModel.cc

@ -4,17 +4,19 @@
#include "../../../src/std_utils.hh"
#include <random>
#include <fstream>
#include <memory>
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<PityNode>(*this, i));
}
}
std::string PityModel::getName() const
@ -27,19 +29,29 @@ namespace pEp {
_name = name;
}
std::vector<std::shared_ptr<PityNode>> PityModel::getNodes() const
std::vector<std::shared_ptr<PityNode>> PityModel::nodes() const
{
return _nodes;
}
PityUnit<PityModel>& PityModel::rootUnit()
PityUnit<PityModel>& PityModel::unit()
{
return _unit;
}
PityUnit<PityModel>* 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>* 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;

23
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<std::shared_ptr<PityNode>> nodes() const;
PityUnit<PityModel>& unit();
PityUnit<PityModel>* unitOfNodeNr(int nr) const;
PityNode* nodeNr(int nr) const;
// Setter
void setName(std::string name);
std::vector<std::shared_ptr<PityNode>> getNodes() const;
PityUnit<PityModel>& rootUnit();
PityUnit<PityModel>* 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<PityModel> _root_unit;
PityUnit<PityModel> _unit;
std::vector<std::shared_ptr<PityNode>> _nodes;
std::string _name;

37
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<PityUnit<PityModel>>(
&(model.rootUnit()),
_unit = std::make_shared<PityUnit<PityModel>>(
&(model.unit()),
getName(),
std::bind(&PityNode::_init,this, std::placeholders::_1),
std::bind(&PityNode::_init, this, std::placeholders::_1),
&model,
PityUnit<PityModel>::ExecutionMode::PROCESS_PARALLEL);
}
// We NEED to customize (perspective) the model here
// This will be executed in the new process
void PityNode::_init(const PityUnit<PityModel>& 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<PityUnit<PityModel>>& PityNode::getProcessUnit() const
const std::shared_ptr<PityUnit<PityModel>>& 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

20
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<PityUnit<PityModel>>& getProcessUnit() const;
const std::shared_ptr<PityUnit<PityModel>>& unit() const;
std::string inboxDir() const;
// Perspective
std::string partner;
std::vector<std::string> peers;
//internal logging
static bool debug_log_enabled;
Adapter::pEpLog::pEpLogger logger_debug{ "PityNode", debug_log_enabled };
private:
void _init(const PityUnit<PityModel>& unit);
//fields
const int _node_nr;
std::shared_ptr<PityUnit<PityModel>> _process_unit;
std::shared_ptr<PityUnit<PityModel>> _unit;
// methods
void _init(const PityUnit<PityModel>& unit);
void _partnerAlgo_NextCircle();
//internal logging
Adapter::pEpLog::pEpLogger& m4gic_logger_n4me = logger_debug;

3
src/PityUnit.hxx

@ -432,7 +432,8 @@ namespace pEp {
template<class T>
Utils::Color PityUnit<T>::_colForProcUnitNr(int procUnitNr) const
{
switch (procUnitNr) {
int nrColors = 7;
switch (procUnitNr % nrColors) {
case 0:
return Utils::Color::WHITE;
case 1:

90
test/test_transport.cc

@ -1,59 +1,59 @@
#include "../src/PityUnit.hh"
#include "../src/PityModel.hh"
#include "../../../src/std_utils.hh"
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
using namespace pEp;
using namespace pEp::Adapter;
using namespace pEp::PityTest11;
void send(const PityUnit<>& myself)
void test_node1(const PityUnit<PityModel>& 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<PityModel> node1_test1 = PityUnit<PityModel>{ model.unitOfNodeNr(0),
"test1",
&test_node1 };
PityUnit<PityModel> node2_test1 = PityUnit<PityModel>{ model.unitOfNodeNr(1),
"test1",
&test_node1 };
PityUnit<PityModel> node3_test1 = PityUnit<PityModel>{ model.unitOfNodeNr(2),
"test1",
&test_node1 };
model.run();
}
Loading…
Cancel
Save