: bharatvaj/forwarder

commit ea6299a123354fdc209bad83a168c724a0f5e9e9
parent 346e74170fbc56ce2ac1b180898f7340936f8558
Author: Dinesh Kumar M <dine15122.cs@rmkec.ac.in>
Date: Tue, 22 Jan 2019 17:04:41 +0530

Added error handling in forwarder.cpp and Forwarder.hpp, init tested for WiFi and Bluetooth
4 files changed, 26 insertions(+), 22 deletions(-)
diff --git a/forwarder/forwarder.cpp b/forwarder/forwarder.cpp
@@ -12,6 +12,7 @@
 #define W_PORT 2500
 
 using namespace std;
+using namespace wa;
 
 void exit_handler(int sig){
   cout << "Forwarder ended" << endl;

@@ -21,11 +22,19 @@ void exit_handler(int sig){
 int main(int argc, char *argv[])
 {
   signal(SIGINT, exit_handler);
-  Network &w = *new WiFi();
-  Network &b = *new Bluetooth();
-  Forwarder &f = *new Forwarder(w, b);
-  f.start();
-  	while (1)
-		;
+  Network *w = new WiFi();
+  Network *b = new Bluetooth();
+  Forwarder *f = nullptr;
+  try {
+  f = new Forwarder(w, b);
+  } catch(const char *err){
+    cout << err << endl;
+  }
+  if(f == nullptr){
+    cout << "Cannot init forwarder" << endl;
+    return -1;
+  }
+  f->start();
+  	while (1);
 	return 0;
 }
diff --git a/forwarder/include/Forwarder.hpp b/forwarder/include/Forwarder.hpp
@@ -8,30 +8,25 @@
 namespace wa {
   class Forwarder {
   private:
-    bool isInit = false;
-    Network *l, *r;
+    Network *lp, *rp;
   public:
-    Forwarder(Network &l, Network &r) : l(*l), r(*r){
-      if(!l.init()){
-        return;
+    Forwarder(Network *l, Network *r) : lp(l), rp(r){
+      if(!l->init()){
+        throw "Left interface not initialized";
       }
-      if(!r.init()){
+      if(!r->init()){
+	throw "Right interface not initialized";
         return;
       }
-      isInit = true;
     }
 
     //TODO
     void start(){
-      if(!isInit){
-        throw "Not initialized";
-      }
     }
 
+    //TODO
     void stop(){
-        if(!isInit){
-          throw "Not initialized";
-        }
+        
     }
 
   };
diff --git a/forwarder/include/Network.hpp b/forwarder/include/Network.hpp
@@ -3,7 +3,7 @@
 namespace wa {
   class Network {
   public:
-    Netowrk(){}
+    Network(){}
     virtual bool init() = 0;
     virtual int send(std::string message) = 0;
     virtual std::string recv(int bytesToRead) = 0;
diff --git a/forwarder/include/WiFi.hpp b/forwarder/include/WiFi.hpp
@@ -20,7 +20,7 @@ namespace wa {
       bool uar = false; //up_and_running
 
       int fd = socket( AF_INET, SOCK_DGRAM, 0 );
-      strcpy( ifr.ifr_name, "wlan0" );
+      strcpy( ifr.ifr_name, "eth0" );
 
       //evavaluate uar
       if( ioctl( fd, SIOCGIFFLAGS, &ifr ) != -1 )

@@ -50,7 +50,7 @@ namespace wa {
     std::string recv(int bytesToRead) override {
       return "";
     }
-  }
+  };
 }
 
 #endif