Skip to content
Snippets Groups Projects
Commit bf243372 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Non functional changes to p2prm for new cluster form

parent 0362c5c9
No related branches found
No related tags found
No related merge requests found
#ifndef _FTL_NET_PROTOCOL_HPP_
#define _FTL_NET_PROTOCOL_HPP_
#define FTL_PROTOCOL_P2P 0x1000
#endif // _FTL_NET_PROTOCOL_HPP_
......@@ -23,7 +23,7 @@ set(CMAKE_CXX_FLAGS_RELEASE "-O3")
SET(CMAKE_USE_RELATIVE_PATHS ON)
set(FTLSOURCE
src/blob.cpp
src/cluster.cpp
src/p2prm.cpp
)
add_library(ftl-p2prm ${FTLSOURCE})
......
#ifndef _FTL_P2P_RA_HPP_
#define _FTL_P2P_RA_HPP_
#ifndef _FTL_P2P_RM_HPP_
#define _FTL_P2P_RM_HPP_
#include "ftl/p2p-rm/mapped_ptr.hpp"
#include "ftl/p2p-rm/internal.hpp"
#include <type_traits>
#include <ftl/p2p-rm/cluster.hpp>
namespace ftl {
namespace rm {
void reset();
inline void destroy() { reset(); }
/**
* Obtain a remote pointer from a URI. A nullptr is returned if the URI is
* not valid. If the URI is actually local then a remote pointer is still
* returned and may be used normally, although it will possibly result in
* unwanted memory copies.
*/
template <typename T>
ftl::mapped_ptr<T> get(const char *uri) {
auto b = _lookup(uri);
// TODO Verify type and size
return ftl::mapped_ptr<T>{b,0};
}
/**
* Get a read-only memory reference from a URI.
*/
template <typename T>
ftl::read_ref<T> getReadable(const char *uri) {
return get<T>(uri).readable();
}
std::shared_ptr<Cluster> cluster(const char *uri);
/**
* Get a read/writable memory reference from a URI.
*/
template <typename T>
ftl::write_ref<T> getWritable(const char *uri) {
return get<T>(uri).writable();
}
/**
* Register a memory area locally mapped to a given URI. The URI
* must not already exist within the peer group.
*/
template <typename T>
ftl::mapped_ptr<T> map(const char *uri, T *addr, size_t size=1) {
if (std::is_pointer<T>::value) return ftl::null_ptr<T>;
if (std::is_function<T>::value) return ftl::null_ptr<T>;
if (std::is_void<T>::value) return ftl::null_ptr<T>;
if (addr == NULL) return ftl::null_ptr<T>;
return ftl::mapped_ptr<T>{_create(uri, (char*)addr, sizeof(T), size,
static_cast<flags_t>(std::is_integral<T>::value * ftl::rm::FLAG_INTEGER |
std::is_signed<T>::value * ftl::rm::FLAG_SIGNED |
std::is_trivial<T>::value * ftl::rm::FLAG_TRIVIAL),
typeid(T).name()),0};
}
void unmap(const char *uri);
template <typename T>
void unmap(ftl::mapped_ptr<T> ptr) {}
/**
* Obtain a list or URI memory blocks in the current peer group that match
* the provided base URI.
*/
std::vector<std::string> search(const char *partial_uri);
/**
* Connect to a new peer node using the specified socket.
*/
void addPeer(ftl::net::Socket *s);
/**
* Connect to a new peer using a URL string.
*/
void addPeer(const char *url);
}
}
#endif // _FTL_P2P_RA_HPP_
#endif // _FTL_P2P_RM_HPP_
#ifndef _FTL_P2P_RM_CLUSTER_HPP_
#define _FTL_P2P_RM_CLUSTER_HPP_
#include "ftl/p2p-rm/mapped_ptr.hpp"
#include "ftl/p2p-rm/internal.hpp"
#include <type_traits>
#include <memory>
#include <vector>
namespace ftl {
namespace net {
class Socket;
class Listener;
};
namespace rm {
class Cluster {
public:
Cluster(const char *uri, std::shared_ptr<ftl::net::Listener> l);
~Cluster();
void reset();
inline void destroy() { reset(); }
/**
* Obtain a remote pointer from a URI. A nullptr is returned if the URI is
* not valid. If the URI is actually local then a remote pointer is still
* returned and may be used normally, although it will possibly result in
* unwanted memory copies.
*/
template <typename T>
ftl::mapped_ptr<T> get(const char *uri) {
auto b = _lookup(uri);
// TODO Verify type and size
return ftl::mapped_ptr<T>{b,0};
}
/**
* Get a read-only memory reference from a URI.
*/
template <typename T>
ftl::read_ref<T> getReadable(const char *uri) {
return get<T>(uri).readable();
}
/**
* Get a read/writable memory reference from a URI.
*/
template <typename T>
ftl::write_ref<T> getWritable(const char *uri) {
return get<T>(uri).writable();
}
/**
* Register a memory area locally mapped to a given URI. The URI
* must not already exist within the peer group.
*/
template <typename T>
ftl::mapped_ptr<T> map(const char *uri, T *addr, size_t size=1) {
if (std::is_pointer<T>::value) return ftl::null_ptr<T>;
if (std::is_function<T>::value) return ftl::null_ptr<T>;
if (std::is_void<T>::value) return ftl::null_ptr<T>;
if (addr == NULL) return ftl::null_ptr<T>;
return ftl::mapped_ptr<T>{_create(this, uri, (char*)addr, sizeof(T), size,
static_cast<flags_t>(std::is_integral<T>::value * ftl::rm::FLAG_INTEGER |
std::is_signed<T>::value * ftl::rm::FLAG_SIGNED |
std::is_trivial<T>::value * ftl::rm::FLAG_TRIVIAL),
typeid(T).name()),0};
}
void unmap(const char *uri);
template <typename T>
void unmap(ftl::mapped_ptr<T> ptr) {}
/**
* Obtain a list or URI memory blocks in the current peer group that match
* the provided base URI.
*/
std::vector<std::string> search(const char *partial_uri);
/**
* Connect to a new peer node using the specified socket.
*/
void addPeer(std::shared_ptr<ftl::net::Socket> s);
/**
* Connect to a new peer using a URL string.
*/
void addPeer(const char *url);
private:
std::string uri_;
std::string root_;
std::shared_ptr<ftl::net::Listener> listener_;
std::vector<std::shared_ptr<ftl::net::Socket>> peers_;
};
};
};
#endif // _FTL_P2P_RM_CLUSTER_HPP_
......@@ -5,6 +5,7 @@ namespace ftl {
namespace rm {
class Blob;
class Cluster;
enum flags_t : uint32_t {
FLAG_INTEGER = 1,
......@@ -13,7 +14,7 @@ enum flags_t : uint32_t {
};
ftl::rm::Blob *_lookup(const char *uri);
ftl::rm::Blob *_create(const char *uri, char *addr, size_t size, size_t count, flags_t flags, const std::string &tname);
ftl::rm::Blob *_create(Cluster *c, const char *uri, char *addr, size_t size, size_t count, flags_t flags, const std::string &tname);
}; // namespace rm
}; // namespace ftl
......
......@@ -9,7 +9,7 @@ add_executable(mapped_ptr EXCLUDE_FROM_ALL
add_executable(p2p_rm EXCLUDE_FROM_ALL
./tests.cpp
../src/p2prm.cpp
../src/blob.cpp
../src/cluster.cpp
./p2p-rm.cpp
)
target_link_libraries(p2p_rm uriparser)
......
#include "ftl/p2p-rm.hpp"
#include <ftl/net.hpp>
#include <gflags/gflags.h>
DEFINE_string(listen, "tcp://*:9000", "Listen URI");
DEFINE_string(peer, "", "Peer to connect to");
using namespace ftl;
int main(int argc, char *argv) {
gflags::ParseCommandLineFlags(&argc, &argv, true);
auto net = net::listen(FLAGS_listen);
auto cluster = rm::cluster("ftl://utu.fi", net);
int data = 20;
auto ptr = cluster->map<int>("ftl://utu.fi/memory/test1", &data);
if (FLAGS_peer) {
cluster->addPeer(FLAGS_peer);
std::cout << "Value = " << *ptr << std::endl; // 25.
std::cout << "Raw = " << data << std::endl; // 25.
*ptr = 30;
} else {
*ptr = 25;
ptr.onChange(()=> {
std::cout << "Value changed = " << *ptr << std::endl; // 30
});
}
while (net());
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment