commit 2ce573810a5e515a59ee8d69fe6a274559b3270e
parent 30534b1f0f20f0157adf6525ac088d7d8ae56e26
Author: Bharatvaj H <bharatvaj@yahoo.com>
Date: Wed, 27 Mar 2019 20:14:54 +0530
parent 30534b1f0f20f0157adf6525ac088d7d8ae56e26
Author: Bharatvaj H <bharatvaj@yahoo.com>
Date: Wed, 27 Mar 2019 20:14:54 +0530
Added feature of multiple binds, refactored NetworkInfo to DeviceInfo
13 files changed, 137 insertions(+), 50 deletions(-)
diff --git a/forwarder.cpp b/forwarder.cpp @@ -9,7 +9,7 @@ #include <ConfigurationBuilder.hpp> #include <NetworkController.hpp> #include <fmt/format.h> -#include <model/NetworkInfo.hpp> +#include <model/DeviceInfo.hpp> #include <network/Network.hpp> #include <network/NetworkIndexer.hpp> @@ -89,9 +89,23 @@ void start(shell4c::Operation op, vector<string> msg) { network.second->setOnDeviceConnected([](Device &d) { Forwarder::addDevice(&d); fmt::print("added device: {}\n", d.address); - Device *de = nc->activeNetworks["Bluetooth"]->connect("B8:"); + Device *de = Forwarder::getPairOf(&d); + if(de != nullptr){ + Network *n = de->network; + if(n == nullptr){ + //not assigned + return; + } + std::thread([&](){ + n->connect(de->address); + }); + } + }); + } + for(auto db : Forwarder::bindMap){ + std::thread([&](){ + db.first->d->network->listen(db.first->a); }); - network.second->listen(); } }
diff --git a/include/Forwarder.hpp b/include/Forwarder.hpp @@ -15,12 +15,12 @@ public: 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) { @@ -45,6 +45,8 @@ private: } public: + static std::map<BindPair *, BindPair *> bindMap; + Forwarder(Device *lp, Device *rp) : lp(lp), rp(rp) { std::cout << "Forwarder Started" << std::endl; } @@ -57,7 +59,7 @@ public: forwarder->start(); } } - + void start() { t1 = new std::thread([&]() { char *str = (char *)calloc(sizeof(char), BUF_SIZE); @@ -83,6 +85,17 @@ public: bindMap[new BindPair(lb, nullptr)] = new BindPair(rb, nullptr); } + //todo test + static Device *getPairOf(Device *d){ + for(auto p : bindMap){ + if(p.first->a == d->address){ + return p.second->d; + } + } + return nullptr; + } + + // TODO test void stop() { shouldStop = true;
diff --git a/include/device/Device.hpp b/include/device/Device.hpp @@ -1,9 +1,9 @@ #ifndef _DEVICE_H #define _DEVICE_H #include <iostream> -#include <model/NetworkInfo.hpp> +#include <model/DeviceInfo.hpp> namespace wa { -class Device : public NetworkInfo { +class Device : public DeviceInfo { public: Device() {} virtual int read(char *message, int bytesToRead) = 0;
diff --git a/include/device/IPDevice.hpp b/include/device/IPDevice.hpp @@ -17,9 +17,10 @@ namespace wa { class IPDevice : Device { private: comm_socket fd; + bool isTcp; public: - IPDevice(comm_socket fd) : fd(fd) {} + IPDevice(comm_socket fd) : fd(fd), isTcp(true) {} int read(char *str, int bytesToRead) override { return ::read(fd, str, bytesToRead);
diff --git a/include/medium/Medium.hpp b/include/medium/Medium.hpp @@ -1,11 +1,11 @@ #ifndef _MEDIUM_H #define _MEDIUM_H -#include <model/NetworkInfo.hpp> +#include <model/DeviceInfo.hpp> namespace wa { class Medium { public: Medium() {} - virtual std::vector<NetworkInfo> discover() = 0; + virtual std::vector<DeviceInfo> discover() = 0; virtual std::string getUserFriendlyName() = 0; }; } // namespace wa
diff --git a/include/model/DeviceInfo.hpp b/include/model/DeviceInfo.hpp @@ -0,0 +1,14 @@ +#ifndef _DEVICE_INFO +#define _DEVICE_INFO +#include <iostream> +#include <network/Network.hpp> +namespace wa { + class Network; + class DeviceInfo { + public: + std::string address; + std::string name; + Network *network; + }; +} +#endif+ \ No newline at end of file
diff --git a/include/model/NetworkInfo.hpp b/include/model/NetworkInfo.hpp @@ -1,12 +0,0 @@ -#ifndef NET_INFO_H -#define NET_INFO_H - -#include <iostream> - -class NetworkInfo { -public: - std::string address; - std::string name; -}; - -#endif
diff --git a/include/network/Bluetooth.hpp b/include/network/Bluetooth.hpp @@ -11,8 +11,8 @@ #include <unistd.h> #include <device/BluetoothDevice.hpp> +#include <model/DeviceInfo.hpp> #include <network/Network.hpp> -#include <model/NetworkInfo.hpp> namespace wa { class Bluetooth : public Network { @@ -30,7 +30,7 @@ public: std::string getUserFriendlyName() override { return _BLUETOOTH_H; } - int listen() { + int listen(std::string networkParams) { struct sockaddr_rc loc_addr = {0}, rem_addr = {0}; comm_socket s; socklen_t opt = sizeof(rem_addr); @@ -45,7 +45,7 @@ public: // local bluetooth adapter loc_addr.rc_family = AF_BLUETOOTH; loc_addr.rc_bdaddr = inc; - loc_addr.rc_channel = (uint8_t)1; + loc_addr.rc_channel = (uint8_t)6; bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); // put socket into listening mode @@ -87,8 +87,8 @@ public: } // TODO - std::vector<NetworkInfo> discover() override { - std::vector<NetworkInfo> values; + std::vector<DeviceInfo> discover() override { + std::vector<DeviceInfo> values; inquiry_info *ii = NULL; int max_rsp, num_rsp; int dev_id, sock, len, flags; @@ -115,7 +115,7 @@ public: for (i = 0; i < num_rsp; i++) { ba2str(&(ii + i)->bdaddr, addr); hci_read_remote_name(sock, &(ii + i)->bdaddr, sizeof(name), name, 0); - NetworkInfo ni; + DeviceInfo ni; ni.name = name; values.push_back(ni); }
diff --git a/include/network/Console.hpp b/include/network/Console.hpp @@ -2,11 +2,11 @@ #define _CONSOLE_H "Console" #include <iostream> -#include <model/NetworkInfo.hpp> #include <network/Network.hpp> #include <vector> #include <device/ConsoleDevice.hpp> +#include <model/DeviceInfo.hpp> namespace wa { class Console : public Network { @@ -17,7 +17,7 @@ public: std::string getUserFriendlyName() override { return _CONSOLE_H; } - int listen() override { + int listen(std::string networkParams) override { Device *d = (Device *)new ConsoleDevice(); devices.push_back(d); deviceConnected(*d); @@ -30,8 +30,8 @@ public: return (Device *)new ConsoleDevice(); } - std::vector<NetworkInfo> discover() override { - return std::vector<NetworkInfo>(); + std::vector<DeviceInfo> discover() override { + return std::vector<DeviceInfo>(); } }; } // namespace wa
diff --git a/include/network/IP.hpp b/include/network/IP.hpp @@ -12,7 +12,7 @@ #include <comm.h> #include <device/IPDevice.hpp> -#include <model/NetworkInfo.hpp> +#include <model/DeviceInfo.hpp> #include <network/Network.hpp> using namespace std; @@ -21,6 +21,8 @@ namespace wa { class IP : public Network { std::string networkParams; + int configPort = 2500; + public: IP() {} @@ -40,11 +42,62 @@ public: std::string getUserFriendlyName() override { return _IP_H; } - int listen() override { + comm_socket start_server(bool isTcp, int port){ + static comm_socket servfd; + + struct sockaddr_in server, client; + socklen_t cli_size; + + if (comm_check_port(port) < 0) + { + return -1; + } + //Prepare the sockaddr_in structure + server.sin_family = AF_INET; + server.sin_addr.s_addr = INADDR_ANY; + server.sin_port = htons(port); + cli_size = sizeof(struct sockaddr_in); + + //Create socket + if(isTcp){ + servfd = socket(AF_INET, SOCK_STREAM, 0); + } else { + servfd = socket(AF_INET, SOCK_DGRAM, 0); + } + if (servfd == SOCKET_ERROR) + { + log_err(_COMM, "could not create socket"); + return -1; + } + //Bind + if (bind(servfd, (struct sockaddr *)&server, sizeof(server)) < 0) + { + log_err(_COMM, "bind failed"); + return -1; + } + //Listen + ::listen(servfd, COMM_SERV_BACKLOG); + //Accept and incoming connection + log_inf(_COMM, "Waiting for incoming connections..."); + //accept connection from an incoming client + comm_socket clifd = accept(servfd, (struct sockaddr *)&client, &cli_size); + if (clifd == SOCKET_ERROR) + { + log_inf(_COMM, "Accept failed"); + return -1; + } + log_inf(_COMM, "Connection accepted"); + return clifd; + } + + int listen(std::string networkParams) override { cout << "IP listening" << endl; struct sockaddr_in peer; socklen_t peerLen = sizeof(peer); - comm_socket fd = comm_start_server(2500); + static int cont; + bool isTcp = false; + comm_socket fd = start_server(isTcp, 2500); + if(fd < 0){ cout << "Socket error" << endl; } @@ -65,6 +118,10 @@ public: int addDevice(std::string ipaddress) { return -1; } + void setNetworkParams(std::string networkParams) override { + Network::setNetworkParams(networkParams); + } + Device *connect(std::string conParams) override { comm_socket fd = ::comm_connect_server(conParams.c_str(), 21); if (fd == SOCKET_ERROR) { @@ -74,8 +131,8 @@ public: return (Device *)new IPDevice(0); } - std::vector<NetworkInfo> discover() override { - return std::vector<NetworkInfo>(); + std::vector<DeviceInfo> discover() override { + return std::vector<DeviceInfo>(); } ~IP() {
diff --git a/include/network/Network.hpp b/include/network/Network.hpp @@ -4,7 +4,7 @@ #include <vector> #include <device/Device.hpp> -#include <model/NetworkInfo.hpp> +#include <model/DeviceInfo.hpp> namespace wa { class Network { @@ -25,7 +25,7 @@ public: std::function<void(Device &)> deviceDisconnected) final { this->deviceDisconnected = deviceDisconnected; } - virtual void setNetworkParams(std::string networkParams) final { + virtual void setNetworkParams(std::string networkParams) { this->networkParams = networkParams; } virtual std::string getNetworkParams() final { @@ -36,11 +36,11 @@ public: // returns a human readable user friendly name virtual std::string getUserFriendlyName() = 0; // listen - virtual int listen() = 0; + virtual int listen(std::string) = 0; // connects to the given address virtual Device *connect(std::string) = 0; // discovers different networks - virtual std::vector<NetworkInfo> discover() = 0; + virtual std::vector<DeviceInfo> discover() = 0; // should be deprecated std::vector<Device *> getDevices() { return devices; } };
diff --git a/include/network/ZWave.hpp b/include/network/ZWave.hpp @@ -15,8 +15,8 @@ #include <xbee/wpan.h> #include <network/Network.hpp> +#include <model/DeviceInfo.hpp> #include <comm.h> -#include <model/NetworkInfo.hpp> using namespace std; @@ -33,15 +33,15 @@ public: std::string getUserFriendlyName() override { return _ZWAVE_H; } - int listen() override { + int listen(std::string networkParams) override { return -1; } Device *connect(std::string conParams) { return nullptr; } // TODO - std::vector<NetworkInfo> discover() override { - std::vector<NetworkInfo> v; + std::vector<DeviceInfo> discover() override { + std::vector<DeviceInfo> v; return v; }
diff --git a/include/network/ZigBee.hpp b/include/network/ZigBee.hpp @@ -15,9 +15,8 @@ #include <xbee/wpan.h> #include <network/Network.hpp> +#include <model/DeviceInfo.hpp> #include <comm.h> -#include <model/NetworkInfo.hpp> - using namespace std; @@ -34,7 +33,7 @@ public: std::string getUserFriendlyName() override { return _ZIGBEE_H; } - int listen() override { + int listen(std::string networkParams) override { // deviceConnected(*this); return -1; } @@ -42,8 +41,8 @@ public: Device *connect(std::string conParams) { return nullptr; } // TODO - std::vector<NetworkInfo> discover() override { - std::vector<NetworkInfo> v; + std::vector<DeviceInfo> discover() override { + std::vector<DeviceInfo> v; return v; }
: bharatvaj/forwarder