diff options
-rw-r--r-- | main.cpp | 3 | ||||
-rw-r--r-- | whiteboard.cpp | 110 | ||||
-rw-r--r-- | whiteboard.h | 24 |
3 files changed, 77 insertions, 60 deletions
@@ -2,6 +2,7 @@ int main(int argc, char* argv[]) { - return whiteboard(argc, argv); + Whiteboard whiteboard; + return whiteboard.run(argc, argv); } 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 <stdio.h> #include <stdlib.h> #include <unistd.h> @@ -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<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); - } - 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<std::mutex> 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<std::mutex> 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<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); + } - 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<std::mutex> lock(storage_mutex); + std::lock_guard<std::mutex> 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<std::string>("request.id")}; std::string data {xml.get<std::string>("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<std::string>("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); diff --git a/whiteboard.h b/whiteboard.h index 39eeb66..be89f4b 100644 --- a/whiteboard.h +++ b/whiteboard.h @@ -1,6 +1,24 @@ -// pseudo main() - for testability - #pragma once -int whiteboard(int argc, char* argv[]); +#include <mutex> +#include <string> + +#include "config.h" +#include "storage.h" + +class Whiteboard +{ +public: + Whiteboard(); + int run(int argc, char* argv[]); + std::string generate_id(); + uint32_t checksum32(const std::string& s); + +private: + Config m_config; + Storage m_storage; + std::mutex m_storage_mutex; + + void storage_cleanup(); +}; |