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/.nix/derivation.nix | 1 + modules/codec/SConstruct | 5 +- 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 +++++++++++++++++++++++++++++++- modules/codec/tests/remote_loopback.cpp | 51 +++++++++++- modules/remote-sycl/c++/transfer.hpp | 17 +--- 9 files changed, 329 insertions(+), 145 deletions(-) create mode 100644 modules/codec/c++/remote.hpp delete mode 100644 modules/codec/c++/rpc.hpp diff --git a/modules/codec/.nix/derivation.nix b/modules/codec/.nix/derivation.nix index db27a5c..7f7811a 100644 --- a/modules/codec/.nix/derivation.nix +++ b/modules/codec/.nix/derivation.nix @@ -17,6 +17,7 @@ in stdenv.mkDerivation { buildInputs = [ forstio.core + forstio.async ]; nativeBuildInputs = [ diff --git a/modules/codec/SConstruct b/modules/codec/SConstruct index d4bc519..3773a17 100644 --- a/modules/codec/SConstruct +++ b/modules/codec/SConstruct @@ -47,7 +47,10 @@ env=Environment(ENV=os.environ, variables=env_vars, CPPPATH=[], CXX=['c++'], CPPDEFINES=['SAW_UNIX'], CXXFLAGS=['-std=c++20','-g','-Wall','-Wextra'], - LIBS=['forstio-core'] + LIBS=[ + 'forstio-core', + 'forstio-async' + ] ) env.__class__.add_source_files = add_kel_source_files env.Tool('compilation_db'); 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; + } + }; } diff --git a/modules/codec/tests/remote_loopback.cpp b/modules/codec/tests/remote_loopback.cpp index 2f6c06c..2895ee7 100644 --- a/modules/codec/tests/remote_loopback.cpp +++ b/modules/codec/tests/remote_loopback.cpp @@ -1,6 +1,6 @@ #include -#include "remote_loopback.hpp" +#include "../c++/remote_loopback.hpp" namespace { namespace sch { @@ -9,9 +9,16 @@ using namespace saw::schema; using TestInterface = Interface< Member, "foo"> >; + +using GroupedSchemas = saw::tmpl_group< + UInt64, + String, + Array, + Float64 +>; } -SAW_TEST("Remote Loopback"){ +SAW_TEST("Remote Loopback Data"){ using namespace saw; remote rmt; @@ -22,9 +29,45 @@ SAW_TEST("Remote Loopback"){ interface iface{ [](data& foo){ - return foo.template cast(); + return foo.template cast_to(); } }; - auto rpc_srv = rmt.listen(*val, std::move(iface)); + + auto srv = data_server{}; + auto client = data_client{srv}; + + data foo{421}; + id sent_id = [&](){ + auto eov = client.send(foo); + SAW_EXPECT(eov.is_value(), "Failed send."); + return eov.get_value(); + }(); + + event_loop loop; + wait_scope wait{loop}; + + { + auto conv = client.receive(sent_id); + auto eov = conv.take(); + + SAW_EXPECT(eov.is_value(), "Failed receive."); + SAW_EXPECT(eov.get_value() == foo, "Wrong received value."); + } + { + auto eov = client.find(sent_id); + SAW_EXPECT(eov.is_value(), "Failed find."); + auto& f_val = eov.get_value(); + SAW_EXPECT(f_val, "Nullptr in find."); + SAW_EXPECT(*f_val == foo, "Wrong received value."); + } + { + auto eov = client.erase(sent_id); + SAW_EXPECT(eov.is_value(), "Failed erase."); + } + { + auto conv = client.receive(sent_id); + auto eov = conv.take(); + SAW_EXPECT(!eov.is_value(), "Failed receive. Value should already be erased."); + } } } diff --git a/modules/remote-sycl/c++/transfer.hpp b/modules/remote-sycl/c++/transfer.hpp index 72b111f..2a95f67 100644 --- a/modules/remote-sycl/c++/transfer.hpp +++ b/modules/remote-sycl/c++/transfer.hpp @@ -9,17 +9,6 @@ #include namespace saw { -namespace impl { -template -struct data_server_redux { - using type = std::tuple<>; -}; - -template -struct data_server_redux> { - using type = std::tuple>...>; -}; -} template class data_server, Encoding, rmt::Sycl> { @@ -32,7 +21,7 @@ private: /** * Store for the data the server manages. */ - typename impl::data_server_redux>::type >::type values_; + typename impl::data_server_redux>::type >::type values_; public: /** * Main constructor @@ -85,7 +74,7 @@ public: */ template error_or erase(id store_id){ - auto& vals = std::get(values_); + auto& vals = std::get>>(values_); auto erase_op = vals.erase(store_id.get_value()); if(erase_op == 0u){ return make_error(); @@ -99,7 +88,7 @@ public: */ template error_or*> find(id store_id){ - auto& vals = std::get(values_); + auto& vals = std::get>>(values_); auto find_res = vals.find(store_id.get_value()); if(find_res == vals.end()){ return make_error(); -- cgit v1.2.3