diff --git a/src/solanaceae/crdtnotes/crdtnotes.cpp b/src/solanaceae/crdtnotes/crdtnotes.cpp index ab6fcd5..5deb1fa 100644 --- a/src/solanaceae/crdtnotes/crdtnotes.cpp +++ b/src/solanaceae/crdtnotes/crdtnotes.cpp @@ -1,6 +1,8 @@ #include "./crdtnotes.hpp" CRDTNotes::CRDTNotes(void) { + _rng.seed(std::random_device{}()); + _rng.discard(707); } CRDTNotes::~CRDTNotes(void) { @@ -37,3 +39,13 @@ CRDTNotes::Doc* CRDTNotes::addDoc(const CRDTAgent& self_agent, const DocID& id) return &doc; } +CRDTNotes::Doc* CRDTNotes::addDoc(const CRDTAgent& self_agent) { + DocID new_id; + for (auto& it : new_id) { + // TODO: this discards alot + it = static_cast(_rng()); + } + + return addDoc(self_agent, new_id); +} + diff --git a/src/solanaceae/crdtnotes/crdtnotes.hpp b/src/solanaceae/crdtnotes/crdtnotes.hpp index d1a88ba..f838cb3 100644 --- a/src/solanaceae/crdtnotes/crdtnotes.hpp +++ b/src/solanaceae/crdtnotes/crdtnotes.hpp @@ -6,6 +6,7 @@ #include #include #include +#include using ID32 = std::array; @@ -36,6 +37,7 @@ class CRDTNotes { private: // TODO: add metadata to docs std::unordered_map _docs; + std::default_random_engine _rng; public: @@ -49,5 +51,8 @@ class CRDTNotes { Doc* getDoc(const DocID& id); Doc* addDoc(const CRDTAgent& self_agent, const DocID& doc); + + // new doc with random id + Doc* addDoc(const CRDTAgent& self_agent); }; diff --git a/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.cpp b/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.cpp index b86c796..c006fab 100644 --- a/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.cpp +++ b/src/solanaceae/crdtnotes_imgui/crdtnotes_imgui.cpp @@ -39,7 +39,7 @@ namespace detail { std::string to_hex(const Container& data) { std::string res; for (const uint8_t it : data) { - res += nib_to_hex(it << 4); + res += nib_to_hex(it >> 4); res += nib_to_hex(it & 0x0f); } return res; @@ -74,9 +74,7 @@ float CRDTNotesImGui::render(void) { _notes.addDoc( // tox id (id from self) - self_agent_id, - // random 32bytes? - {} + self_agent_id ); // and open the doc