: bharatvaj/forwarder

commit 1b62f7e2901c3c65b0dc55420928ab0034e12ad2
parent e4acbbf333044db09d0921b3184e07d5ec135b0e
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Sun, 10 Mar 2019 15:12:51 +0000

Networks can be selected by the user on runtime
7 files changed, 90 insertions(+), 20 deletions(-)
A
NetworkFactory.hpp
|
10
++++++++++
M
forwarder.cpp
|
40
+++++++++++++++++++++++++++++-----------
M
include/Bluetooth.hpp
|
9
+++++++--
M
include/Console.hpp
|
11
++++++++---
M
include/Network.hpp
|
6
+++++-
A
include/NetworkIndexer.hpp
|
25
+++++++++++++++++++++++++
M
include/WiFi.hpp
|
9
++++++---
diff --git a/NetworkFactory.hpp b/NetworkFactory.hpp
@@ -0,0 +1,9 @@
+#ifndef _NET_FACTORY_H
+#define _NET_FACTORY_H
+#include <Network.hpp>
+class NetworkFactory {
+    static Network *getNetwork(std::string s){
+
+    }
+};
+#endif+
\ No newline at end of file
diff --git a/forwarder.cpp b/forwarder.cpp
@@ -1,4 +1,5 @@
 #include <iostream>
+#include <map>
 #include <csignal>
 #include <thread>
 

@@ -9,11 +10,13 @@
 #include <Connector.hpp>
 #include <Forwarder.hpp>
 #include <model/NetworkInfo.hpp>
+#include <NetworkIndexer.hpp>
 
 using namespace std;
 using namespace wa;
 
 Forwarder *f = nullptr;
+std::map<std::string, Network *> networks;
 Network *l = nullptr;
 Network *r = nullptr;
 

@@ -29,23 +32,37 @@ void exit_handler(int sig){
   exit(EXIT_SUCCESS);
 }
 
+Network *getNetwork(std::string in){
+  cout << in;
+  std::string str;
+  cin >> str;
+  if(networks[str] != nullptr){
+    return networks[str];
+  }
+  cerr << "No Such interface available, please select from the list" << endl;
+  return getNetwork(in);
+}
+
 int main(int argc, char *argv[])
 {
   signal(SIGINT, exit_handler);
-  //TODO get all wireless
-   l = new WiFi(argv[1]);
-    r = new Console();
-  //Wait for the initiators to connect
+  cout << "Select the networks" << endl;
 
-  //init and check status
-  if(!l->init()){
-    cout << "Left interface not initialized" << endl;
-    return -1;
+  std::cout << "Initializing please wait..." << std::endl;
+
+  for(Network *n : NetworkIndexer::getNetworks()){
+    if(n->init()){
+      networks[n->getUserFriendlyName()] = n;
+    }
   }
-  if(!r->init()){
-    cout << "Right interface not initialized" << endl;
-    return -1;
+
+  cout << "Select the networks" << endl;
+  for(auto n : networks){
+    cout << n.first << std::endl;
   }
+  
+  l = getNetwork("Enter the name of the left device: ");
+  r = getNetwork("Enter the name of the right device: ");
 
   auto v = l->discover();
   cout << "Discovered" << endl;

@@ -53,6 +70,7 @@ int main(int argc, char *argv[])
      cout << s.name << " : " << s.address << endl;
   }
 
+  //Wait for the initiators to connect
   Connector c = Connector(*l, *r);
   c.setOnPipeCreated([](Network &l, Network &r){
     cout << "Pipe created" << endl;
diff --git a/include/Bluetooth.hpp b/include/Bluetooth.hpp
@@ -1,5 +1,5 @@
 #ifndef _BLUETOOTH_H
-#define _BLUETOOTH_H
+#define _BLUETOOTH_H "Bluetooth"
 
 #include <bluetooth/bluetooth.h>
 #include <bluetooth/rfcomm.h>

@@ -19,7 +19,8 @@ private:
   int client;
   bdaddr_t inc = ((bdaddr_t) {{0, 0, 0, 0, 0, 0}});
 public:
-  Bluetooth() {}
+  Bluetooth() {
+  }
 
   bool init() override {
     int s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);

@@ -27,6 +28,10 @@ public:
     return !(s < 0);
   }
 
+  std::string getUserFriendlyName() override {
+    return _BLUETOOTH_H;
+  }
+
   void listen() {
     struct sockaddr_rc loc_addr = {0}, rem_addr = {0};
     int s;
diff --git a/include/Console.hpp b/include/Console.hpp
@@ -1,5 +1,5 @@
-#ifndef _Console_H
-#define _Console_H
+#ifndef _CONSOLE_H
+#define _CONSOLE_H "Console"
 
 #include <iostream>
 #include <vector>

@@ -9,12 +9,17 @@
 namespace wa {
   class Console : public Network {
   public:
-    Console(){}
+    Console(){
+    }
 
     bool init() override {
           return true;
     }
 
+    std::string getUserFriendlyName() override{
+      return _CONSOLE_H;
+    }
+
     int send(std::string message) override{
       cout << message;
       return message.size();
diff --git a/include/Network.hpp b/include/Network.hpp
@@ -9,12 +9,16 @@ namespace wa {
   protected:
     std::function<void(Network &)> listener;
   public:
-    Network(){}
+    Network(){
+    }
+    ~Network(){
+    }
     //FIXME add function for unregistering
     virtual void setOnNetworkConnected(std::function<void(Network &)> listener){
       this->listener = listener;
     }
     virtual bool init() = 0;
+    virtual std::string getUserFriendlyName() = 0;
     virtual void listen() = 0;
     virtual int connect(std::string) = 0;
     virtual int send(std::string message) = 0;
diff --git a/include/NetworkIndexer.hpp b/include/NetworkIndexer.hpp
@@ -0,0 +1,24 @@
+#ifndef _NET_INDEXER_H
+#define _NET_INDEXER_H
+#include <Network.hpp>
+
+#include <WiFi.hpp>
+#include <Bluetooth.hpp>
+#include <Console.hpp>
+
+namespace wa
+{
+class NetworkIndexer
+{
+  public:
+    static std::vector<Network *> getNetworks()
+    {
+        std::vector<Network *> networks;
+        networks.push_back(new Bluetooth());
+        networks.push_back(new Console());
+        networks.push_back(new WiFi(""));
+        return networks;
+    }
+};
+}
+#endif+
\ No newline at end of file
diff --git a/include/WiFi.hpp b/include/WiFi.hpp
@@ -1,5 +1,5 @@
 #ifndef _WIFI_H
-#define _WIFI_H
+#define _WIFI_H "WiFi"
 
 #include <iostream>
 #include <cstdlib>

@@ -25,7 +25,6 @@ namespace wa {
       std::string ifName;
   public:
     WiFi(std::string ifName) : ifName(ifName){
-
     }
 
     bool init() override {

@@ -33,13 +32,17 @@ namespace wa {
       bool uar = false; //up_and_running
       strcpy( ifr.ifr_name, ifName.c_str() );
       int fd = socket(AF_INET, SOCK_STREAM, 0);
-      //evavaluate uar
+      //evaluate uar
       if( ioctl( fd, SIOCGIFFLAGS, &ifr ) != -1 )
         uar = (ifr.ifr_flags & ( IFF_UP | IFF_RUNNING )) == ( IFF_UP | IFF_RUNNING );
       else uar = false;
       return uar;
     }
 
+    std::string getUserFriendlyName() override {
+      return _WIFI_H;
+    }
+
     void listen(){
       fd = comm_start_server(W_DEFAULT_PORT);
       listener(*this);