: bharatvaj/forwarder

commit 919e87c7c255f541ad88ed1246136c093b357bdb
parent 4de797ec788e7391ff425e98d7238b8678789ef7
Author: Goutham SM <goutham.freaks@gmail.com>
Date: Tue, 22 Jan 2019 18:39:11 +0530

Added Console class for Network testing, forward algorithm implemented, modified forwarder.cpp to accomodate stopping forwarding process
3 files changed, 49 insertions(+), 4 deletions(-)
M
forwarder/forwarder.cpp
|
4
+++-
A
forwarder/include/Console.hpp
|
29
+++++++++++++++++++++++++++++
M
forwarder/include/Forwarder.hpp
|
20
+++++++++++++++++---
diff --git a/forwarder/forwarder.cpp b/forwarder/forwarder.cpp
@@ -9,8 +9,11 @@
 using namespace std;
 using namespace wa;
 
+Forwarder *f = nullptr;
+
 void exit_handler(int sig){
   cout << "Forwarder ended" << endl;
+  f->stop();
   exit(EXIT_SUCCESS);
 }
 

@@ -19,7 +22,6 @@ int main(int argc, char *argv[])
   signal(SIGINT, exit_handler);
   Network *w = new WiFi();
   Network *b = new Bluetooth();
-  Forwarder *f = nullptr;
   try {
   f = new Forwarder(w, b);
   } catch(const char *err){
diff --git a/forwarder/include/Console.hpp b/forwarder/include/Console.hpp
@@ -0,0 +1,29 @@
+#ifndef _Console_H
+#define _Console_H
+
+#include <iostream>
+#include <Network.hpp>
+
+namespace wa {
+  class Console : public Network {
+  public:
+    Console(){}
+
+    bool init() override {
+          return true;
+    }
+
+    int send(std::string message) override{
+      cout << message;
+    }
+
+    //TODO
+    std::string recv(int bytesToRead) override {
+      std::streamsize len = bytesToRead - 1;
+      char buffer[bytesToRead];
+      std::basic_istream::get(buffer, len);
+      return buffer;
+    }
+  };
+}
+#endif
diff --git a/forwarder/include/Forwarder.hpp b/forwarder/include/Forwarder.hpp
@@ -9,6 +9,8 @@ namespace wa {
   class Forwarder {
   private:
     Network *lp, *rp;
+    std::thread *t1, *t2;
+    bool shouldStop = false;
   public:
     Forwarder(Network *l, Network *r) : lp(l), rp(r){
       if(!l->init()){

@@ -20,13 +22,25 @@ namespace wa {
       }
     }
 
-    //TODO
+    //TODO test
     void start(){
+      t1 = new std::thread([](){
+        while(!shouldStop){
+          rp->send(lp->recv());
+        }
+      });
+      t2 = new std::thread([](){
+        while(!shouldStop){
+          lp->send(rp->recv());
+        }
+      });
     }
 
-    //TODO
+    //TODO test
     void stop(){
-        
+      shouldStop = true;
+      t1->join();
+      t2->join();
     }
 
   };