diff options
Diffstat (limited to 'database.cpp')
| -rw-r--r-- | database.cpp | 91 |
1 files changed, 0 insertions, 91 deletions
| diff --git a/database.cpp b/database.cpp deleted file mode 100644 index 037579b..0000000 --- a/database.cpp +++ /dev/null | |||
| @@ -1,91 +0,0 @@ | |||
| 1 | #include "database.h" | ||
| 2 | |||
| 3 | #include <include/uuid.h> | ||
| 4 | |||
| 5 | #include <stdexcept> | ||
| 6 | |||
| 7 | std::string database::create(std::mt19937& rng) { | ||
| 8 | std::lock_guard state_guard(mutex_); | ||
| 9 | |||
| 10 | std::string token = uuids::to_string(uuids::uuid_random_generator{rng}()); | ||
| 11 | requests_[token] = request{}; | ||
| 12 | |||
| 13 | return token; | ||
| 14 | } | ||
| 15 | |||
| 16 | void database::subscribe(const std::string& token, | ||
| 17 | subscribe_callback_type callback) { | ||
| 18 | std::lock_guard state_guard(mutex_); | ||
| 19 | |||
| 20 | if (!requests_.count(token)) { | ||
| 21 | throw std::invalid_argument("Could not find request."); | ||
| 22 | } | ||
| 23 | |||
| 24 | request& req = requests_[token]; | ||
| 25 | size_t nextId = req.subscribers.size(); | ||
| 26 | req.subscribers.push_back(callback); | ||
| 27 | } | ||
| 28 | |||
| 29 | void database::post(const std::string& token, const std::string& msg) { | ||
| 30 | std::lock_guard state_guard(mutex_); | ||
| 31 | |||
| 32 | if (!requests_.count(token)) { | ||
| 33 | throw std::invalid_argument("Could not find request."); | ||
| 34 | } | ||
| 35 | |||
| 36 | request& req = requests_.at(token); | ||
| 37 | |||
| 38 | for (std::optional<subscribe_callback_type>& callback : req.subscribers) { | ||
| 39 | if (callback) { | ||
| 40 | try { | ||
| 41 | (*callback)(msg); | ||
| 42 | } catch (const std::exception& ex) { | ||
| 43 | callback = std::nullopt; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | void database::setResult(const std::string& token, const std::string& result) { | ||
| 50 | std::lock_guard state_guard(mutex_); | ||
| 51 | |||
| 52 | if (!requests_.count(token)) { | ||
| 53 | throw std::invalid_argument("Could not find request."); | ||
| 54 | } | ||
| 55 | |||
| 56 | request& req = requests_[token]; | ||
| 57 | req.result = result; | ||
| 58 | } | ||
| 59 | |||
| 60 | const std::string& database::getResult(const std::string& token) { | ||
| 61 | std::lock_guard state_guard(mutex_); | ||
| 62 | |||
| 63 | if (!requests_.count(token)) { | ||
| 64 | throw std::invalid_argument("Could not find request."); | ||
| 65 | } | ||
| 66 | |||
| 67 | const request& req = requests_.at(token); | ||
| 68 | return req.result; | ||
| 69 | } | ||
| 70 | |||
| 71 | void database::mark_done(const std::string& token) { | ||
| 72 | std::lock_guard state_guard(mutex_); | ||
| 73 | |||
| 74 | if (!requests_.count(token)) { | ||
| 75 | throw std::invalid_argument("Could not find request."); | ||
| 76 | } | ||
| 77 | |||
| 78 | request& req = requests_[token]; | ||
| 79 | req.done = true; | ||
| 80 | } | ||
| 81 | |||
| 82 | bool database::is_done(const std::string& token) { | ||
| 83 | std::lock_guard state_guard(mutex_); | ||
| 84 | |||
| 85 | if (!requests_.count(token)) { | ||
| 86 | throw std::invalid_argument("Could not find request."); | ||
| 87 | } | ||
| 88 | |||
| 89 | const request& req = requests_.at(token); | ||
| 90 | return req.done; | ||
| 91 | } | ||
