commit c3bc873b053f4a2ad06b33fc4313982127123786
parent 31748c4645c53429dcd8dd659e7d94c1761543ae
Author: Bharatvaj H <bharatvaj@yahoo.com>
Date: Mon, 25 Mar 2019 21:55:42 +0000
parent 31748c4645c53429dcd8dd659e7d94c1761543ae
Author: Bharatvaj H <bharatvaj@yahoo.com>
Date: Mon, 25 Mar 2019 21:55:42 +0000
Corrected Forwarder startup, fixed occurence of garbage values in Forwarder
9 files changed, 76 insertions(+), 75 deletions(-)
diff --git a/forwarder.cpp b/forwarder.cpp @@ -18,7 +18,7 @@ using namespace wa; using namespace shell4c; NetworkController *nc; -Shell shell(false, "forwarder> "); +Shell shell(false, "adapter> "); void exit_handler(int sig) { if (nc != nullptr) { @@ -81,9 +81,14 @@ void list(shell4c::Operation op, vector<string> msg) { } void start(shell4c::Operation op, vector<string> msg) { + if (nc->activeNetworks.size() == 0) { + shell.print("Nothing to start, atleast one network is required\n"); + return; + } for (auto network : nc->activeNetworks) { - network.second->setOnDeviceConnected([](Device &d){ + network.second->setOnDeviceConnected([](Device &d) { Forwarder::addDevice(&d); + fmt::print("added device: {}\n", d.address); }); network.second->listen(); } @@ -118,11 +123,13 @@ void bind_(shell4c::Operation op, vector<string> msg) { // } // } +void select_(shell4c::Operation op, vector<string> messages) {} + void help(shell4c::Operation op, vector<string> messages) { auto ops = shell.getOperations(); if (messages.size() == 0) { for (auto op : ops) { - shell.println(fmt::format("{} - {}", op.first, op.second->description)); + shell.println(fmt::format("\u001b[33m{}\u001b[0m\t- {}", op.first, op.second->description)); } return; } @@ -136,8 +143,6 @@ void help(shell4c::Operation op, vector<string> messages) { int main(int argc, char *argv[]) { signal(SIGINT, exit_handler); - std::cout << "Initializing please wait..." << std::endl; - nc = new NetworkController(); shell.addOperation(new shell4c::Operation( "add", "adds network to ready state", add, "add <network1> [options]")); @@ -154,6 +159,9 @@ int main(int argc, char *argv[]) { new shell4c::Operation("start", "start forwarder", start, "start")); shell.addOperation(new shell4c::Operation("help", "prints this menu", help, "help [operation1 ...]")); + shell.addOperation( + new shell4c::Operation("select", "select a network to perform operations", + select_, "select <network> [<network2>]")); shell.setOnShellExit(exit_handler); shell.start(); while (1)
diff --git a/include/Forwarder.hpp b/include/Forwarder.hpp @@ -30,20 +30,14 @@ private: if (n1.second->d == nullptr) { return nullptr; } else { - auto p = (new std::pair<BindPair *, BindPair *>()); - p->first = n1.first; - p->second = n1.second; - return p; + return new std::pair<BindPair *, BindPair *>(n1); } } else if (d->address == n1.second->a) { n1.second->d = d; if (n1.first->d == nullptr) { return nullptr; } else { - auto p = (new std::pair<BindPair *, BindPair *>()); - p->first = n1.first; - p->second = n1.second; - return p; + return new std::pair<BindPair *, BindPair *>(n1); } } } @@ -51,25 +45,36 @@ private: } public: - Forwarder(Device *lp, Device *rp) : lp(lp), rp(rp) {} + Forwarder(Device *lp, Device *rp) : lp(lp), rp(rp) { + std::cout << "Forwarder Started" << std::endl; + } static void addDevice(Device *d) { std::pair<BindPair *, BindPair *> *pair = findAdd(d); if (pair) { - forwarders.push_back(new Forwarder(pair->first->d, pair->second->d)); + Forwarder *forwarder = new Forwarder(pair->first->d, pair->second->d); + forwarders.push_back(forwarder); + forwarder->start(); } } - // FIXME last 'while' blocked, input expected from the network before closing void start() { t1 = new std::thread([&]() { - while (!shouldStop) { - rp->send(lp->recv(BUF_SIZE)); + char *str = (char *)calloc(sizeof(char), BUF_SIZE); + int bytesRead = 0; + while ((bytesRead = lp->read(str, BUF_SIZE-1)) > 0) { + if (rp->write(str, bytesRead) < 0) { + break; + }; } }); t2 = new std::thread([&]() { - while (!shouldStop) { - lp->send(rp->recv(BUF_SIZE)); + char *str = (char *)calloc(sizeof(char), BUF_SIZE); + int bytesRead = 0; + while ((bytesRead = rp->read(str, BUF_SIZE-1)) > 0) { + if (lp->write(str, bytesRead) < 0) { + break; + } } }); }
diff --git a/include/device/BluetoothDevice.hpp b/include/device/BluetoothDevice.hpp @@ -22,20 +22,12 @@ private: public: BluetoothDevice(comm_socket s) : s(s) {} - // TODO - int send(std::string message) override { - return write(s, message.c_str(), message.size()); + int write(char *str, int bytesToSend) override { + return ::write(s, str, bytesToSend); } - // TODO - std::string recv(int bytesToRead) override { - char buf[1024] = {0}; - int bytes_read = read(s, buf, sizeof(buf)); - // TODO investigate functionality - // if( bytes_read > 0 ) { - // return std::string(buf); - // } else return ""; - return std::string(buf); + int read(char *str, int bytesToRead) override { + return ::read(s, str, bytesToRead); } }; } // namespace wa
diff --git a/include/device/ConsoleDevice.hpp b/include/device/ConsoleDevice.hpp @@ -0,0 +1,20 @@ +#include <cstdio> +#include <unistd.h> +namespace wa { +class ConsoleDevice : public Device { +public: + ConsoleDevice() { address = "console"; } + + int read(char *str, int bytesToRead){ + char *msg = str; + int status = ::read(STDIN_FILENO, msg, bytesToRead-1); + msg[bytesToRead-1] = '\0'; + return status; + } + + int write(char *str, int bytesToWrite){ + return ::write(STDOUT_FILENO, str, bytesToWrite); + } + +}; +} // namespace wa
diff --git a/include/device/Device.hpp b/include/device/Device.hpp @@ -6,8 +6,8 @@ namespace wa { class Device : public NetworkInfo { public: Device() {} - virtual int send(std::string msgToSend) = 0; - virtual std::string recv(int bytesToRead) = 0; + virtual int read(char *message, int bytesToRead) = 0; + virtual int write(char *message, int bytesToWrite) = 0; }; } // namespace wa #endif \ No newline at end of file
diff --git a/include/device/IPDevice.hpp b/include/device/IPDevice.hpp @@ -23,18 +23,14 @@ private: 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); + int read(char *str, int bytesToRead) override { + return ::read(fd, str, bytesToRead); } - // TODO test - int send(std::string message) override { - return ::send(fd, message.c_str(), message.size(), 0); + int write(char *str, int bytesToSend) override { + return ::write(fd, str, bytesToSend); } + ~IPDevice() { comm_close_socket(fd); } }; } // namespace wa
diff --git a/include/device/ZWaveDevice.hpp b/include/device/ZWaveDevice.hpp @@ -10,12 +10,12 @@ public: ZWaveDevice() {} // TODO test - int send(std::string message) override { return -1; } + int write(char *msg, int bytesToSend) override { return -1; } // TODO test - std::string recv(int bytesToRead) override { - // zigbee recv - return ""; + int read(char *msg, int bytesToRead) override { + // zwave recv + return -1; } }; } // namespace wa
diff --git a/include/device/ZigBeeDevice.hpp b/include/device/ZigBeeDevice.hpp @@ -10,12 +10,12 @@ public: ZigBeeDevice() {} // TODO test - int send(std::string message) override { return -1; } + int write(char *msg, int bytesToSend) override { return -1; } // TODO test - std::string recv(int bytesToRead) override { + int read(char *msg, int bytesToRead) override { // zigbee recv - return ""; + return -1; } }; } // namespace wa
diff --git a/include/network/Console.hpp b/include/network/Console.hpp @@ -1,34 +1,14 @@ #ifndef _CONSOLE_H #define _CONSOLE_H "Console" -#include <network/Network.hpp> #include <iostream> #include <model/NetworkInfo.hpp> +#include <network/Network.hpp> #include <vector> -namespace wa { - -class ConsoleDevice : Device { -public: - std::string consoleType; - ConsoleDevice(std::string consoleType) : consoleType(consoleType) {} - - int send(std::string message) override { - std::cout << message; - return message.size(); - } +#include <device/ConsoleDevice.hpp> - // FIXME should not read until 'bytesToRead', should also check for 'EOF' - std::string recv(int bytesToRead) override { - char buffer[bytesToRead]; - char c = 'a'; - int i = 0; - while (i < bytesToRead) { - buffer[i++] = getc(stdin); - } - return buffer; - } -}; +namespace wa { class Console : public Network { public: Console() {} @@ -38,7 +18,7 @@ public: std::string getUserFriendlyName() override { return _CONSOLE_H; } int listen() override { - Device *d = (Device *)new ConsoleDevice("stdout"); + Device *d = (Device *)new ConsoleDevice(); devices.push_back(d); deviceConnected(*d); return 0; @@ -46,7 +26,7 @@ public: // TODO int connect(std::string conParam) override { - devices.push_back((Device *)new ConsoleDevice(conParam)); + devices.push_back((Device *)new ConsoleDevice()); return 0; }
: bharatvaj/forwarder