summaryrefslogtreecommitdiffhomepage
path: root/whiteboard.cpp
diff options
context:
space:
mode:
authorRoland Reichwein <mail@reichwein.it>2023-01-01 22:37:36 +0100
committerRoland Reichwein <mail@reichwein.it>2023-01-01 22:37:36 +0100
commit4ca6b48f8d0d9cb61995fec63dde95880ff37547 (patch)
tree5c46c472371a88e3123916985a6314309b0bccdc /whiteboard.cpp
parenta72cd70af957a06ae870d93314b4ed0f3625f6ee (diff)
generate_id() using DB not FS
Diffstat (limited to 'whiteboard.cpp')
-rw-r--r--whiteboard.cpp110
1 files changed, 54 insertions, 56 deletions
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);