commit d8bed0c0b34844d1a568c26035185884a1ade7f5
parent ee3b33dd36b9942596eb14608250a46d4924e5f0
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Mon, 28 Jan 2019 13:50:41 +0000
parent ee3b33dd36b9942596eb14608250a46d4924e5f0
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Mon, 28 Jan 2019 13:50:41 +0000
Added Events
9 files changed, 150 insertions(+), 82 deletions(-)
M
|
80
+++++++++++++++++++++++++++++++++++++++----------------------------------------
diff --git a/forwarder/forwarder.cpp b/forwarder/forwarder.cpp @@ -6,48 +6,51 @@ #include <WiFi.hpp> #include <Bluetooth.hpp> #include <Console.hpp> +#include <Connector.hpp> #include <Forwarder.hpp> using namespace std; using namespace wa; Forwarder *f = nullptr; -std::thread *t1 = nullptr; -std::thread *t2 = nullptr; - - Network *w = nullptr; - Network *b = nullptr; +Network *l = nullptr; +Network *r = nullptr; void exit_handler(int sig){ - cout << "Forwarder ended" << endl; - f->stop(); - delete w; - delete b; + if(f != nullptr){ + f->stop(); + cout << "Forwarder ended" << endl; + } + //delete left and right initiators + //unregister callbacks + delete l; + delete r; exit(EXIT_SUCCESS); } int main(int argc, char *argv[]) { signal(SIGINT, exit_handler); - Network *c = new Console(); - t1 = new std::thread([](){ - w = new WiFi(); - }); - t2 = new std::thread([](){ - b = new Bluetooth(); - }); - t1->join(); - t2->join(); - try { - f = new Forwarder(w, c); - } catch(const char *err){ - cout << err << endl; + l = new WiFi(); + r = new Bluetooth(); + //Wait for the initiators to connect + + //init and check status + if(!l->init()){ + cout << "Left interface not initialized" << endl; + return -1; } - if(f == nullptr){ - cout << "Cannot init forwarder" << endl; + if(!r->init()){ + cout << "Right interface not initialized" << endl; return -1; } - f->start(); - while (1); + + Connector c = Connector(*l, *r); + c.setOnPipeCreated([](Network &l, Network &r){ + f = new Forwarder(l, r); + f->start(); + }); + c.start(); + while (1); return 0; }
diff --git a/forwarder/include/Bluetooth.hpp b/forwarder/include/Bluetooth.hpp @@ -1,72 +1,70 @@ #ifndef _BLUETOOTH_H #define _BLUETOOTH_H -#include <stdio.h> -#include <unistd.h> -#include <sys/socket.h> #include <bluetooth/bluetooth.h> #include <bluetooth/rfcomm.h> +#include <stdio.h> +#include <sys/socket.h> +#include <unistd.h> #include <Network.hpp> namespace wa { - class Bluetooth : public Network { - private: - int client; - //baddr_t *a = nullptr; - public: - Bluetooth(){} +class Bluetooth : public Network { +private: + int client; + // baddr_t *a = nullptr; +public: + Bluetooth() {} - bool init() override { - struct sockaddr_rc loc_addr = { 0 }, rem_addr = { 0 }; + bool init() override { return true; } + + void listen() { + struct sockaddr_rc loc_addr = {0}, rem_addr = {0}; int s; socklen_t opt = sizeof(rem_addr); // allocate socket s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); - if(s < 0){ - return false; + if (s < 0) { + throw s; } // bind socket to port 1 of the first available // local bluetooth adapter loc_addr.rc_family = AF_BLUETOOTH; - //loc_addr.rc_bdaddr = *BDADDR_ANY; - loc_addr.rc_channel = (uint8_t) 1; + // loc_addr.rc_bdaddr = *BDADDR_ANY; + loc_addr.rc_channel = (uint8_t)1; bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); // put socket into listening mode - listen(s, 1); + ::listen(s, 1); // accept one connection client = accept(s, (struct sockaddr *)&rem_addr, &opt); close(s); - if(client < 0){ - return false; - } - return true; - } + } - //TODO - int send(std::string message) override{ - return write(client, message.c_str(), message.size()); - } + // TODO + int send(std::string message) override { + return write(client, message.c_str(), message.size()); + } - //TODO - std::string recv(int bytesToRead) override { - char buf[1024] = { 0 }; - int bytes_read = read(client, buf, sizeof(buf)); - //TODO investigate functionality - // if( bytes_read > 0 ) { - // return std::string(buf); - // } else return ""; - return std::string(buf); - } + // TODO + std::string recv(int bytesToRead) override { + char buf[1024] = {0}; + int bytes_read = read(client, buf, sizeof(buf)); + // TODO investigate functionality + // if( bytes_read > 0 ) { + // return std::string(buf); + // } else return ""; + return std::string(buf); + } - ~Bluetooth(){ - // close connection - close(client); - } - }; -} + ~Bluetooth() { + // close connection + close(client); + } +}; +} // namespace wa #endif
diff --git a/forwarder/include/Connector.hpp b/forwarder/include/Connector.hpp @@ -0,0 +1,50 @@ +#ifndef _CONNECTOR_H +#define _CONNECTOR_H + +#include <iostream> +#include <functional> + +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); + } + } + + public: + Connector(Network &l, Network &r) : l(&l), r(&r){ + l.setOnNetworkConnected([&](Network &n){ + addNetworkAndCheck(n); + }); + r.setOnNetworkConnected([&](Network &n){ + addNetworkAndCheck(n); + }); + } + + void start(){ + l->listen(); + r->listen(); + } + + void setOnPipeCreated(std::function<void(Network &l, Network &r)> listener){ + this->listener = listener; + }; + }; +} + +#endif
diff --git a/forwarder/include/Console.hpp b/forwarder/include/Console.hpp @@ -18,6 +18,10 @@ namespace wa { return message.size(); } + void listen(){ + listener(*this); + } + //FIXME should not read until 'bytesToRead', should also check for 'EOF' std::string recv(int bytesToRead) override { char buffer[bytesToRead];
diff --git a/forwarder/include/Forwarder.hpp b/forwarder/include/Forwarder.hpp @@ -16,14 +16,7 @@ namespace wa { std::thread *t1, *t2; bool shouldStop = false; public: - Forwarder(Network *l, Network *r) : lp(l), rp(r){ - if(!l->init()){ - throw "Left interface not initialized"; - } - if(!r->init()){ - throw "Right interface not initialized"; - return; - } + Forwarder(Network &l, Network &r) : lp(&l), rp(&r){ } //FIXME last 'while' blocked, input expected from the network before closing
diff --git a/forwarder/include/Network.hpp b/forwarder/include/Network.hpp @@ -1,10 +1,19 @@ #ifndef _NETWORK_H #define _NETWORK_H +#include <functional> + namespace wa { class Network { + protected: + std::function<void(Network &)> listener; public: Network(){} + //FIXME add function for unregistering + virtual void setOnNetworkConnected(std::function<void(Network &)> listener){ + this->listener = listener; + } virtual bool init() = 0; + virtual void listen() = 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,15 +20,15 @@ namespace wa { class WiFi : public Network{ int fd; public: - WiFi(){} + WiFi(){ + + } bool init() override { struct ifreq ifr = {0}; bool uar = false; //up_and_running - - fd = comm_start_server(W_DEFAULT_PORT); - strcpy( ifr.ifr_name, "eth0" ); //FIXME allow client to customize - + strcpy( ifr.ifr_name, "enp0s20f0u8" ); //FIXME allow client to customize + int fd = socket(AF_INET, SOCK_STREAM, 0); //evavaluate uar if( ioctl( fd, SIOCGIFFLAGS, &ifr ) != -1 ) uar = (ifr.ifr_flags & ( IFF_UP | IFF_RUNNING )) == ( IFF_UP | IFF_RUNNING ); @@ -36,6 +36,11 @@ namespace wa { return uar; } + void listen(){ + fd = comm_start_server(W_DEFAULT_PORT); + listener(*this); + } + //TODO test int send(std::string message) override { return ::send(fd, message.c_str(), message.size(), 0);
diff --git a/left_initiator/left_initiator.cpp b/left_initiator/left_initiator.cpp @@ -8,6 +8,7 @@ int main(int argc, char *argv[]){ Dot &dot = Dot::getDot(2500); dot.onConnect([](Dot &d){ d.write("hello"); + cout << "Wrote Hello" << endl; }); while(1); return 0;
diff --git a/right_initiator/right_initiator.cpp b/right_initiator/right_initiator.cpp @@ -1,9 +1,11 @@ -#include <stdio.h> +#include <iostream> #include <unistd.h> #include <sys/socket.h> #include <bluetooth/bluetooth.h> #include <bluetooth/rfcomm.h> +using namespace std; + int main(int argc, char *argv[]){ struct sockaddr_rc addr = { 0 }; @@ -22,8 +24,11 @@ int main(int argc, char *argv[]){ // send a message if( status == 0 ) { - char buffer[10]; + cout << "Receing message" << endl; + char buffer[10]; status = recv(s, buffer, 6, 0); + } else { + cout << "Connection failed" << endl; } close(s); while(1);
: bharatvaj/forwarder