Browse Source

Test: PityTest - Add transport

master
heck 4 years ago
parent
commit
fd30abb783
  1. 67
      src/PityModel.cc
  2. 25
      src/PityModel.hh
  3. 16
      test/test_model.cc

67
src/PityModel.cc

@ -1,6 +1,9 @@
#include "PityModel.hh"
//#include "PityUnit.hh"
#include "iostream"
#include "../../../src/std_utils.hh"
#include <random>
#include <fstream>
namespace pEp {
namespace PityTest11 {
@ -10,8 +13,7 @@ namespace pEp {
_name{ name }, _root_unit{ nullptr, name, nullptr, this }
{
for (int i = 0; i < nodeCount; i++) {
auto tmp = std::make_shared<PityNode>(*this, i);
_nodes.emplace_back(tmp);
_nodes.emplace_back(std::make_shared<PityNode>(*this, i));
}
}
@ -40,5 +42,66 @@ namespace pEp {
return getNodes().at(nr)->getProcessUnit().get();
}
void PityModel::sendMsg(const std::string nodename, const std::string& msg) const
{
pEpLogClass("Address: " + nodename + " msg: " + msg);
bool found = false;
for (auto n : _nodes) {
if (n->getName() == nodename) {
found = true;
Utils::dir_ensure(n->inboxDir());
std::stringstream filename;
// collision detect
do {
filename << n->inboxDir() << Utils::random_string(97, 122, 16) << ".pitymsg";
} while (Utils::path_exists(filename.str()));
std::ofstream msgfile = Utils::file_create(filename.str());
msgfile << msg;
}
}
if (!found) {
throw std::runtime_error("no such nodename: " + nodename);
}
}
// Non-blocking
// throws underflow_error if inbox empty
std::string PityModel::pollMsg() const
{
pEpLogClass("called");
std::string ret;
Utils::dir_ensure(own_node->inboxDir());
auto msg_filenames = Utils::dir_list_files(own_node->inboxDir());
if (!msg_filenames.empty()) {
std::string msg_filename = msg_filenames.at(0);
std::string msg_path = own_node->inboxDir() + "/" + msg_filename;
pEpLogClass("Reading file: " + msg_filename);
ret = Utils::file_read(msg_path);
Utils::path_delete(msg_path);
} else {
throw std::underflow_error("inbox empty: " + own_node->inboxDir());
}
return ret;
}
std::string PityModel::receiveMsg(int timeout_msec) const
{
pEpLogClass("called");
std::string ret;
bool retry = false;
do {
try {
ret = pollMsg();
retry = false;
} catch (const std::underflow_error &){
pEpLogClass("polling again in [ms]: " + std::to_string(timeout_msec) + "...");
Utils::sleep_millis(timeout_msec);
retry = true;
}
} while (retry);
return ret;
}
} // namespace PityTest11
} // namespace pEp

25
src/PityModel.hh

@ -10,6 +10,23 @@
#include <vector>
#include <memory>
// The Model currently is as follows:
// The Model creates the TestTree using PityUnits.
// When creating the model you specify how many nodes you want
// The Model has a PityUnit rootUnit.
// The Model then creates a list of PityNodes, each containing a PityUnit connected to the rootUnit
// The PityNode, when run will modify the model to represent the perspective of the respective node nr.
// The perspective currently is complete by specifying a node, since there is a 1-1 node/ident relationship currently
//
// ATTENTION - TODO:
// Currently there is a strict 1-1 relationship of nodes and identities.
// One Node has exactly one identity, and this identity is only on this node.
// This needs to be enhanced to be a n-n relationship
// The Transport only addresses nodes, not idents, therefore
// If you have one ident on n nodes, the transport needs to check the model for all nodes the
// ident is on and send the message to all these nodes.
// If you have a node that has n identities, the persepective needs to specify node AND ident.
namespace pEp {
namespace PityTest11 {
class PityModel {
@ -17,16 +34,22 @@ namespace pEp {
PityModel() = delete;
PityModel(const std::string& name, int nodeCount);
std::string getName() const;
void setName(std::string name) ;
void setName(std::string name);
std::vector<std::shared_ptr<PityNode>> getNodes() const;
PityUnit<PityModel>& rootUnit();
PityUnit<PityModel>* getNodeUnit(int nr) const;
//Transport
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;
std::vector<std::shared_ptr<PityNode>> _nodes;

16
test/test_model.cc

@ -1,12 +1,5 @@
#include "../src/PityUnit.hh"
#include "../src/PityModel.hh"
#include "../../framework/utils.hh"
#include "../../../src/std_utils.hh"
#include "../../../src/pEpLog.hh"
#include <iostream>
#include <fstream>
#include <algorithm>
#include <sstream>
using namespace std;
using namespace pEp::Adapter;
@ -16,14 +9,19 @@ using namespace pEp::PityTest11;
void test_node1(const PityUnit<PityModel>& unit)
{
unit.log(unit.getModel()->getName());
unit.log(unit.getModel()->own_node->);
unit.log(unit.getModel()->own_node->to_string());
unit.getModel()->sendMsg("node_2@peptest.org", "Fdsfs");
while(true) {
unit.log("MSG RX:" + unit.getModel()->receiveMsg());
}
}
int main(int argc, char* argv[])
{
PityModel::debug_log_enabled = false;
PityNode::debug_log_enabled = true;
PityNode::debug_log_enabled = false;
PityModel model{ "test_model", 3 };
for (auto n : model.getNodes()) {

Loading…
Cancel
Save