From 13f2007d104149f69ab7a794d2e119830e638eaa Mon Sep 17 00:00:00 2001 From: dec05eba <0xdec05eba@gmail.com> Date: Tue, 16 Oct 2018 00:38:01 +0200 Subject: Replace opendht with sibs pubsub This should fix issues with memory usage/leaks and make it easier to get peers subscribed to the same key. It will also be easier to modify and also works easier cross platform because of no additional dependencies. --- include/odhtdb/Database.hpp | 54 ++++++++++++++------------------------ include/odhtdb/DatabaseStorage.hpp | 12 +++------ include/odhtdb/DhtKey.hpp | 12 ++++----- include/odhtdb/InfoHash.hpp | 39 +++++++++++++++++++++++++++ include/odhtdb/Key.hpp | 15 ----------- 5 files changed, 69 insertions(+), 63 deletions(-) create mode 100644 include/odhtdb/InfoHash.hpp delete mode 100644 include/odhtdb/Key.hpp (limited to 'include') diff --git a/include/odhtdb/Database.hpp b/include/odhtdb/Database.hpp index 372b19c..a326398 100644 --- a/include/odhtdb/Database.hpp +++ b/include/odhtdb/Database.hpp @@ -1,7 +1,7 @@ #pragma once +#include "InfoHash.hpp" #include "types.hpp" -#include "Key.hpp" #include "DataView.hpp" #include "DatabaseStorage.hpp" #include "Hash.hpp" @@ -13,13 +13,13 @@ #include "OwnedMemory.hpp" #include "DatabaseOperation.hpp" #include "DatabaseOrder.hpp" -#include #include #include #include #include #include #include +#include namespace odhtdb { @@ -134,28 +134,11 @@ namespace odhtdb struct DatabaseSeedInfo { - std::shared_ptr> newDataListenerFuture; - std::shared_ptr> responseKeyFuture; - std::shared_ptr> requestOldDataListenerFuture; + sibs::ListenHandle newDataListenHandle; + sibs::ListenHandle responseKeyListenHandle; + sibs::ListenHandle requestOldDataListenHandle; - std::shared_ptr reponseKeyInfoHash; - - DatabaseSeedInfo(){} - DatabaseSeedInfo(const DatabaseSeedInfo &other) - { - newDataListenerFuture = other.newDataListenerFuture; - responseKeyFuture = other.responseKeyFuture; - requestOldDataListenerFuture = other.requestOldDataListenerFuture; - reponseKeyInfoHash = other.reponseKeyInfoHash; - } - DatabaseSeedInfo& operator=(const DatabaseSeedInfo &other) - { - newDataListenerFuture = other.newDataListenerFuture; - responseKeyFuture = other.responseKeyFuture; - requestOldDataListenerFuture = other.requestOldDataListenerFuture; - reponseKeyInfoHash = other.reponseKeyInfoHash; - return *this; - } + std::shared_ptr reponseKeyInfoHash; }; using CreateNodeCallbackFunc = std::function; @@ -165,6 +148,8 @@ namespace odhtdb using ReceiveCustomMessageCallbackFunc = std::function; using SendCustomMessageCallbackFunc = std::function; + using Value = std::vector; + struct DatabaseCallbackFuncs { CreateNodeCallbackFunc createNodeCallbackFunc; @@ -185,6 +170,7 @@ namespace odhtdb void stopSeeding(const Hash &nodeHash); void loadNode(const Hash &nodeHash, DatabaseLoadOrder loadOrder = DatabaseLoadOrder::OLDEST_FIRST); + // When a node is created, you automatically seed it as well so there is no need to call seed on it. // Throws DatabaseCreateException on failure. std::unique_ptr create(); // Throws PermissionDeniedException if user @userToPerformActionWith is not allowed to add data to node @@ -213,26 +199,26 @@ namespace odhtdb // Returns -1 on failure int getUserLowestPermissionLevel(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; - std::future receiveCustomMessage(const dht::InfoHash &requestKey, ReceiveCustomMessageCallbackFunc callbackFunc); + sibs::ListenHandle receiveCustomMessage(const InfoHash &requestKey, ReceiveCustomMessageCallbackFunc callbackFunc); - void sendCustomMessage(const dht::InfoHash &key, std::vector &&data); + void sendCustomMessage(const InfoHash &key, const void *data, const usize size); // Return true in @callbackFunc if you want to continue listening for responses, otherwise return false - std::future sendCustomMessage(const dht::InfoHash &key, std::vector &&data, SendCustomMessageCallbackFunc callbackFunc); + sibs::ListenHandle sendCustomMessage(const InfoHash &key, const void *data, const usize size, SendCustomMessageCallbackFunc callbackFunc); - void cancelNodeListener(const dht::InfoHash &infoHash, std::future &nodeListener); + void cancelNodeListener(sibs::ListenHandle &nodeListener); int clearCache(); - static dht::InfoHash getInfoHash(const void *data, usize size); + static InfoHash getInfoHash(const void *data, usize size); private: - void sendOldDataToPeer(const DatabaseNode nodeToSeed, const std::shared_ptr requestResponseInfoHash, const std::shared_ptr value, usize valueOffset); - void deserializeCreateRequest(const std::shared_ptr &value, const Hash &hash, const std::shared_ptr encryptionKey); - void deserializeAddRequest(const std::shared_ptr &value, const Hash &requestDataHash, const std::shared_ptr &nodeHash, const std::shared_ptr encryptionKey); - bool listenCreateData(std::shared_ptr value, const Hash &hash, const std::shared_ptr encryptionKey); - bool listenAddData(std::shared_ptr value, const Hash &requestDataHash, const std::shared_ptr nodeHash, const std::shared_ptr encryptionKey); + bool sendOldDataToPeer(const DatabaseNode nodeToSeed, const void *data, const usize size); + void deserializeCreateRequest(const void *data, const usize size, const Hash &hash, const std::shared_ptr encryptionKey); + void deserializeAddRequest(const void *data, const usize size, const Hash &requestDataHash, const std::shared_ptr &nodeHash, const std::shared_ptr encryptionKey); + bool listenCreateData(const void *data, const usize size, const Hash &hash, const std::shared_ptr encryptionKey); + bool listenAddData(const void *data, const usize size, const Hash &requestDataHash, const std::shared_ptr nodeHash, const std::shared_ptr encryptionKey); private: - dht::DhtRunner node; + sibs::BootstrapConnection bootstrapConnection; DatabaseStorage databaseStorage; std::function onCreateNodeCallbackFunc; std::function onAddNodeCallbackFunc; diff --git a/include/odhtdb/DatabaseStorage.hpp b/include/odhtdb/DatabaseStorage.hpp index b84635c..b0c081e 100644 --- a/include/odhtdb/DatabaseStorage.hpp +++ b/include/odhtdb/DatabaseStorage.hpp @@ -17,9 +17,8 @@ #include #include #include -#include -#include #include +#include class sqlite3; class sqlite3_stmt; @@ -141,13 +140,11 @@ namespace odhtdb std::pair> getNodeDecryptionKey(const Hash &nodeHash); void setNodeDecryptionKey(const Hash &nodeHash, const DataView &decryptionKey); - const std::vector& getRemoteNodes() const; - void setRemoteNodes(const std::vector &remoteNodes); + const std::vector>& getRemotePeers() const; + void setRemotePeers(const std::vector> &remoteNodes); std::vector getUserGroups(const Hash &nodeHash, const Signature::PublicKey &userPublicKey) const; - const dht::crypto::Identity& getIdentity() const; - // Update storage state (remove quarantine objects if they are too old, etc) void update(); @@ -203,7 +200,6 @@ namespace odhtdb boost::filesystem::path remoteNodesFilePath; u8 passwordSalt[PASSWORD_SALT_LEN]; - std::pair, std::shared_ptr> identity; - std::vector remoteNodes; + std::vector> remotePeers; }; } diff --git a/include/odhtdb/DhtKey.hpp b/include/odhtdb/DhtKey.hpp index 959bb65..8af2bd4 100644 --- a/include/odhtdb/DhtKey.hpp +++ b/include/odhtdb/DhtKey.hpp @@ -1,7 +1,7 @@ #pragma once #include "Hash.hpp" -#include +#include "InfoHash.hpp" namespace odhtdb { @@ -9,13 +9,13 @@ namespace odhtdb { public: DhtKey(const Hash &key); - DhtKey(const dht::InfoHash &infoHash); + DhtKey(const InfoHash &infoHash); - dht::InfoHash getNewDataListenerKey(); - dht::InfoHash getRequestOldDataKey(); - dht::InfoHash getPingKey(); + InfoHash getNewDataListenerKey(); + InfoHash getRequestOldDataKey(); + InfoHash getPingKey(); private: - dht::InfoHash infoHash; + InfoHash infoHash; unsigned char firstByteOriginalValue; }; } diff --git a/include/odhtdb/InfoHash.hpp b/include/odhtdb/InfoHash.hpp new file mode 100644 index 0000000..aba0a24 --- /dev/null +++ b/include/odhtdb/InfoHash.hpp @@ -0,0 +1,39 @@ +#pragma once + +#include +#include +#include +#include +#include "types.hpp" + +namespace odhtdb +{ + class InfoHashException : public std::runtime_error + { + public: + InfoHashException(const std::string &errMsg) : std::runtime_error(errMsg) {} + }; + + class InfoHash + { + public: + InfoHash(); + InfoHash(const u8 *data, const size_t size); + + // Throws InfoHashException on error + static InfoHash generateHash(const u8 *data, const size_t size); + + u8& operator [] (size_t index) + { + assert(index < key.data.size()); + return key.data[index]; + } + + const sibs::PubsubKey& getKey() const { return key; } + + bool operator == (const InfoHash &other) const; + bool operator != (const InfoHash &other) const; + private: + sibs::PubsubKey key; + }; +} \ No newline at end of file diff --git a/include/odhtdb/Key.hpp b/include/odhtdb/Key.hpp deleted file mode 100644 index 18971d1..0000000 --- a/include/odhtdb/Key.hpp +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include - -namespace odhtdb -{ - class Key - { - public: - Key() {} - Key(const char *key) : hashedKey(dht::InfoHash::get(key)) {} - - dht::InfoHash hashedKey; - }; -} -- cgit v1.2.3