From 4ca6b48f8d0d9cb61995fec63dde95880ff37547 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sun, 1 Jan 2023 22:37:36 +0100 Subject: generate_id() using DB not FS --- whiteboard.cpp | 110 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 54 insertions(+), 56 deletions(-) (limited to 'whiteboard.cpp') diff --git a/whiteboard.cpp b/whiteboard.cpp index cebd0b1..48125a8 100644 --- a/whiteboard.cpp +++ b/whiteboard.cpp @@ -1,3 +1,5 @@ +#include "whiteboard.h" + #include #include #include @@ -35,41 +37,6 @@ namespace fs = std::filesystem; namespace { - uint32_t 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; - } - - fs::path data_path; - - std::string generate_id() - { - static std::random_device r; - static std::default_random_engine e1(r()); - static std::uniform_int_distribution 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('0' + uniform_dist(e1))}; - if (c > '9') - c = c - '9' + 'a'; - result.push_back(c); - } - fs::path path{data_path / result}; - - if (!fs::exists(path)) - return result; - } - - return "endofcodes"; - } - void usage() { std::cout << "Usage: \n" @@ -82,40 +49,71 @@ namespace { << std::endl; } - std::mutex storage_mutex; +} // namespace - void storage_cleanup(Storage& storage) - { - while(true) { - { - std::lock_guard lock(storage_mutex); - storage.cleanup(); - } - std::this_thread::sleep_for(std::chrono::minutes(10)); +Whiteboard::Whiteboard(): + m_config(), + m_storage(m_config) +{ +} + +void Whiteboard::storage_cleanup() +{ + while(true) { + { + std::lock_guard lock(m_storage_mutex); + m_storage.cleanup(); } + std::this_thread::sleep_for(std::chrono::minutes(10)); } +} -} // namespace +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; +} -// the actual main() for testability -int whiteboard(int argc, char* argv[]) +std::string Whiteboard::generate_id() { - Config config; - data_path = config.getDataPath(); + static std::random_device r; + static std::default_random_engine e1(r()); + static std::uniform_int_distribution 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('0' + uniform_dist(e1))}; + if (c > '9') + c = c - '9' + 'a'; + result.push_back(c); + } - Storage storage(config); + if (!m_storage.exists(result)) + return result; + } + return "endofcodes"; +} + +// the actual main() for testability +int Whiteboard::run(int argc, char* argv[]) +{ if (argc == 2) { if (argv[1] == "-h"s || argv[1] == "-?"s) { usage(); exit(0); } else if (argv[1] == "-c"s) { - storage.cleanup(); + m_storage.cleanup(); exit(0); } } - std::thread storage_cleanup_thread(storage_cleanup, std::ref(storage)); + std::thread storage_cleanup_thread(std::bind(&Whiteboard::storage_cleanup, this)); Magick::InitializeMagick(NULL); // for qrcode.cpp @@ -140,7 +138,7 @@ int whiteboard(int argc, char* argv[]) while (FCGX_Accept_r(&request) >= 0) { try { - std::lock_guard lock(storage_mutex); + std::lock_guard lock(m_storage_mutex); char* method = FCGX_GetParam("REQUEST_METHOD", request.envp); // POST for server actions, changes @@ -164,12 +162,12 @@ int whiteboard(int argc, char* argv[]) if (command == "modify") { std::string id {xml.get("request.id")}; std::string data {xml.get("request.data")}; - storage.setDocument(id, data); + m_storage.setDocument(id, data); FCGX_PutS("Content-Type: text/plain\r\n\r\n", request.out); } else if (command == "getfile") { std::string id {xml.get("request.id")}; - std::string filedata {storage.getDocument(id)}; + std::string filedata {m_storage.getDocument(id)}; if (filedata.size() > 30000000) throw std::runtime_error("File too big"); @@ -185,7 +183,7 @@ int whiteboard(int argc, char* argv[]) //std::cout << "Checksum JS: " << checksum_s << std::endl; //std::cout << "Checksum C++: " << checksum32(filedata) << std::endl; - std::string filedata {storage.getDocument(id)}; + std::string filedata {m_storage.getDocument(id)}; if (checksum != checksum32(filedata)) { //std::cout << "Sending change..." << std::endl; FCGX_PutS("Content-Type: application/octet-stream\r\n", request.out); -- cgit v1.2.3