From 725766be7fb2dfe96406270b6879c8da3271eb94 Mon Sep 17 00:00:00 2001 From: Nicolas Pope <nwpope@utu.fi> Date: Fri, 29 Mar 2019 11:59:13 +0200 Subject: [PATCH] Update net code file structure --- net/README.md | 12 +++ net/{ => cpp}/CMakeLists.txt | 0 net/{ => cpp}/include/ftl/net.hpp | 0 net/{ => cpp}/include/ftl/net/dispatcher.hpp | 0 net/{ => cpp}/include/ftl/net/func_traits.hpp | 0 net/{ => cpp}/include/ftl/net/handlers.hpp | 0 net/{ => cpp}/include/ftl/net/ice.hpp | 0 net/{ => cpp}/include/ftl/net/listener.hpp | 0 net/{ => cpp}/include/ftl/net/p2p.hpp | 0 net/{ => cpp}/include/ftl/net/protocol.hpp | 0 net/{ => cpp}/include/ftl/net/socket.hpp | 0 net/{ => cpp}/include/ftl/net/stun.hpp | 0 net/{ => cpp}/include/ftl/uri.hpp | 0 net/{ => cpp}/include/ftl/uuid.hpp | 0 net/{ => cpp}/src/dispatcher.cpp | 0 net/{ => cpp}/src/ice.cpp | 0 net/{ => cpp}/src/listener.cpp | 0 net/cpp/src/main.cpp | 84 +++++++++++++++++++ net/{ => cpp}/src/net.cpp | 0 net/{ => cpp}/src/p2p.cpp | 0 net/{ => cpp}/src/protocol.cpp | 0 net/{ => cpp}/src/socket.cpp | 0 net/{ => cpp}/test/CMakeLists.txt | 0 net/{ => cpp}/test/catch.hpp | 0 net/{ => cpp}/test/ice.cpp | 0 net/{ => cpp}/test/net_integration.cpp | 0 net/{ => cpp}/test/p2p_base_unit.cpp | 0 net/{ => cpp}/test/protocol_unit.cpp | 0 net/{ => cpp}/test/socket_unit.cpp | 0 net/{ => cpp}/test/tests.cpp | 0 net/{ => cpp}/test/uri_unit.cpp | 0 31 files changed, 96 insertions(+) create mode 100644 net/README.md rename net/{ => cpp}/CMakeLists.txt (100%) rename net/{ => cpp}/include/ftl/net.hpp (100%) rename net/{ => cpp}/include/ftl/net/dispatcher.hpp (100%) rename net/{ => cpp}/include/ftl/net/func_traits.hpp (100%) rename net/{ => cpp}/include/ftl/net/handlers.hpp (100%) rename net/{ => cpp}/include/ftl/net/ice.hpp (100%) rename net/{ => cpp}/include/ftl/net/listener.hpp (100%) rename net/{ => cpp}/include/ftl/net/p2p.hpp (100%) rename net/{ => cpp}/include/ftl/net/protocol.hpp (100%) rename net/{ => cpp}/include/ftl/net/socket.hpp (100%) rename net/{ => cpp}/include/ftl/net/stun.hpp (100%) rename net/{ => cpp}/include/ftl/uri.hpp (100%) rename net/{ => cpp}/include/ftl/uuid.hpp (100%) rename net/{ => cpp}/src/dispatcher.cpp (100%) rename net/{ => cpp}/src/ice.cpp (100%) rename net/{ => cpp}/src/listener.cpp (100%) create mode 100644 net/cpp/src/main.cpp rename net/{ => cpp}/src/net.cpp (100%) rename net/{ => cpp}/src/p2p.cpp (100%) rename net/{ => cpp}/src/protocol.cpp (100%) rename net/{ => cpp}/src/socket.cpp (100%) rename net/{ => cpp}/test/CMakeLists.txt (100%) rename net/{ => cpp}/test/catch.hpp (100%) rename net/{ => cpp}/test/ice.cpp (100%) rename net/{ => cpp}/test/net_integration.cpp (100%) rename net/{ => cpp}/test/p2p_base_unit.cpp (100%) rename net/{ => cpp}/test/protocol_unit.cpp (100%) rename net/{ => cpp}/test/socket_unit.cpp (100%) rename net/{ => cpp}/test/tests.cpp (100%) rename net/{ => cpp}/test/uri_unit.cpp (100%) diff --git a/net/README.md b/net/README.md new file mode 100644 index 000000000..d563fc6ab --- /dev/null +++ b/net/README.md @@ -0,0 +1,12 @@ +# FTL Network Library +FTL Net provides stream, RPC and Peer-2-Peer functionality for the FTL system. +The idea is to allow an efficient mapping to operating system sockets to +minimise userspace copy operations, whilst still allowing for data packing for +smaller RPC calls. The P2P component implements different rpc search strategies +to allow calls to find one, all, many or specific results across the network. + +Multiple protocols are supported, and it is intended that NAT traversal will be +included. However, security, whether encryption or identification, is not +considered presently. + + diff --git a/net/CMakeLists.txt b/net/cpp/CMakeLists.txt similarity index 100% rename from net/CMakeLists.txt rename to net/cpp/CMakeLists.txt diff --git a/net/include/ftl/net.hpp b/net/cpp/include/ftl/net.hpp similarity index 100% rename from net/include/ftl/net.hpp rename to net/cpp/include/ftl/net.hpp diff --git a/net/include/ftl/net/dispatcher.hpp b/net/cpp/include/ftl/net/dispatcher.hpp similarity index 100% rename from net/include/ftl/net/dispatcher.hpp rename to net/cpp/include/ftl/net/dispatcher.hpp diff --git a/net/include/ftl/net/func_traits.hpp b/net/cpp/include/ftl/net/func_traits.hpp similarity index 100% rename from net/include/ftl/net/func_traits.hpp rename to net/cpp/include/ftl/net/func_traits.hpp diff --git a/net/include/ftl/net/handlers.hpp b/net/cpp/include/ftl/net/handlers.hpp similarity index 100% rename from net/include/ftl/net/handlers.hpp rename to net/cpp/include/ftl/net/handlers.hpp diff --git a/net/include/ftl/net/ice.hpp b/net/cpp/include/ftl/net/ice.hpp similarity index 100% rename from net/include/ftl/net/ice.hpp rename to net/cpp/include/ftl/net/ice.hpp diff --git a/net/include/ftl/net/listener.hpp b/net/cpp/include/ftl/net/listener.hpp similarity index 100% rename from net/include/ftl/net/listener.hpp rename to net/cpp/include/ftl/net/listener.hpp diff --git a/net/include/ftl/net/p2p.hpp b/net/cpp/include/ftl/net/p2p.hpp similarity index 100% rename from net/include/ftl/net/p2p.hpp rename to net/cpp/include/ftl/net/p2p.hpp diff --git a/net/include/ftl/net/protocol.hpp b/net/cpp/include/ftl/net/protocol.hpp similarity index 100% rename from net/include/ftl/net/protocol.hpp rename to net/cpp/include/ftl/net/protocol.hpp diff --git a/net/include/ftl/net/socket.hpp b/net/cpp/include/ftl/net/socket.hpp similarity index 100% rename from net/include/ftl/net/socket.hpp rename to net/cpp/include/ftl/net/socket.hpp diff --git a/net/include/ftl/net/stun.hpp b/net/cpp/include/ftl/net/stun.hpp similarity index 100% rename from net/include/ftl/net/stun.hpp rename to net/cpp/include/ftl/net/stun.hpp diff --git a/net/include/ftl/uri.hpp b/net/cpp/include/ftl/uri.hpp similarity index 100% rename from net/include/ftl/uri.hpp rename to net/cpp/include/ftl/uri.hpp diff --git a/net/include/ftl/uuid.hpp b/net/cpp/include/ftl/uuid.hpp similarity index 100% rename from net/include/ftl/uuid.hpp rename to net/cpp/include/ftl/uuid.hpp diff --git a/net/src/dispatcher.cpp b/net/cpp/src/dispatcher.cpp similarity index 100% rename from net/src/dispatcher.cpp rename to net/cpp/src/dispatcher.cpp diff --git a/net/src/ice.cpp b/net/cpp/src/ice.cpp similarity index 100% rename from net/src/ice.cpp rename to net/cpp/src/ice.cpp diff --git a/net/src/listener.cpp b/net/cpp/src/listener.cpp similarity index 100% rename from net/src/listener.cpp rename to net/cpp/src/listener.cpp diff --git a/net/cpp/src/main.cpp b/net/cpp/src/main.cpp new file mode 100644 index 000000000..a63f538a6 --- /dev/null +++ b/net/cpp/src/main.cpp @@ -0,0 +1,84 @@ +#include <string> +#include <iostream> +#include <ftl/net/p2p.hpp> +#include <ftl/net/listener.hpp> +#include <ftl/net/socket.hpp> +#include <memory> +#include <thread> +#include <readline/readline.h> + +using std::string; +using std::shared_ptr; +using ftl::net::P2P; +using ftl::net::Listener; +using ftl::net::Socket; + +static P2P p2p("ftl://cli"); +static shared_ptr<Listener> listener = nullptr; +static volatile bool stop = false; + +void handle_options(const char ***argv, int *argc) { + while (*argc > 0) { + string cmd((*argv)[0]); + if (cmd[0] != '-') break; + + if (cmd.find("--peer=") == 0) { + cmd = cmd.substr(cmd.find("=")+1); + //std::cout << "Peer added " << cmd.substr(cmd.find("=")+1) << std::endl; + p2p.addPeer(cmd); + } else if (cmd.find("--listen=") == 0) { + cmd = cmd.substr(cmd.find("=")+1); + listener = ftl::net::listen(cmd.c_str()); + if (listener) listener->setProtocol(&p2p); + listener->onConnection([](shared_ptr<Socket> &s) { p2p.addPeer(s); }); + } + + (*argc)--; + (*argv)++; + } +} + +void run() { + while (!stop) ftl::net::wait(); +} + +void handle_command(const char *l) { + string cmd = string(l); + + if (cmd == "exit") { + stop = true; + } else if (cmd.find("peer ") == 0) { + cmd = cmd.substr(cmd.find(" ")+1); + p2p.addPeer(cmd); + } else if (cmd.find("list ") == 0) { + cmd = cmd.substr(cmd.find(" ")+1); + if (cmd == "peers") { + auto res = p2p.getPeers(); + for (auto r : res) std::cout << " " << r.to_string() << std::endl; + } + } +} + +int main(int argc, const char **argv) { + argc--; + argv++; + + // Process Arguments + handle_options(&argv, &argc); + + std::thread nthread(run); + + while (!stop) { + char *line = readline("> "); + if (!line) break; + + handle_command(line); + + free(line); + } + stop = true; + + nthread.join(); + return 0; +} + diff --git a/net/src/net.cpp b/net/cpp/src/net.cpp similarity index 100% rename from net/src/net.cpp rename to net/cpp/src/net.cpp diff --git a/net/src/p2p.cpp b/net/cpp/src/p2p.cpp similarity index 100% rename from net/src/p2p.cpp rename to net/cpp/src/p2p.cpp diff --git a/net/src/protocol.cpp b/net/cpp/src/protocol.cpp similarity index 100% rename from net/src/protocol.cpp rename to net/cpp/src/protocol.cpp diff --git a/net/src/socket.cpp b/net/cpp/src/socket.cpp similarity index 100% rename from net/src/socket.cpp rename to net/cpp/src/socket.cpp diff --git a/net/test/CMakeLists.txt b/net/cpp/test/CMakeLists.txt similarity index 100% rename from net/test/CMakeLists.txt rename to net/cpp/test/CMakeLists.txt diff --git a/net/test/catch.hpp b/net/cpp/test/catch.hpp similarity index 100% rename from net/test/catch.hpp rename to net/cpp/test/catch.hpp diff --git a/net/test/ice.cpp b/net/cpp/test/ice.cpp similarity index 100% rename from net/test/ice.cpp rename to net/cpp/test/ice.cpp diff --git a/net/test/net_integration.cpp b/net/cpp/test/net_integration.cpp similarity index 100% rename from net/test/net_integration.cpp rename to net/cpp/test/net_integration.cpp diff --git a/net/test/p2p_base_unit.cpp b/net/cpp/test/p2p_base_unit.cpp similarity index 100% rename from net/test/p2p_base_unit.cpp rename to net/cpp/test/p2p_base_unit.cpp diff --git a/net/test/protocol_unit.cpp b/net/cpp/test/protocol_unit.cpp similarity index 100% rename from net/test/protocol_unit.cpp rename to net/cpp/test/protocol_unit.cpp diff --git a/net/test/socket_unit.cpp b/net/cpp/test/socket_unit.cpp similarity index 100% rename from net/test/socket_unit.cpp rename to net/cpp/test/socket_unit.cpp diff --git a/net/test/tests.cpp b/net/cpp/test/tests.cpp similarity index 100% rename from net/test/tests.cpp rename to net/cpp/test/tests.cpp diff --git a/net/test/uri_unit.cpp b/net/cpp/test/uri_unit.cpp similarity index 100% rename from net/test/uri_unit.cpp rename to net/cpp/test/uri_unit.cpp -- GitLab