diff options
author | Roland Reichwein <mail@reichwein.it> | 2023-01-02 21:00:10 +0100 |
---|---|---|
committer | Roland Reichwein <mail@reichwein.it> | 2023-01-02 21:00:10 +0100 |
commit | 676e924f7c5bd780250a80187bfcb74550348af7 (patch) | |
tree | a9ad983eb5ef2e29713d5630270496f24df36a67 /whiteboard.cpp | |
parent | b1c99b0c3b0a8d65f237f507ad238ce441233b3f (diff) |
Added tests
Diffstat (limited to 'whiteboard.cpp')
-rw-r--r-- | whiteboard.cpp | 41 |
1 files changed, 3 insertions, 38 deletions
diff --git a/whiteboard.cpp b/whiteboard.cpp index 75472ba..6466635 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -14,7 +14,6 @@ #include <functional> #include <filesystem> #include <mutex> -#include <random> #include <regex> #include <string> #include <thread> @@ -56,6 +55,7 @@ Whiteboard::Whiteboard(): { } +// contents of cleanup thread; looping void Whiteboard::storage_cleanup() { while(true) { @@ -67,38 +67,6 @@ void Whiteboard::storage_cleanup() } } -uint32_t Whiteboard::checksum32(const std::string& s) -{ - uint32_t result{0}; - for (unsigned int i = 0; i < s.size(); i++) { - result = ((result >> 1) | ((result & 1) << 31)) ^ (s[i] & 0xFF); - } - return result & 0x7FFFFFFF; -} - -std::string Whiteboard::generate_id() -{ - static std::random_device r; - static std::default_random_engine e1(r()); - static std::uniform_int_distribution<int> uniform_dist(0, 35); - - // limit tries - for (int j = 0; j < 100000; j++) { - std::string result; - for (int i = 0; i < 6; i++) { - char c{static_cast<char>('0' + uniform_dist(e1))}; - if (c > '9') - c = c - '9' + 'a'; - result.push_back(c); - } - - if (!m_storage.exists(result)) - return result; - } - - return "endofcodes"; -} - // the actual main() for testability int Whiteboard::run(int argc, char* argv[]) { @@ -179,11 +147,8 @@ int Whiteboard::run(int argc, char* argv[]) std::string checksum_s {xml.get<std::string>("request.checksum")}; uint32_t checksum{static_cast<uint32_t>(stoul(checksum_s))}; - //std::cout << "Checksum JS: " << checksum_s << std::endl; - //std::cout << "Checksum C++: " << checksum32(filedata) << std::endl; - std::string filedata {m_storage.getDocument(id)}; - if (checksum != checksum32(filedata)) { + if (checksum != m_storage.checksum32(filedata)) { //std::cout << "Sending change..." << std::endl; FCGX_PutS("Content-Type: application/octet-stream\r\n", request.out); FCGX_FPrintF(request.out, "Content-Length: %d\r\n\r\n", filedata.size()); @@ -195,7 +160,7 @@ int Whiteboard::run(int argc, char* argv[]) } } else if (command == "newid") { FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); - FCGX_PutS(generate_id().c_str(), request.out); + FCGX_PutS(m_storage.generate_id().c_str(), request.out); } else if (command == "qrcode") { std::string url{xml.get<std::string>("request.url")}; |