commit e616670e87103baccfda469e0c855c736df26c5a
parent e451bd70b9062b9ec7897679eb013169f3ba67d1
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Fri, 26 Apr 2019 18:53:25 +0000
parent e451bd70b9062b9ec7897679eb013169f3ba67d1
Author: Bharatvaj H <bharatvajph@gmail.com>
Date: Fri, 26 Apr 2019 18:53:25 +0000
Added Log, added crosssocket for socket functionality
12 files changed, 147 insertions(+), 69 deletions(-)
M
|
81
+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
diff --git a/CMakeLists.txt b/CMakeLists.txt @@ -4,7 +4,7 @@ project(forwarder) find_package(comm) find_package(Threads) find_package(fmt) -find_package(crossguid) +find_package(crosssocket) find_package(Dot) add_subdirectory(deps/xbee_ansic_library) add_subdirectory(include/network) @@ -18,12 +18,12 @@ target_include_directories(forwarder PRIVATE ${COMM_INCLUDE_LIBS}) target_include_directories(forwarder PRIVATE fmt) target_include_directories(forwarder PRIVATE include) target_include_directories(forwarder PRIVATE xbee) -target_include_directories(forwarder PRIVATE crossguid) target_include_directories(forwarder PRIVATE Dot) +target_include_directories(forwarder PRIVATE crosssocket) target_link_libraries(forwarder ${COMM_LIBS}) target_link_libraries(forwarder ${CMAKE_THREAD_LIBS_INIT}) -target_link_libraries(forwarder crossguid) +target_link_libraries(forwarder crosssocket) target_link_libraries(forwarder ${DOT_LIBS}) target_link_libraries(forwarder fmt) target_link_libraries(forwarder -lbluetooth)
diff --git a/forwarder.cpp b/forwarder.cpp @@ -7,6 +7,7 @@ #include "Shell.hpp" #include <Configuration.hpp> #include <ConfigurationBuilder.hpp> +#include <Log.hpp> #include <NetworkController.hpp> #include <fmt/format.h> #include <model/DeviceInfo.hpp> @@ -93,20 +94,7 @@ void bind_(shell4c::Operation op, vector<string> msg) { shell.println(op.usage); return; } - if (nc->activeNetworks["IP"] == nullptr) { - shell.println("Add IP first"); - return; - } else { - IP *i = (IP *)nc->activeNetworks["IP"]; - int stat = system( - fmt::format("ip address add {} dev {}", msg[0], i->getNetworkParams()) - .c_str()); - if (stat < 0) { - shell.println("Operation failed"); - return; - } - nc->bind(msg[0], msg[1], msg[2], msg[3]); - } + nc->bind(msg[0], msg[1], msg[2], msg[3]); } // FIXME @@ -142,6 +130,7 @@ void help(shell4c::Operation op, vector<string> messages) { int main(int argc, char *argv[]) { signal(SIGINT, exit_handler); + Log::enable(); nc = new NetworkController(); shell.addOperation(new shell4c::Operation( "add", "adds network to ready state", add, "add <network1> [options]"));
diff --git a/include/Forwarder.hpp b/include/Forwarder.hpp @@ -5,6 +5,7 @@ #include <thread> #include <device/Device.hpp> +#include <Log.hpp> #define BUF_SIZE 256 @@ -18,9 +19,10 @@ private: public: Forwarder(Device *lp, Device *rp) : lp(lp), rp(rp) { - std::cout << "Forwarder Started" << std::endl; + Log::i("Forwarder started"); } + //FIXME retransmission error void start() { t1 = new std::thread([&]() { char *str = (char *)calloc(sizeof(char), BUF_SIZE);
diff --git a/include/Log.hpp b/include/Log.hpp @@ -0,0 +1,29 @@ +#ifndef _FLOG_H +#define _FLOG_H +#include <clog/clog.h> +#include <iostream> +static std::string TAG = ""; +class Log { +public: + static void enable() { clog_enable(); } + static void disable() { clog_disable(); } + static void i(std::string msg, std::string tag = TAG) { + clog_i(tag.c_str(), msg.c_str()); + } + static void e(std::string msg, std::string tag = TAG) { + clog_e(tag.c_str(), msg.c_str()); + } + static void w(std::string msg, std::string tag = TAG) { + clog_w(tag.c_str(), msg.c_str()); + } + static void f(std::string msg, std::string tag = TAG) { + clog_f(tag.c_str(), msg.c_str()); + } + static void p(std::string msg, std::string tag = TAG) { + clog_e(tag.c_str(), msg.c_str()); + } + static void v(std::string msg, std::string tag = TAG) { + clog_v(tag.c_str(), msg.c_str()); + } +}; +#endif
diff --git a/include/NetworkController.hpp b/include/NetworkController.hpp @@ -37,7 +37,28 @@ public: void bind(BindInfo *b1, BindInfo *b2) { bindMap[b1] = b2; } + // TODO test: consistency, atomic void bind(std::string n1, std::string cp1, std::string n2, std::string cp2) { + Network *net1 = activeNetworks[n1]; + Network *net2 = activeNetworks[n2]; + if (net1 == nullptr && net2 == nullptr) { + Log::e(fmt::format("Network {} and {} are unavailable", n1, n2)); + return; + } else if (net1 == nullptr || net2 == nullptr) { + Log::e( + fmt::format("Network {} is unavailable", net1 == nullptr ? n2 : n1)); + return; + } + if (net1->bind(cp1) < 0) { + Log::e(fmt::format("Binding failed for net1")); + return; + } + if (net2->bind(cp2) < 0) { + // error + Log::e(fmt::format("Binding failed for net2")); + net1->unbind(cp1); + return; + } BindInfo *bi1 = new BindInfo(); bi1->networkName = n1; bi1->params = cp1;
diff --git a/include/device/BluetoothDevice.hpp b/include/device/BluetoothDevice.hpp @@ -12,15 +12,15 @@ #include <sys/socket.h> #include <unistd.h> -#include <comm.h> +#include <crosssocket.h> namespace wa { class BluetoothDevice : Device { private: - comm_socket s; + xs_SOCKET s; public: - BluetoothDevice(comm_socket s) : s(s) {} + BluetoothDevice(xs_SOCKET s) : s(s) {} int write(char *str, int bytesToSend) override { return ::write(s, str, bytesToSend); @@ -31,4 +31,4 @@ public: } }; } // namespace wa -#endif- \ No newline at end of file +#endif
diff --git a/include/device/ConsoleDevice.hpp b/include/device/ConsoleDevice.hpp @@ -5,8 +5,8 @@ class ConsoleDevice : public Device { public: ConsoleDevice() { address = "console"; } - int read(char *str, int bytesToRead){ - char *msg = str; + int read(char *msg, int bytesToRead){ + FILE *f = fopen(fmt::format("/proc/{}/fd/1").c_str(), "r"); int status = ::read(STDIN_FILENO, msg, bytesToRead-1); msg[bytesToRead-1] = '\0'; return status;
diff --git a/include/device/IPDevice.hpp b/include/device/IPDevice.hpp @@ -16,11 +16,11 @@ namespace wa { class IPDevice : Device { private: - comm_socket fd; + xs_SOCKET fd; bool isTcp; public: - IPDevice(comm_socket fd) : fd(fd), isTcp(true) {} + IPDevice(xs_SOCKET fd) : fd(fd), isTcp(true) {} int read(char *str, int bytesToRead) override { return ::read(fd, str, bytesToRead);
diff --git a/include/network/Bluetooth.hpp b/include/network/Bluetooth.hpp @@ -32,7 +32,7 @@ public: int listen(std::string networkParams) { struct sockaddr_rc loc_addr = {0}, rem_addr = {0}; - comm_socket s; + xs_SOCKET s; socklen_t opt = sizeof(rem_addr); // allocate socket @@ -46,13 +46,13 @@ public: loc_addr.rc_family = AF_BLUETOOTH; loc_addr.rc_bdaddr = inc; loc_addr.rc_channel = (uint8_t)6; - bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); + ::bind(s, (struct sockaddr *)&loc_addr, sizeof(loc_addr)); // put socket into listening mode ::listen(s, 1); // accept one connection - comm_socket c = accept(s, (struct sockaddr *)&rem_addr, &opt); + xs_SOCKET c = accept(s, (struct sockaddr *)&rem_addr, &opt); Device *d = (Device *)new BluetoothDevice(c); d->address = "B8:27:EB:A5:A1:70"; devices.push_back(d); @@ -66,7 +66,7 @@ public: Device *connect(std::string conParams) { struct sockaddr_rc addr = {0}; - comm_socket s; + xs_SOCKET s; const char *dest = conParams.c_str(); // allocate a socket s = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); @@ -80,12 +80,20 @@ public: // connect to server int status = ::connect(s, (struct sockaddr *)&addr, sizeof(addr)); Device *d = (Device *)new BluetoothDevice(s); - if(status < 0){ - return nullptr; + if (status < 0) { + return nullptr; } return d; } + int bind(std::string bindParams) override { + return -1; + } + + int unbind(std::string bindParams) override { + return -1; + } + // TODO std::vector<DeviceInfo> discover() override { std::vector<DeviceInfo> values;
diff --git a/include/network/Console.hpp b/include/network/Console.hpp @@ -24,10 +24,21 @@ public: return 0; } + int bind(std::string bindParams){ + return 0; + } + + int unbind(std::string bindParams){ + return 0; + } + // TODO Device *connect(std::string conParam) override { + Device *d = new ConsoleDevice(); + d->address = conParam; + d->network = this; devices.push_back((Device *)new ConsoleDevice()); - return (Device *)new ConsoleDevice(); + return d; } std::vector<DeviceInfo> discover() override {
diff --git a/include/network/IP.hpp b/include/network/IP.hpp @@ -14,6 +14,7 @@ #include <device/IPDevice.hpp> #include <model/DeviceInfo.hpp> #include <network/Network.hpp> +#include <Log.hpp> using namespace std; @@ -23,7 +24,6 @@ class IP : public Network { std::string networkParams; int configPort = 2500; - public: IP() {} @@ -42,64 +42,75 @@ public: std::string getUserFriendlyName() override { return _IP_H; } - comm_socket start_server(bool isTcp, int port){ - static comm_socket servfd; + xs_SOCKET start_server(bool isTcp, int port) { + static xs_SOCKET servfd; struct sockaddr_in server, client; socklen_t cli_size; - if (comm_check_port(port) < 0) - { - return -1; + if (comm_check_port(port) < 0) { + return -1; } - //Prepare the sockaddr_in structure + // 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); + // 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; + if (servfd == SOCKET_ERROR) { + clog_e(_COMM, "could not create socket"); + return -1; } - //Bind - if (bind(servfd, (struct sockaddr *)&server, sizeof(server)) < 0) - { - log_err(_COMM, "bind failed"); - return -1; + // Bind + if (::bind(servfd, (struct sockaddr *)&server, sizeof(server)) < 0) { + clog_e(_COMM, "bind failed"); + return -1; } - //Listen + // 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; + // Accept and incoming connection + clog_i(_COMM, "Waiting for incoming connections..."); + // accept connection from an incoming client + xs_SOCKET clifd = accept(servfd, (struct sockaddr *)&client, &cli_size); + if (clifd == SOCKET_ERROR) { + clog_i(_COMM, "Accept failed"); + return -1; } - log_inf(_COMM, "Connection accepted"); + clog_i(_COMM, "Connection accepted"); return clifd; } + int bind(std::string params) override { + int stat = system( + fmt::format("ip address add {} dev {}", params, getNetworkParams()) + .c_str()); + if (stat < 0) { + Log::e("Operation failed"); + return -1; + } + return 0; + } + + int unbind(std::string params) override { + return -1; + } + int listen(std::string networkParams) override { - cout << "IP listening" << endl; + cout << "IP listening" << endl; struct sockaddr_in peer; socklen_t peerLen = sizeof(peer); - static int cont; + static int cont; bool isTcp = false; - comm_socket fd = start_server(isTcp, 2500); + xs_SOCKET fd = start_server(isTcp, 2500); - if(fd < 0){ - cout << "Socket error" << endl; + if (fd < 0) { + cout << "Socket error" << endl; } cout << fd << endl; getsockname(fd, (struct sockaddr *)&peer, &peerLen); @@ -123,7 +134,7 @@ public: } Device *connect(std::string conParams) override { - comm_socket fd = ::comm_connect_server(conParams.c_str(), 21); + xs_SOCKET fd = ::comm_connect_server(conParams.c_str(), 21); if (fd == SOCKET_ERROR) { return nullptr; }
diff --git a/include/network/Network.hpp b/include/network/Network.hpp @@ -39,6 +39,14 @@ public: virtual int listen(std::string) = 0; // connects to the given address virtual Device *connect(std::string) = 0; + // bind a route to system + virtual int bind(std::string) { + return -1; + } + // unbind a route from system + virtual int unbind(std::string) { + return -1; + } // discovers different networks virtual std::vector<DeviceInfo> discover() = 0; // should be deprecated
: bharatvaj/forwarder