diff options
author | dec05eba <0xdec05eba@gmail.com> | 2018-10-16 00:38:01 +0200 |
---|---|---|
committer | dec05eba <0xdec05eba@gmail.com> | 2018-10-16 00:39:21 +0200 |
commit | 13f2007d104149f69ab7a794d2e119830e638eaa (patch) | |
tree | 8dfffb5669d9db6f2328426f5a1cccb72275d92e /include | |
parent | 911e62afb82b140e368181f4966442cd5c2e1bd8 (diff) |
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.
Diffstat (limited to 'include')
-rw-r--r-- | include/odhtdb/Database.hpp | 54 | ||||
-rw-r--r-- | include/odhtdb/DatabaseStorage.hpp | 12 | ||||
-rw-r--r-- | include/odhtdb/DhtKey.hpp | 12 | ||||
-rw-r--r-- | include/odhtdb/InfoHash.hpp | 39 | ||||
-rw-r--r-- | include/odhtdb/Key.hpp | 15 |
5 files changed, 69 insertions, 63 deletions
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 <opendht/dhtrunner.h> #include <vector> #include <ntp/NtpClient.hpp> #include <boost/filesystem/path.hpp> #include <sibs/SafeSerializer.hpp> #include <stdexcept> #include <functional> +#include <sibs/BootstrapConnection.hpp> namespace odhtdb { @@ -134,28 +134,11 @@ namespace odhtdb struct DatabaseSeedInfo { - std::shared_ptr<std::future<size_t>> newDataListenerFuture; - std::shared_ptr<std::future<size_t>> responseKeyFuture; - std::shared_ptr<std::future<size_t>> requestOldDataListenerFuture; + sibs::ListenHandle newDataListenHandle; + sibs::ListenHandle responseKeyListenHandle; + sibs::ListenHandle requestOldDataListenHandle; - std::shared_ptr<dht::InfoHash> 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<InfoHash> reponseKeyInfoHash; }; using CreateNodeCallbackFunc = std::function<void(const DatabaseCreateNodeRequest&)>; @@ -165,6 +148,8 @@ namespace odhtdb using ReceiveCustomMessageCallbackFunc = std::function<sibs::SafeSerializer(const void *data, usize size)>; using SendCustomMessageCallbackFunc = std::function<bool(bool gotResponse, const void *data, usize size)>; + using Value = std::vector<u8>; + 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<DatabaseCreateResponse> 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<size_t> receiveCustomMessage(const dht::InfoHash &requestKey, ReceiveCustomMessageCallbackFunc callbackFunc); + sibs::ListenHandle receiveCustomMessage(const InfoHash &requestKey, ReceiveCustomMessageCallbackFunc callbackFunc); - void sendCustomMessage(const dht::InfoHash &key, std::vector<u8> &&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<size_t> sendCustomMessage(const dht::InfoHash &key, std::vector<u8> &&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<size_t> &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<dht::InfoHash> requestResponseInfoHash, const std::shared_ptr<dht::Value> value, usize valueOffset); - void deserializeCreateRequest(const std::shared_ptr<dht::Value> &value, const Hash &hash, const std::shared_ptr<OwnedByteArray> encryptionKey); - void deserializeAddRequest(const std::shared_ptr<dht::Value> &value, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const std::shared_ptr<OwnedByteArray> encryptionKey); - bool listenCreateData(std::shared_ptr<dht::Value> value, const Hash &hash, const std::shared_ptr<OwnedByteArray> encryptionKey); - bool listenAddData(std::shared_ptr<dht::Value> value, const Hash &requestDataHash, const std::shared_ptr<Hash> nodeHash, const std::shared_ptr<OwnedByteArray> 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<OwnedByteArray> encryptionKey); + void deserializeAddRequest(const void *data, const usize size, const Hash &requestDataHash, const std::shared_ptr<Hash> &nodeHash, const std::shared_ptr<OwnedByteArray> encryptionKey); + bool listenCreateData(const void *data, const usize size, const Hash &hash, const std::shared_ptr<OwnedByteArray> encryptionKey); + bool listenAddData(const void *data, const usize size, const Hash &requestDataHash, const std::shared_ptr<Hash> nodeHash, const std::shared_ptr<OwnedByteArray> encryptionKey); private: - dht::DhtRunner node; + sibs::BootstrapConnection bootstrapConnection; DatabaseStorage databaseStorage; std::function<void(const DatabaseCreateNodeRequest&)> onCreateNodeCallbackFunc; std::function<void(const DatabaseAddNodeRequest&)> 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 <stdexcept> #include <boost/filesystem/path.hpp> #include <sibs/SafeDeserializer.hpp> -#include <opendht/crypto.h> -#include <opendht/dhtrunner.h> #include <functional> +#include <sibs/DirectConnection.hpp> class sqlite3; class sqlite3_stmt; @@ -141,13 +140,11 @@ namespace odhtdb std::pair<bool, std::shared_ptr<OwnedByteArray>> getNodeDecryptionKey(const Hash &nodeHash); void setNodeDecryptionKey(const Hash &nodeHash, const DataView &decryptionKey); - const std::vector<dht::NodeExport>& getRemoteNodes() const; - void setRemoteNodes(const std::vector<dht::NodeExport> &remoteNodes); + const std::vector<std::shared_ptr<sibs::DirectConnectionPeer>>& getRemotePeers() const; + void setRemotePeers(const std::vector<std::shared_ptr<sibs::DirectConnectionPeer>> &remoteNodes); std::vector<OwnedByteArray> 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<dht::crypto::PrivateKey>, std::shared_ptr<dht::crypto::Certificate>> identity; - std::vector<dht::NodeExport> remoteNodes; + std::vector<std::shared_ptr<sibs::DirectConnectionPeer>> 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 <opendht/infohash.h> +#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 <array> +#include <stdexcept> +#include <sibs/PubsubKey.hpp> +#include <cassert> +#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 <opendht/infohash.h> - -namespace odhtdb -{ - class Key - { - public: - Key() {} - Key(const char *key) : hashedKey(dht::InfoHash::get(key)) {} - - dht::InfoHash hashedKey; - }; -} |