diff --git a/p2p-rm/include/ftl/p2p-rm/protocol.hpp b/p2p-rm/include/ftl/p2p-rm/protocol.hpp new file mode 100644 index 0000000000000000000000000000000000000000..888106b88a7ebcf5ae61544d732d69968531f866 --- /dev/null +++ b/p2p-rm/include/ftl/p2p-rm/protocol.hpp @@ -0,0 +1,15 @@ +#ifndef _FTL_P2P_RM_PROTOCOL_HPP_ +#define _FTL_P2P_RM_PROTOCOL_HPP_ + +/* To get the service space for p2p */ +#include <ftl/net/protocol.hpp> + +#define P2P_SYNC (FTL_PROTOCOL_P2P + 1) +#define P2P_REQUESTOWNERSHIP (FTL_PROTOCOL_P2P + 2) +#define P2P_FINDOWNER (FTL_PROTOCOL_P2P + 3) +#define P2P_NOTIFYOWNERSHIP (FTL_PROTOCOL_P2P + 4) +#define P2P_URISEARCH (FTL_PROTOCOL_P2P + 5) +#define P2P_PEERSEARCH (FTL_PROTOCOL_P2P + 6) + +#endif // _FTL_P2P_RM_PROTOCOL_HPP_ + diff --git a/p2p-rm/src/blob.cpp b/p2p-rm/src/blob.cpp index 3b8b1beeffd6275b8e41c25cadbe6e9acbc0e55e..440832de1fc655427a0e53a0f49187440449f470 100644 --- a/p2p-rm/src/blob.cpp +++ b/p2p-rm/src/blob.cpp @@ -1,33 +1,4 @@ #include <memory.h> -#include <ftl/net.hpp> - -#include "ftl/p2p-rm/blob.hpp" - -#define MEMORY_SYNC 0x1000 - -struct Header { - uint32_t blobid; - uint32_t offset; - uint32_t size; -}; - -void ftl::rm::Blob::sync(size_t offset, size_t size) { - // Sanity check - if (offset + size > size_) throw -1; - - // TODO Delay send to collate many write operations? - - if (sockets_.size() > 0) { - Header header{blobid_,static_cast<uint32_t>(offset),static_cast<uint32_t>(size)}; - - // If local, write direct to data_, otherwise send over network - for (auto s : sockets_) { - // Send over network - s->send2(MEMORY_SYNC, std::string((const char*)&header,sizeof(header)), - std::string(&data_[offset],size)); - } - } -} /*void ftl::rm::Blob::write(size_t offset, const char *data, size_t size) { // Sanity check diff --git a/p2p-rm/src/p2prm.cpp b/p2p-rm/src/p2prm.cpp index 1281c51ea0f33b0cd226c1ae16dc83216c5ad35e..6f91b1da4431e9ba5332cf235b3b4bffcb2b31f1 100644 --- a/p2p-rm/src/p2prm.cpp +++ b/p2p-rm/src/p2prm.cpp @@ -1,6 +1,10 @@ #include "ftl/p2p-rm.hpp" +#include "ftl/p2p-rm/blob.hpp" +#include "ftl/p2p-rm/protocol.hpp" #include <ftl/uri.hpp> +#include <ftl/net.hpp> + #include <map> #include <string> @@ -13,6 +17,29 @@ void ftl::rm::reset() { blobs.clear(); } +struct SyncHeader { + uint32_t blobid; + uint32_t offset; + uint32_t size; +}; + +void ftl::rm::_sync(const ftl::rm::Blob &blob, size_t offset, size_t size) { + // Sanity check + if (offset + size > size_) throw -1; + + // TODO Delay send to collate many write operations? + + if (blob.sockets_.size() > 0) { + SyncHeader header{blob.blobid_,static_cast<uint32_t>(offset),static_cast<uint32_t>(size)}; + + for (auto s : blob.sockets_) { + // Send over network + s->send2(P2P_SYNC, std::string((const char*)&header,sizeof(header)), + std::string(&data_[offset],size)); + } + } +} + ftl::rm::Blob *ftl::rm::_lookup(const char *uri) { URI u(uri); if (!u.isValid()) return NULL; diff --git a/p2p-rm/test/p2p-rm.cpp b/p2p-rm/test/p2p-rm.cpp index d124341a30cca2714637cd7b233a20ef41bce0b0..663e3574d8cde97a060e025f515e3897334c8c6d 100644 --- a/p2p-rm/test/p2p-rm.cpp +++ b/p2p-rm/test/p2p-rm.cpp @@ -1,9 +1,13 @@ #include "catch.hpp" #include <ftl/p2p-rm.hpp> +#include <vector> // --- Mock Socket Send +static std::vector<uint32_t> msgs; + int ftl::net::raw::Socket::send2(uint32_t service, const std::string &data1, const std::string &data2) { + msgs.push_back(service); return 0; } @@ -73,5 +77,12 @@ SCENARIO( "Getting a read_ref", "[get]" ) { REQUIRE( r.pointer().is_local() ); REQUIRE( r == 89 ); } + + GIVEN( "a valid URI to remote memory" ) { + const auto r = ftl::rm::getReadable<int>("ftl://uti.fi/memory/remote0"); + REQUIRE( r.is_valid() ); + REQUIRE( !r.pointer().is_local() ); + REQUIRE( r == 888 ); + } }