diff options
author | dec05eba <dec05eba@protonmail.com> | 2018-06-07 22:00:42 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2020-08-18 22:56:48 +0200 |
commit | c2187ca6b61c701c281cc528db43f6b97c50f3d8 (patch) | |
tree | f0baf317846902ae628c2e12cf8c25b6eb235c77 /include/sibs/PubsubKey.hpp | |
parent | ed71e8adf36e3d0c3f6f2b54794fe069091d3376 (diff) |
Add bootstrap node, listen method
Diffstat (limited to 'include/sibs/PubsubKey.hpp')
-rw-r--r-- | include/sibs/PubsubKey.hpp | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/include/sibs/PubsubKey.hpp b/include/sibs/PubsubKey.hpp new file mode 100644 index 0000000..d123331 --- /dev/null +++ b/include/sibs/PubsubKey.hpp @@ -0,0 +1,42 @@ +#pragma once + +#include "../types.hpp" +#include <array> +#include <unordered_map> + +namespace sibs +{ + const usize PUBSUB_KEY_LENGTH = 32; + + // Source: https://stackoverflow.com/a/11414104 (public license) + static size_t fnvHash(const unsigned char *key, int len) + { + size_t h = 2166136261; + for (int i = 0; i < len; i++) + h = (h * 16777619) ^ key[i]; + return h; + } + + class PubsubKey + { + public: + PubsubKey(); + // Create pubsub key from existing data, data will be cutoff at PUBSUB_KEY_LENGTH. If @size is less than PUBSUB_KEY_LENGTH then data is appended with 0 + PubsubKey(const void *data, const usize size); + bool operator == (const PubsubKey &other) const; + bool operator != (const PubsubKey &other) const; + + std::array<u8, PUBSUB_KEY_LENGTH> data; + }; + + struct PubsubKeyHasher + { + size_t operator()(const PubsubKey &pubsubKey) const + { + return fnvHash((const unsigned char*)pubsubKey.data.data(), PUBSUB_KEY_LENGTH); + } + }; + + template <typename ValueType> + using PubsubKeyMap = std::unordered_map<PubsubKey, ValueType, PubsubKeyHasher>; +} |