commit e451bd70b9062b9ec7897679eb013169f3ba67d1
parent 8111fda1e88b55ff5e7e6fe0b5569b2a6481d3fb
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Thu, 28 Mar 2019 20:53:13 +0000
parent 8111fda1e88b55ff5e7e6fe0b5569b2a6481d3fb
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Thu, 28 Mar 2019 20:53:13 +0000
Listen connect mode updated
3 files changed, 60 insertions(+), 74 deletions(-)
M
|
81
+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
diff --git a/forwarder.cpp b/forwarder.cpp @@ -89,7 +89,7 @@ void start(shell4c::Operation op, vector<string> msg) { } void bind_(shell4c::Operation op, vector<string> msg) { - if (msg.size() < 2) { + if (msg.size() < 4) { shell.println(op.usage); return; } @@ -105,7 +105,7 @@ void bind_(shell4c::Operation op, vector<string> msg) { shell.println("Operation failed"); return; } - Forwarder::bind(msg[0], msg[1]); + nc->bind(msg[0], msg[1], msg[2], msg[3]); } } @@ -123,15 +123,18 @@ void help(shell4c::Operation op, vector<string> messages) { auto ops = shell.getOperations(); if (messages.size() == 0) { for (auto op : ops) { - if(op.second == nullptr)continue; - shell.println(fmt::format("\u001b[33m{}\u001b[0m\t- {}", op.first, op.second->description)); + if (op.second == nullptr) + continue; + shell.println(fmt::format("\u001b[33m{}\u001b[0m\t- {}", op.first, + op.second->description)); } return; } for (auto msg : messages) { if (ops.find(msg) != ops.end()) { shell4c::Operation *op = ops[msg]; - if(op == nullptr) continue; + if (op == nullptr) + continue; shell.println(op->usage); } }
diff --git a/include/Forwarder.hpp b/include/Forwarder.hpp @@ -9,52 +9,23 @@ #define BUF_SIZE 256 namespace wa { -class BindPair { -public: - std::string a; - Device *d; - BindPair(std::string a, Device *d) : a(a), d(d) {} -}; class Forwarder { private: Device *lp, *rp; std::thread *t1, *t2; bool shouldStop = false; - static std::vector<Forwarder *> forwarders; - - static std::pair<BindPair *, BindPair *> *findAdd(Device *d) { - for (auto n1 : bindMap) { - if (d->address == n1.first->a) { - n1.first->d = d; - if (n1.second->d == nullptr) { - return nullptr; - } else { - return new std::pair<BindPair *, BindPair *>(n1); - } - } else if (d->address == n1.second->a) { - n1.second->d = d; - if (n1.first->d == nullptr) { - return nullptr; - } else { - return new std::pair<BindPair *, BindPair *>(n1); - } - } - } - return nullptr; - } public: - Forwarder(Device *lp, Device *rp) : lp(lp), rp(rp) { std::cout << "Forwarder Started" << std::endl; } - + void start() { t1 = new std::thread([&]() { char *str = (char *)calloc(sizeof(char), BUF_SIZE); int bytesRead = 0; - while ((bytesRead = lp->read(str, BUF_SIZE-1)) > 0) { + while ((bytesRead = lp->read(str, BUF_SIZE - 1)) > 0) { if (rp->write(str, bytesRead) < 0) { break; }; @@ -63,7 +34,7 @@ public: t2 = new std::thread([&]() { char *str = (char *)calloc(sizeof(char), BUF_SIZE); int bytesRead = 0; - while ((bytesRead = rp->read(str, BUF_SIZE-1)) > 0) { + while ((bytesRead = rp->read(str, BUF_SIZE - 1)) > 0) { if (lp->write(str, bytesRead) < 0) { break; } @@ -71,13 +42,14 @@ public: }); } - // TODO test void stop() { shouldStop = true; t1->join(); t2->join(); } -std::vector<Forwarder *> Forwarder::forwarders = std::vector<Forwarder *>(); + // std::vector<Forwarder *> Forwarder::forwarders = std::vector<Forwarder + // *>(); +}; } // namespace wa #endif
diff --git a/include/NetworkController.hpp b/include/NetworkController.hpp @@ -11,12 +11,11 @@ private: std::vector<Forwarder *> forwarders; class BindInfo { - public: - std::string address; + public: + std::string networkName; + std::string params; Device *device; - BindInfo(std::string a, Device *d) : address(a), device(d){ - - } + BindInfo() {} }; public: @@ -36,45 +35,57 @@ public: } } - void bind(std::string p1, std::string p2){ - bindMap[new BindInfo(p1, nullptr)] = new BindInfo(p2, nullptr); - } - + void bind(BindInfo *b1, BindInfo *b2) { bindMap[b1] = b2; } + void bind(std::string n1, std::string cp1, std::string n2, std::string cp2) { + BindInfo *bi1 = new BindInfo(); + bi1->networkName = n1; + bi1->params = cp1; + BindInfo *bi2 = new BindInfo(); + bi2->networkName = n2; + bi2->params = cp2; + bind(bi1, bi2); + } - //todo test - Device *getPairOf(Device *d){ - for(auto p : bindMap){ - if(p.first->address == d->address){ - return p.second->device; + // todo test + BindInfo *getPairOf(Device *d) { + for (auto p : bindMap) { + if (p.first->params == d->address) { + return p.second; } } return nullptr; } - void start(){ - for (auto network : activeNetworks) { - network.second->setOnDeviceConnected([&](Device &d) { - // Forwarder::addDevice(&d); - fmt::print("added device: {}\n", d.address); - Device *de = getPairOf(&d); - if(de != nullptr){ - Network *n = de->network; - if(n == nullptr){ - //not assigned + void start() { + for (auto networkBind : bindMap) { + Network *n = activeNetworks[networkBind.first->networkName]; + if (n == nullptr) { + // TODO ERROR HANDLE + return; + } + std::string nParams = networkBind.first->params; + n->setOnDeviceConnected([&](Device &d) { + BindInfo *bi = getPairOf(&d); + if (bi == nullptr) { + // TODO ERROR HANDLE return; } - std::thread([&](){ - n->connect(de->address); - }); - } - }); - } - for(auto db : bindMap){ - std::thread([&](){ - db.first->device->network->listen(db.first->address); - }); - } + Network *destNetwork = activeNetworks[bi->networkName]; + if (destNetwork == nullptr) { + // TODO ERROR HANDLE + return; + } + Device *d2 = destNetwork->connect(bi->params); + if (d2 == nullptr) { + // TODO ERROR HANDLE + return; + } + Forwarder *forwarder = new Forwarder(&d, d2); + forwarders.push_back(forwarder); + }); + n->listen(nParams); + } } }; } // namespace wa
: bharatvaj/forwarder