: bharatvaj/forwarder

commit 21392f0bcccfe211b0b9a0b28b898624640da6a1
parent f319d2db40c7184e2c9236001214ce423b6ddd9a
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Wed, 1 May 2019 22:26:37 +0000

Server starts automatically after bind
4 files changed, 46 insertions(+), 45 deletions(-)
M
Shell.hpp
|
8
+++++---
M
forwarder.cpp
|
10
----------
M
include/NetworkController.hpp
|
71
++++++++++++++++++++++++++++++++++++++++-------------------------------
M
include/network/IP.hpp
|
2
+-
diff --git a/Shell.hpp b/Shell.hpp
@@ -12,7 +12,7 @@ using namespace dot;
 namespace shell4c {
 class Shell {
 private:
-  Dot &d = Dot::getDot(_SHELL_PORT);
+  Dot *d;
   std::map<std::string, Operation *> operations;
   std::function<void(int)> exitHandler = [](int) {};
   std::string shellIcon;

@@ -51,7 +51,9 @@ private:
 public:
   Shell(bool isDaemon, std::string shellIcon)
       : isDaemon(isDaemon), shellIcon(shellIcon) {
-    d.onConnect([=](Dot &c) {
+    if(!isDaemon) return;
+    d = &Dot::getDot(_SHELL_PORT);
+    d->onConnect([=](Dot &c) {
       isConnected = true;
       client = &c;
       print(shellIcon);

@@ -62,7 +64,7 @@ public:
       });
       client->onDisconnect([=](Dot &d) { exitHandler(0); });
     });
-    d.onDisconnect([=](Dot &client) { isConnected = false; });
+    d->onDisconnect([=](Dot &client) { isConnected = false; });
   }
   void addOperation(std::string op,
                     std::function<void(std::vector<std::string>)> opFunc) {
diff --git a/forwarder.cpp b/forwarder.cpp
@@ -81,14 +81,6 @@ void list(shell4c::Operation op, vector<string> msg) {
   }
 }
 
-void start(shell4c::Operation op, vector<string> msg) {
-  if (nc->activeNetworks.size() == 0) {
-    shell.print("Nothing to start, atleast one network is required\n");
-    return;
-  }
-  nc->start();
-}
-
 void bind_(shell4c::Operation op, vector<string> msg) {
   if (msg.size() < 4) {
     shell.println(op.usage);

@@ -143,8 +135,6 @@ int main(int argc, char *argv[]) {
       new shell4c::Operation("clear", "clears the screen", clear, "clear"));
   shell.addOperation(new shell4c::Operation("ls", "list all available network",
                                             list, "ls [network]"));
-  shell.addOperation(
-      new shell4c::Operation("start", "start forwarder", start, "start"));
   shell.addOperation(new shell4c::Operation("help", "prints this menu", help,
                                             "help [operation1 ...]"));
   shell.addOperation(
diff --git a/include/NetworkController.hpp b/include/NetworkController.hpp
@@ -28,6 +28,7 @@ public:
         networks[n->getUserFriendlyName()] = n;
       }
     }
+    activeNetworks = networks;
   }
   void stop() {
     for (auto forwarder : forwarders) {

@@ -37,6 +38,8 @@ public:
 
   void bind(BindInfo *b1, BindInfo *b2) { bindMap[b1] = b2; }
 
+  void unbind(BindInfo *b1) { bindMap.erase(b1); }
+
   // TODO test: consistency, atomic
   void bind(std::string n1, std::string cp1, std::string n2, std::string cp2) {
     Network *net1 = activeNetworks[n1];

@@ -50,12 +53,12 @@ public:
       return;
     }
     if (net1->bind(cp1) < 0) {
-      Log::e(fmt::format("Binding failed for net1"));
+      Log::e(fmt::format("Binding failed: error in param net1"));
       return;
     }
     if (net2->bind(cp2) < 0) {
       // error
-      Log::e(fmt::format("Binding failed for net2"));
+      Log::e(fmt::format("Binding failed: error in param net2"));
       net1->unbind(cp1);
       return;
     }

@@ -65,7 +68,15 @@ public:
     BindInfo *bi2 = new BindInfo();
     bi2->networkName = n2;
     bi2->params = cp2;
+
     bind(bi1, bi2);
+    if (!start(bi1)) {
+      unbind(bi1);
+      //stop server
+      Log::e("Bind unsuccesful");
+      return;
+    }
+    Log::i("Bind successful");
   }
 
   // todo test

@@ -78,38 +89,36 @@ public:
     return nullptr;
   }
 
-  void start() {
-    for (auto networkBind : bindMap) {
-      Network *n = activeNetworks[networkBind.first->networkName];
-      if (n == nullptr) {
+  bool start(BindInfo *bi2) {
+    Network *n = activeNetworks[bi2->networkName];
+    if (n == nullptr) {
+      // TODO ERROR HANDLE
+      return false;
+    }
+    std::string nParams = bi2->params;
+    n->setOnDeviceConnected([&](Device &d) {
+      BindInfo *bi = getPairOf(&d);
+      if (bi == 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;
-        }
-        Network *destNetwork = activeNetworks[bi->networkName];
-        if (destNetwork == nullptr) {
-          // TODO ERROR HANDLE
-          return;
-        }
-        Device *d2 = destNetwork->connect(bi->params);
-        if (d2 == nullptr) {
-	        fmt::print("Destination not found\n");
-          d.disconnect();
-          // TODO ERROR HANDLE
-          return;
-        }
-        Forwarder *forwarder = new Forwarder(&d, d2);
-	forwarder->start();
-        forwarders.push_back(forwarder);
-      });
-      n->listen(nParams);
-    }
+      Network *destNetwork = activeNetworks[bi->networkName];
+      if (destNetwork == nullptr) {
+        // TODO ERROR HANDLE
+        return;
+      }
+      Device *d2 = destNetwork->connect(bi->params);
+      if (d2 == nullptr) {
+        fmt::print("Destination not found\n");
+        d.disconnect();
+        // TODO ERROR HANDLE
+        return;
+      }
+      Forwarder *forwarder = new Forwarder(&d, d2);
+      forwarder->start();
+      forwarders.push_back(forwarder);
+    });
+    return !n->listen(nParams);
   }
 };
 } // namespace wa
diff --git a/include/network/IP.hpp b/include/network/IP.hpp
@@ -90,7 +90,7 @@ public:
     int stat = system(
         fmt::format("ip address add {} dev {}", params, getNetworkParams())
             .c_str());
-    if (stat < 0) {
+    if (stat != 0) {
       Log::e("Operation failed");
       return -1;
     }