: bharatvaj/forwarder

commit 8111fda1e88b55ff5e7e6fe0b5569b2a6481d3fb
parent 2ce573810a5e515a59ee8d69fe6a274559b3270e
Author: Bharatvaj H <bharatvaj@yahoo.com>
Date: Thu, 28 Mar 2019 20:16:28 +0530

Network rewiring is delegated to NetworkController
3 files changed, 52 insertions(+), 49 deletions(-)
M
forwarder.cpp
|
23
+----------------------
M
include/Forwarder.hpp
|
27
---------------------------
M
include/NetworkController.hpp
|
51
+++++++++++++++++++++++++++++++++++++++++++++++++++
diff --git a/forwarder.cpp b/forwarder.cpp
@@ -85,28 +85,7 @@ void start(shell4c::Operation op, vector<string> msg) {
     shell.print("Nothing to start, atleast one network is required\n");
     return;
   }
-  for (auto network : nc->activeNetworks) {
-    network.second->setOnDeviceConnected([](Device &d) {
-      Forwarder::addDevice(&d);
-      fmt::print("added device: {}\n", d.address);
-      Device *de = Forwarder::getPairOf(&d);
-      if(de != nullptr){
-        Network *n = de->network;
-        if(n == nullptr){
-          //not assigned
-          return;
-        }
-        std::thread([&](){
-          n->connect(de->address);
-        });
-      }
-    });
-  }
-  for(auto db : Forwarder::bindMap){
-    std::thread([&](){
-        db.first->d->network->listen(db.first->a);
-    });
-  }
+  nc->start();
 }
 
 void bind_(shell4c::Operation op, vector<string> msg) {
diff --git a/include/Forwarder.hpp b/include/Forwarder.hpp
@@ -45,20 +45,10 @@ private:
   }
 
 public:
-  static std::map<BindPair *, BindPair *> bindMap;
 
   Forwarder(Device *lp, Device *rp) : lp(lp), rp(rp) {
     std::cout << "Forwarder Started" << std::endl;
   }
-
-  static void addDevice(Device *d) {
-    std::pair<BindPair *, BindPair *> *pair = findAdd(d);
-    if (pair) {
-      Forwarder *forwarder = new Forwarder(pair->first->d, pair->second->d);
-      forwarders.push_back(forwarder);
-      forwarder->start();
-    }
-  }  
   
   void start() {
     t1 = new std::thread([&]() {

@@ -81,20 +71,6 @@ public:
     });
   }
 
-  static void bind(std::string lb, std::string rb) {
-    bindMap[new BindPair(lb, nullptr)] = new BindPair(rb, nullptr);
-  }
-
-  //todo test
-  static Device *getPairOf(Device *d){
-    for(auto p : bindMap){
-      if(p.first->a == d->address){
-        return p.second->d;
-      }
-    }
-    return nullptr;
-  }
-
 
   // TODO test
   void stop() {

@@ -102,9 +78,6 @@ public:
     t1->join();
     t2->join();
   }
-};
-std::map<BindPair *, BindPair *> Forwarder::bindMap =
-    std::map<BindPair *, BindPair *>();
 std::vector<Forwarder *> Forwarder::forwarders = std::vector<Forwarder *>();
 } // namespace wa
 #endif
diff --git a/include/NetworkController.hpp b/include/NetworkController.hpp
@@ -10,9 +10,19 @@ class NetworkController {
 private:
   std::vector<Forwarder *> forwarders;
 
+  class BindInfo {
+    public:
+    std::string address;
+    Device *device;
+    BindInfo(std::string a, Device *d) : address(a), device(d){
+
+    }
+  };
+
 public:
   std::map<std::string, Network *> networks;
   std::map<std::string, Network *> activeNetworks;
+  std::map<BindInfo *, BindInfo *> bindMap;
   NetworkController() {
     for (Network *n : NetworkIndexer::getNetworks()) {
       if (n->init()) {

@@ -25,6 +35,47 @@ public:
       forwarder->stop();
     }
   }
+
+  void bind(std::string p1, std::string p2){
+    bindMap[new BindInfo(p1, nullptr)] = new BindInfo(p2, nullptr);
+  }
+
+
+
+  //todo test
+   Device *getPairOf(Device *d){
+    for(auto p : bindMap){
+      if(p.first->address == d->address){
+        return p.second->device;
+      }
+    }
+    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
+          return;
+        }
+        std::thread([&](){
+          n->connect(de->address);
+        });
+      }
+    });
+  }
+  for(auto db : bindMap){
+    std::thread([&](){
+        db.first->device->network->listen(db.first->address);
+    });
+  }
+  }
 };
 } // namespace wa
 #endif 
\ No newline at end of file