: bharatvaj/forwarder

commit 31748c4645c53429dcd8dd659e7d94c1761543ae
parent 02178ec96da90470a13f3a6468a0a78053a319e7
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Mon, 25 Mar 2019 03:56:59 +0000

Implemented slot allocation algorithm for Forwarder bind
6 files changed, 81 insertions(+), 25 deletions(-)
M
CMakeLists.txt
|
2
+-
M
Shell.hpp
|
28
+++++++++++++---------------
M
depinstall.sh
|
2
++
M
forwarder.cpp
|
7
++++++-
M
include/Forwarder.hpp
|
52
+++++++++++++++++++++++++++++++++++++++++++++++++++-
M
include/network/IP.hpp
|
15
++++++++-------
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -1,4 +1,4 @@
-cmake_minimum_required(VERSION 3.0)
+cmake_minimum_required(VERSION 3.0.2)
 project(forwarder)
 
 find_package(comm)
diff --git a/Shell.hpp b/Shell.hpp
@@ -68,14 +68,10 @@ public:
                     std::function<void(std::vector<std::string>)> opFunc) {
     // operations[op] = opFunc;
   }
-  void addOperation(Operation *op){
-    operations[op->name] = op;
-  }
+  void addOperation(Operation *op) { operations[op->name] = op; }
 
-  std::map<std::string, Operation *> getOperations(){
-    return operations;
-  }
-  //TODO
+  std::map<std::string, Operation *> getOperations() { return operations; }
+  // TODO
   // void addOperation(Operation op){
   //   operations[op.name] = op;
   // }

@@ -84,28 +80,30 @@ public:
       return;
     }
     Operation *op = operations[ins[0]];
-    if(op == nullptr){
+    if (op == nullptr) {
       print(fmt::format("{}: operation not found\n", ins[0]));
       return;
     }
     std::function<void(Operation, std::vector<std::string>)> func = op->fp;
-      ins.erase(ins.begin(), ins.begin() + 1);
+    ins.erase(ins.begin(), ins.begin() + 1);
+    try {
       func(*op, ins);
+    } catch (std::bad_function_call bfc) {
+      print(fmt::format("{}: operation not found\n", ins[0]));
+    }
   }
 
   void print(std::string message) {
     if (isDaemon) {
-        if (client != nullptr) {
-          client->write(message);
+      if (client != nullptr) {
+        client->write(message);
       }
     } else {
       fmt::print(message);
     }
   }
 
-  void println(std::string message){
-    print(message + "\n");
-  }
+  void println(std::string message) { print(message + "\n"); }
 
   void start() {
     if (isDaemon) {

@@ -123,5 +121,5 @@ public:
 
   void setOnShellExit(std::function<void(int)> func) { exitHandler = func; }
 };
-} // namespace wa
+} // namespace shell4c
 #endif 
\ No newline at end of file
diff --git a/depinstall.sh b/depinstall.sh
@@ -59,3 +59,5 @@ cloneAndInstall https://github.com/bmegli/wifi-scan
 cloneAndInstall https://github.com/bharatvaj/clog
 cloneAndInstall https://github.com/bharatvaj/libcomm
 downloadAndInstallXBee
+cloneAndInstall https://github.com/bharatvaj/EventManager
+cloneAndInstall https://github.com/bharatvaj/Dot
diff --git a/forwarder.cpp b/forwarder.cpp
@@ -18,7 +18,7 @@ using namespace wa;
 using namespace shell4c;
 
 NetworkController *nc;
