summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--whiteboard.cpp55
1 files changed, 25 insertions, 30 deletions
diff --git a/whiteboard.cpp b/whiteboard.cpp
index 6d07576..2718937 100644
--- a/whiteboard.cpp
+++ b/whiteboard.cpp
@@ -7,12 +7,15 @@
#include <fcgiapp.h>
+#include <chrono>
#include <iostream>
#include <functional>
#include <filesystem>
+#include <mutex>
#include <random>
#include <regex>
#include <string>
+#include <thread>
#include <unordered_map>
#include <boost/algorithm/string/predicate.hpp>
@@ -67,33 +70,6 @@ namespace {
return "endofcodes";
}
- void setFileById(const std::string& data, const std::string& id)
- {
- fs::path path{data_path / id};
-
- // skip if it doesn't exists
- if (data.empty() && !fs::exists(path))
- return;
-
- // clean up immediately, if appropriate
- if (data.empty()) {
- fs::remove(path);
- return;
- }
-
- File::setFile(path, data);
- }
-
- std::string getFileById(const std::string& id)
- {
- fs::path path{data_path / id};
-
- if (!fs::exists(path))
- return {};
-
- return File::getFile(path);
- }
-
void usage() {
std::cout <<
"Usage: \n"
@@ -105,6 +81,20 @@ namespace {
"Without options, whiteboard will be started as FCGI application"
<< std::endl;
}
+
+ std::mutex storage_mutex;
+
+ 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));
+ }
+ }
+
} // namespace
// the actual main() for testability
@@ -125,6 +115,8 @@ int whiteboard(int argc, char* argv[])
}
}
+ std::thread storage_cleanup_thread(storage_cleanup, std::ref(storage));
+
Magick::InitializeMagick(NULL); // for qrcode.cpp
int result = FCGX_Init();
@@ -148,6 +140,7 @@ int whiteboard(int argc, char* argv[])
while (FCGX_Accept_r(&request) >= 0) {
try {
+ std::lock_guard<std::mutex> lock(storage_mutex);
char* method = FCGX_GetParam("REQUEST_METHOD", request.envp);
// POST for server actions, changes
@@ -171,12 +164,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")};
- setFileById(data, id);
+ 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 {getFileById(id)};
+ std::string filedata {storage.getDocument(id)};
if (filedata.size() > 30000000)
throw std::runtime_error("File too big");
@@ -192,7 +185,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 {getFileById(id)};
+ std::string filedata {storage.getDocument(id)};
if (checksum != checksum32(filedata)) {
//std::cout << "Sending change..." << std::endl;
FCGX_PutS("Content-Type: application/octet-stream\r\n", request.out);
@@ -235,6 +228,8 @@ int whiteboard(int argc, char* argv[])
}
}
+ storage_cleanup_thread.join();
+
return 0;
}