commit 787fd08203d8663344ef132eba18d2913366627f
parent 26038c32d62037db820d915656096ebd8b79e6b5
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Sat, 16 Mar 2019 13:11:57 +0000
parent 26038c32d62037db820d915656096ebd8b79e6b5
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Sat, 16 Mar 2019 13:11:57 +0000
Classified as Networks and Devices, and refactored WiFi to IP
8 files changed, 187 insertions(+), 164 deletions(-)
diff --git a/forwarder.cpp b/forwarder.cpp @@ -4,7 +4,7 @@ #include <thread> #include <Network.hpp> -#include <WiFi.hpp> +#include <IP.hpp> #include <Bluetooth.hpp> #include <Console.hpp> #include <ZigBee.hpp> @@ -18,8 +18,7 @@ using namespace wa; Forwarder *f = nullptr; std::map<std::string, Network *> networks; -Network *l = nullptr; -Network *r = nullptr; +std::map<std::string, Network *> activeNetworks; void exit_handler(int sig){ if(f != nullptr){ @@ -28,26 +27,35 @@ void exit_handler(int sig){ } //delete left and right initiators* //unregister callbacks - delete l; - delete r; - exit(EXIT_SUCCESS); + for(auto network : activeNetworks){ + delete network.second; + } + exit(EXIT_SUCCESS); } -Network *getNetwork(std::string in){ - cout << in; +Network *getNetwork(){ + cout << "Enter the name of the device: "; std::string str; cin >> str; + if(str == "end"){ + return nullptr; + } if(networks[str] != nullptr){ return networks[str]; } - cerr << "No Such interface available, please select from the list" << endl; - return getNetwork(in); + cerr << "No such network available, please select from the list" << endl; + return getNetwork(); +} + +void displaySelectedNetworks(){ + for(auto network : activeNetworks){ + cout << network.first << endl; + } } int main(int argc, char *argv[]) { signal(SIGINT, exit_handler); - cout << "Select the networks" << endl; std::cout << "Initializing please wait..." << std::endl; @@ -62,32 +70,17 @@ int main(int argc, char *argv[]) cout << n.first << std::endl; } - l = getNetwork("Enter the name of the left device: "); - r = getNetwork("Enter the name of the right device: "); + Network *t = nullptr; + do{ + if(t == nullptr)continue; + activeNetworks[t->getUserFriendlyName()] = t; + } while((t = getNetwork()) != nullptr); - thread *t = new thread([](){ - cerr << "Debug Information" << endl; - auto v = l->discover(); - cout << l->getUserFriendlyName() << " discovers" << endl; - for (NetworkInfo s : v){ - cerr << s.name << " : " << s.address << endl; - } - v = r->discover(); - cout << r->getUserFriendlyName() << " discovers" << endl; - for (NetworkInfo s : v){ - cerr << s.name << " : " << s.address << endl; - } - }); + displaySelectedNetworks(); + + Connector c = Connector(activeNetworks); - //Wait for the initiators to connect - Connector c = Connector(*l, *r); - c.setOnPipeCreated([](Network &l, Network &r){ - cout << "Pipe created" << endl; - f = new Forwarder(l, r); - f->start(); - }); - c.start(); + // c.start(); while (1); - t->join(); return 0; }
diff --git a/include/Connector.hpp b/include/Connector.hpp @@ -2,48 +2,24 @@ #define _CONNECTOR_H #include <iostream> +#include <vector> #include <functional> +#include <Network.hpp> + using namespace std; namespace wa { class Connector { - private: - bool lo = false; - bool ro = false; - Network *l = nullptr; - Network *r = nullptr; - std::function<void(Network &l, Network &r)> listener; - void addNetworkAndCheck(Network &n){ - if(!lo){ - lo = true; - return; - } - if(!ro){ - ro = true; - listener(*l, *r); - } - } + private: + //onAddedSuccessfully + std::function<void(Network &n)> listener; + std::map<std::string, Network *> networks; public: - Connector(Network &l, Network &r) : l(&l), r(&r){ - l.setOnNetworkConnected([&](Network &n){ - addNetworkAndCheck(n); - }); - r.setOnNetworkConnected([&](Network &n){ - addNetworkAndCheck(n); - }); + Connector(std::map<std::string, Network *> networks) : networks(networks){ } - - void start(){ - l->listen(); - r->listen(); - } - - void setOnPipeCreated(std::function<void(Network &l, Network &r)> listener){ - this->listener = listener; - }; }; }
diff --git a/include/Forwarder.hpp b/include/Forwarder.hpp @@ -4,7 +4,7 @@ #include <thread> #include <Network.hpp> -#include <WiFi.hpp> +#include <IP.hpp> #include <Bluetooth.hpp> #define BUF_SIZE 20
diff --git a/include/IP.hpp b/include/IP.hpp @@ -0,0 +1,84 @@ +#ifndef _IP_H +#define _IP_H "IP" + +#include <iostream> +#include <cstdlib> +#include <cstring> +#include <unistd.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <net/if.h> +#include <wifi_scan.h> + +#include <Network.hpp> +#include <model/NetworkInfo.hpp> +#include <IPDevice.hpp> +#include <comm.h> + +//TODO update value based on the hardware using cmake +#define W_DEFAULT_PORT 2500 + +using namespace std; + +namespace wa { + class IP : public Network{ + std::string ifName; + public: + IP(std::string ifName) : ifName(ifName){ + } + + bool init() override { + return true; + struct ifreq ifr = {0}; + bool uar = false; //up_and_running + strcpy( ifr.ifr_name, ifName.c_str() ); + int fd = socket(AF_INET, SOCK_STREAM, 0); + //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 _IP_H; + } + + void listen() + { + comm_socket fd = comm_start_server(W_DEFAULT_PORT); + devices.push_back((Device *)new IPDevice(fd)); + listener(*this); + } + + int connect(std::string conParams){ + comm_socket fd = ::comm_connect_server(conParams.c_str(), W_DEFAULT_PORT); + if(fd == SOCKET_ERROR){ + return -1; + } + devices.push_back((Device *)new IPDevice(fd)); + return 0; + } + + std::vector<NetworkInfo> discover() override{ + std::vector<NetworkInfo> v; + int status, i; + struct bss_info bss[10]; + struct wifi_scan *wifi = wifi_scan_init(ifName.c_str()); + status=wifi_scan_all(wifi, bss, 10); + for(i=0;i<status && i<10;++i){ + NetworkInfo ni; + ni.address = bss[i].ssid; + v.push_back(ni); + } + wifi_scan_close(wifi); + return v; + } + + ~IP(){ + //deinit + } + }; +} + +#endif
diff --git a/include/IPDevice.hpp b/include/IPDevice.hpp @@ -0,0 +1,50 @@ +#ifndef _IP_DEVICE_H +#define _IP_DEVICE_H + +#include <sys/socket.h> +#include <unistd.h> + +#include <unistd.h> +#include <sys/ioctl.h> +#include <sys/socket.h> +#include <net/if.h> + +#include <comm.h> + +#include <Device.hpp> + +#define W_DEFAULT_PORT 2500 + + +namespace wa +{ +class IPDevice : Device +{ + private: + comm_socket fd; + + public: + IPDevice(comm_socket fd) : fd(fd) + { + } + + //TODO test + std::string recv(int bytesToRead) override + { + char buffer[bytesToRead]; + ::recv(fd, buffer, bytesToRead - 2, 0); + buffer[bytesToRead - 1] = '\0'; + return std::string(buffer); + } + + //TODO test + int send(std::string message) override + { + return ::send(fd, message.c_str(), message.size(), 0); + } + ~IPDevice(){ + comm_close_socket(fd); + } +}; +} // namespace wa +#endif+ \ No newline at end of file
diff --git a/include/Network.hpp b/include/Network.hpp @@ -2,12 +2,15 @@ #define _NETWORK_H #include <functional> #include <vector> + #include <model/NetworkInfo.hpp> +#include <Device.hpp> namespace wa { class Network { protected: std::function<void(Network &)> listener; + std::vector<Device *> devices; public: Network(){ } @@ -21,9 +24,16 @@ namespace wa { virtual std::string getUserFriendlyName() = 0; virtual void listen() = 0; virtual int connect(std::string) = 0; - virtual int send(std::string message) = 0; - virtual std::string recv(int bytesToRead) = 0; + virtual int send(std::string message){ + return -1; + } + virtual std::string recv(int bytesToRead) { + return ""; + } virtual std::vector<NetworkInfo> discover() = 0; + std::vector<Device *> getDevices(){ + return devices; + } }; }; #endif
diff --git a/include/NetworkIndexer.hpp b/include/NetworkIndexer.hpp @@ -2,7 +2,7 @@ #define _NET_INDEXER_H #include <Network.hpp> -#include <WiFi.hpp> +#include <IP.hpp> #include <Bluetooth.hpp> #include <Console.hpp> #include <ZigBee.hpp> @@ -17,10 +17,10 @@ class NetworkIndexer std::vector<Network *> networks; networks.push_back(new Bluetooth()); networks.push_back(new Console()); - networks.push_back(new WiFi("")); + networks.push_back(new IP("enp3s0")); networks.push_back(new ZigBee("")); return networks; } }; } -#endif- \ No newline at end of file +#endif
diff --git a/include/WiFi.hpp b/include/WiFi.hpp @@ -1,90 +0,0 @@ -#ifndef _WIFI_H -#define _WIFI_H "WiFi" - -#include <iostream> -#include <cstdlib> -#include <cstring> -#include <unistd.h> -#include <sys/ioctl.h> -#include <sys/socket.h> -#include <net/if.h> -#include <wifi_scan.h> - -#include <Network.hpp> -#include <model/NetworkInfo.hpp> -#include <comm.h> - -//TODO update value based on the hardware using cmake -#define W_DEFAULT_PORT 2500 - -using namespace std; - -namespace wa { - class WiFi : public Network{ - int fd; - std::string ifName; - public: - WiFi(std::string ifName) : ifName(ifName){ - } - - bool init() override { - struct ifreq ifr = {0}; - bool uar = false; //up_and_running - strcpy( ifr.ifr_name, ifName.c_str() ); - int fd = socket(AF_INET, SOCK_STREAM, 0); - //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); - } - - int connect(std::string conParams){ - return -1; - } - - //TODO test - int send(std::string message) override { - return ::send(fd, message.c_str(), message.size(), 0); - } - - //TODO - std::vector<NetworkInfo> discover() override{ - std::vector<NetworkInfo> v; - int status, i; - struct bss_info bss[10]; - struct wifi_scan *wifi = wifi_scan_init(ifName.c_str()); - status=wifi_scan_all(wifi, bss, 10); - for(i=0;i<status && i<10;++i){ - NetworkInfo ni; - ni.address = bss[i].ssid; - v.push_back(ni); - } - wifi_scan_close(wifi); - return v; - } - - //TODO test - std::string recv(int bytesToRead) override { - char buffer[bytesToRead]; - ::recv(fd, buffer, bytesToRead - 2, 0); - buffer[bytesToRead - 1] = '\0'; - return std::string(buffer); - } - - ~WiFi(){ - close(fd); - } - }; -} - -#endif
: bharatvaj/forwarder