-Shell shell(true, "forwarder> ");
+Shell shell(false, "forwarder> ");
 
 void exit_handler(int sig) {
   if (nc != nullptr) {

@@ -82,6 +82,9 @@ void list(shell4c::Operation op, vector<string> msg) {
 
 void start(shell4c::Operation op, vector<string> msg) {
   for (auto network : nc->activeNetworks) {
+    network.second->setOnDeviceConnected([](Device &d){
+      Forwarder::addDevice(&d);
+    });
     network.second->listen();
   }
 }

@@ -101,7 +104,9 @@ void bind_(shell4c::Operation op, vector<string> msg) {
             .c_str());
     if (stat < 0) {
       shell.println("Operation failed");
+      return;
     }
+    Forwarder::bind(msg[0], msg[1]);
   }
 }
 
diff --git a/include/Forwarder.hpp b/include/Forwarder.hpp
@@ -1,6 +1,7 @@
 #ifndef _FORWARDER_H
 #define _FORWARDER_H
 
+#include <map>
 #include <thread>
 
 #include <device/Device.hpp>

@@ -8,14 +9,56 @@
 #define BUF_SIZE 20
 
 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::map<BindPair *, BindPair *> bindMap;
+  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 {
+          auto p = (new std::pair<BindPair *, BindPair *>());
+          p->first = n1.first;
+          p->second = n1.second;
+          return p;
+        }
+      } else if (d->address == n1.second->a) {
+        n1.second->d = d;
+        if (n1.first->d == nullptr) {
+          return nullptr;
+        } else {
+          auto p = (new std::pair<BindPair *, BindPair *>());
+          p->first = n1.first;
+          p->second = n1.second;
+          return p;
+        }
+      }
+    }
+    return nullptr;
+  }
 
 public:
-  Forwarder(Device *l, Device *r) : lp(l), rp(r) {}
+  Forwarder(Device *lp, Device *rp) : lp(lp), rp(rp) {}
+
+  static void addDevice(Device *d) {
+    std::pair<BindPair *, BindPair *> *pair = findAdd(d);
+    if (pair) {
+      forwarders.push_back(new Forwarder(pair->first->d, pair->second->d));
+    }
+  }
 
   // FIXME last 'while' blocked, input expected from the network before closing
   void start() {

@@ -31,6 +74,10 @@ public:
     });
   }
 
+  static void bind(std::string lb, std::string rb) {
+    bindMap[new BindPair(lb, nullptr)] = new BindPair(rb, nullptr);
+  }
+
   // TODO test
   void stop() {
     shouldStop = true;

@@ -38,5 +85,8 @@ public:
     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/network/IP.hpp b/include/network/IP.hpp
@@ -5,14 +5,15 @@
 #include <cstring>
 #include <iostream>
 #include <net/if.h>
+#include <netinet/in.h>
 #include <sys/ioctl.h>
 #include <sys/socket.h>
 #include <unistd.h>
 
-#include <device/IPDevice.hpp>
-#include <network/Network.hpp>
 #include <comm.h>
+#include <device/IPDevice.hpp>
 #include <model/NetworkInfo.hpp>
+#include <network/Network.hpp>
 
 // TODO update value based on the hardware using cmake
 #define W_DEFAULT_PORT 2500

@@ -22,8 +23,6 @@ using namespace std;
 namespace wa {
 class IP : public Network {
 
-    
-
   std::string networkParams;
 
 public:

@@ -45,10 +44,14 @@ public:
   std::string getUserFriendlyName() override { return _IP_H; }
 
   int listen() override {
+    struct sockaddr_in peer;
+    socklen_t peerLen = sizeof(peer);
     comm_socket fd = comm_start_server(W_DEFAULT_PORT);
+    getsockname(fd, (struct sockaddr *)&peer, &peerLen);
     Device *d = (Device *)new IPDevice(fd);
     // FIXME NAMING ERROR
     d->name = "IPDevice " + std::to_string(devices.size());
+    d->address = inet_ntoa(peer.sin_addr);
     devices.push_back(d);
 
     if (fd == SOCKET_ERROR) {

@@ -58,9 +61,7 @@ public:
     return 0;
   }
 
-  int addDevice(std::string ipaddress){
-    return -1;
-  }
+  int addDevice(std::string ipaddress) { return -1; }
 
   int connect(std::string conParams) override {
     comm_socket fd = ::comm_connect_server(conParams.c_str(), W_DEFAULT_PORT);