From 614d0bf07789457a97d194c4af9cc7393f871351 Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Fri, 5 Jul 2024 16:50:33 +0200 Subject: Working on loopback data transmission --- modules/codec/c++/remote.hpp | 116 ++++++++++++++++++++++++++ modules/codec/c++/remote_loopback.hpp | 16 ++-- modules/codec/c++/rpc.hpp | 116 -------------------------- modules/codec/c++/transfer.hpp | 12 +++ modules/codec/c++/transfer_loopback.hpp | 140 +++++++++++++++++++++++++++++++- 5 files changed, 274 insertions(+), 126 deletions(-) create mode 100644 modules/codec/c++/remote.hpp delete mode 100644 modules/codec/c++/rpc.hpp (limited to 'modules/codec/c++') diff --git a/modules/codec/c++/remote.hpp b/modules/codec/c++/remote.hpp new file mode 100644 index 0000000..5803154 --- /dev/null +++ b/modules/codec/c++/remote.hpp @@ -0,0 +1,116 @@ +#pragma once + +#include +#include + +#include "data.hpp" +#include "interface.hpp" + +#include + +namespace saw { +/** + * This class acts as a helper for rpc calls and representing data on the remote. + */ +template +class data_or_id { +private: + /** + * Variant representing the either id or data class. + */ + std::variant, data> doi_; +public: + /** + * Constructor for instantiating. + */ + data_or_id(const id& val): + doi_{val} + {} + + /** + * Constructor for instantiating. + */ + data_or_id(data val): + doi_{std::move(val)} + {} + + /** + * Check if this class holds an id. + */ + bool is_id() const { + return std::holds_alternative>(doi_); + } + + /** + * Check if this class holds data. + */ + bool is_data() const { + return std::holds_alternative>(doi_); + } + + /** + * Returns the id. + */ + id get_id() const { + return std::get>(doi_); + } + + /** + * Return a data reference. + */ + data& get_data(){ + return std::get>(doi_); + } + + /** + * Return a data reference. + */ + const data& get_data() const { + return std::get>(doi_); + } +}; + + +/** + * Representing data on the remote + */ +template +class remote_data; + +template +class rpc_client; + +/** + * Implementation of a remote server on the backend + */ +template +class rpc_server { +private: + interface iface_; +public: + rpc_server(interface iface): + iface_{std::move(iface)} + {} +}; + +/** + * Representation of a remote. + * Partially similar to a network address + */ +template +class remote_address { + static_assert(always_false, "Type of remote not supported"); + + /** + * + */ +}; + +/** + * Reference Backend structure + */ +template +class remote { + static_assert(always_false, "Type of backend not supported"); +}; +} diff --git a/modules/codec/c++/remote_loopback.hpp b/modules/codec/c++/remote_loopback.hpp index 22ca15f..2949243 100644 --- a/modules/codec/c++/remote_loopback.hpp +++ b/modules/codec/c++/remote_loopback.hpp @@ -1,9 +1,9 @@ #pragma once -#include - #include +#include "interface.hpp" +#include "remote.hpp" #include "transfer_loopback.hpp" namespace saw { @@ -37,7 +37,7 @@ class rpc_client { * request the data from the remote */ template - remote_data request_data(id data); + remote_data request_data(id data); /** @todo * Determine type based on Name @@ -64,19 +64,23 @@ private: const remote_address* addr_; InterfaceT iface_; public: - rpc_server(const remode_address& addr__, InterfaceT iface__): + rpc_server(const remote_address& addr__, InterfaceT iface__): addr_{&addr__}, iface_{std::move(iface__)} {} + + // error_or> }; template<> class remote { - +public: /** * Resolves an address for the remote */ - conveyor> resolve_address(); + error_or>> parse_address(){ + return heap>(); + } /** * Connect to a remote diff --git a/modules/codec/c++/rpc.hpp b/modules/codec/c++/rpc.hpp deleted file mode 100644 index 6b63580..0000000 --- a/modules/codec/c++/rpc.hpp +++ /dev/null @@ -1,116 +0,0 @@ -#pragma once - -#include -#include -#include - -#include - -#include - -namespace saw { -/** - * This class acts as a helper for rpc calls and representing data on the remote. - */ -template -class data_or_id { -private: - /** - * Variant representing the either id or data class. - */ - std::variant, data> doi_; -public: - /** - * Constructor for instantiating. - */ - data_or_id(const id& val): - doi_{val} - {} - - /** - * Constructor for instantiating. - */ - data_or_id(data val): - doi_{std::move(val)} - {} - - /** - * Check if this class holds an id. - */ - bool is_id() const { - return std::holds_alternative>(doi_); - } - - /** - * Check if this class holds data. - */ - bool is_data() const { - return std::holds_alternative>(doi_); - } - - /** - * Returns the id. - */ - id get_id() const { - return std::get>(doi_); - } - - /** - * Return a data reference. - */ - data& get_data(){ - return std::get>(doi_); - } - - /** - * Return a data reference. - */ - const data& get_data() const { - return std::get>(doi_); - } -}; - - -/** - * Representing data on the remote - */ -template -class remote_data; - -template -class rpc_client; - -/** - * Implementation of a remote server on the backend - */ -template -class rpc_server { -private: - interface iface_; -public: - rpc_server(interface iface): - iface_{std::move(iface)} - {} -}; - -/** - * Representation of a remote. - * Partially similar to a network address - */ -template -class remote_address { - static_assert(always_false, "Type of remote not supported"); - - /** - * - */ -}; - -/** - * Reference Backend structure - */ -template -class remote { - static_assert(always_false, "Type of backend not supported"); -}; -} diff --git a/modules/codec/c++/transfer.hpp b/modules/codec/c++/transfer.hpp index 1fb297e..2fdd0b9 100644 --- a/modules/codec/c++/transfer.hpp +++ b/modules/codec/c++/transfer.hpp @@ -6,4 +6,16 @@ class data_server; template class data_client; + +namespace impl { +template +struct data_server_redux { + using type = std::tuple<>; +}; + +template +struct data_server_redux> { + using type = std::tuple>...>; +}; +} } diff --git a/modules/codec/c++/transfer_loopback.hpp b/modules/codec/c++/transfer_loopback.hpp index ec61669..6c81c9c 100644 --- a/modules/codec/c++/transfer_loopback.hpp +++ b/modules/codec/c++/transfer_loopback.hpp @@ -1,20 +1,152 @@ #pragma once +#include + +#include "transfer.hpp" + namespace saw { namespace rmt { struct Loopback {}; } -template -class data_server { +template +class data_server, Encoding, rmt::Loopback> { private: + typename impl::data_server_redux>::type>::type values_; public: + /** + * Get data from client + */ + template + error_or send(const data& dat, id store_id){ + auto& vals = std::get>>(values_); + + try { + auto insert_res = vals.emplace(std::make_pair(store_id.get_value(), std::move(dat))); + if(!insert_res.second){ + return make_error(); + } + }catch(std::exception& ){ + return make_error(); + } + return void_t{}; + } + + template + error_or> receive(id store_id){ + auto& vals = std::get>>(values_); + + auto find_res = vals.find(store_id.get_value()); + if(find_res == vals.end()){ + return make_error(); + } + + auto& dat = find_res->second; + return dat; + } + + template + error_or erase(id store_id){ + auto& vals = std::get>>(values_); + auto erase_op = vals.erase(store_id.get_value()); + if(erase_op == 0u){ + return make_error(); + } + return void_t{}; + } + + template + error_or*> find(id store_id){ + auto& vals = std::get>>(values_); + auto find_res = vals.find(store_id.get_value()); + if(find_res == vals.end()){ + return make_error(); + } + + auto& dat = find_res->second; + return &dat; + } }; -template -class data_client { +/** + * Client for transporting data to remote and receiving data back + */ +template +class data_client, Encoding, rmt::Loopback> { private: + /** + * Corresponding server for this client + */ + data_server, Encoding, rmt::Loopback>* srv_; + + /** + * The next id for identifying issues on the remote side. + */ + uint64_t next_id_; public: + /** + * Main constructor + */ + data_client(data_server, Encoding, rmt::Loopback>& srv__): + srv_{&srv__}, + next_id_{0u} + {} + + /** + * Send data to the remote. + */ + template + error_or> send(const data& dat){ + id dat_id{next_id_}; + auto eov = srv_->send(dat, dat_id); + if(eov.is_error()){ + auto& err = eov.get_error(); + return std::move(err); + } + + ++next_id_; + return dat_id; + } + + /** + * Receive data + */ + template + conveyor> receive(id dat_id){ + auto eov = srv_->receive(dat_id); + if(eov.is_error()){ + auto& err = eov.get_error(); + return std::move(err); + } + + auto& val = eov.get_value(); + return std::move(val); + } + + /** + * Erase data + */ + template + error_or erase(id dat_id){ + return srv_->erase(dat_id); + } + + /** + * An exception for the Loopback backend. Here we can safely use find from + * the client side. + */ + template + error_or*> find(id dat_id){ + auto eov = srv_->find(dat_id); + if(eov.is_error()){ + auto& err = eov.get_error(); + return std::move(err); + } + + auto val = eov.get_value(); + return val; + } + }; } -- cgit v1.2.3