1.4.10 updates

This commit is contained in:
WolverinDEV
2020-02-28 11:24:07 +01:00
parent 2b929fd3c0
commit 0d456eea5d
48 changed files with 2267 additions and 781 deletions
+30 -18
View File
@@ -1,26 +1,30 @@
#include "log/LogUtils.h"
#include "LicenseManager.h"
#include "DatabaseHandler.h"
using namespace license;
using namespace license::server;
using namespace license::server::database;
using namespace std;
using namespace std::chrono;
using KeyIdCache = LicenseManager::KeyIdCache;
KeyIdCache::KeyIdCache(license::server::LicenseManager *handle) : handle(handle) {}
KeyIdCache::KeyIdCache(DatabaseHandler *handle) : handle(handle) {}
std::string KeyIdCache::getKey(size_t keyId) {
void KeyIdCache::clear_cache() {
std::lock_guard elock{this->entry_lock};
this->entries.clear();
}
std::string KeyIdCache::get_key_from_id(size_t keyId) {
{
threads::MutexLock lock(this->entry_lock);
std::lock_guard elock{this->entry_lock};
for(const auto& entry : this->entries)
if(entry->keyId == keyId) return entry->key;
}
sql::command(this->handle->database, "SELECT `key`, `keyId` FROM `license` WHERE `keyId` = :key", variable{":key", keyId})
sql::command(this->handle->sql(), "SELECT `key`, `keyId` FROM `license` WHERE `keyId` = :key", variable{":key", keyId})
.query(&KeyIdCache::insert_entry, this);
{
threads::MutexLock lock(this->entry_lock);
std::lock_guard elock{this->entry_lock};
for(const auto& entry : this->entries)
if(entry->keyId == keyId) return entry->key;
@@ -28,20 +32,20 @@ std::string KeyIdCache::getKey(size_t keyId) {
}
}
size_t KeyIdCache::getKeyId(const std::string &key) {
size_t KeyIdCache::get_key_id_from_key(const std::string &key) {
{
threads::MutexLock lock(this->entry_lock);
std::lock_guard elock{this->entry_lock};
for(const auto& entry : this->entries)
if(entry->key == key) return entry->keyId;
}
auto result = sql::command(this->handle->database, "SELECT `key`, `keyId` FROM `license` WHERE `key` = :key", variable{":key", key})
auto result = sql::command(this->handle->sql(), "SELECT `key`, `keyId` FROM `license` WHERE `key` = :key", variable{":key", key})
.query(&KeyIdCache::insert_entry, this);
if(!result)
logError(LOG_GENERAL, "Failed to query key id for license. Query resulted in {}", result.fmtStr());
{
threads::MutexLock lock(this->entry_lock);
std::lock_guard elock{this->entry_lock};
for(const auto& entry : this->entries)
if(entry->key == key)
@@ -52,16 +56,24 @@ size_t KeyIdCache::getKeyId(const std::string &key) {
}
int KeyIdCache::insert_entry(int length, std::string *value, std::string *names) {
string key;
size_t keyId = 0;
for(int index = 0; index < length; index++)
string key{"unknown"};
size_t keyId{0};
for(int index = 0; index < length; index++) {
if(names[index] == "key")
key = value[index];
else if(names[index] == "keyId")
keyId = stoll(value[index]);
keyId = std::strtoll(value[index].c_str(), nullptr, 10);
}
if(!keyId) {
logWarning(LOG_GENERAL, "Failed to parse key id for key {}", key);
return 0;
}
{
threads::MutexLock lock(this->entry_lock);
this->entries.push_back(new KeyIdCache::CacheEntry{key, keyId, system_clock::now()});
auto entry = new KeyIdCache::CacheEntry{key, keyId, system_clock::now()};
std::lock_guard elock{this->entry_lock};
this->entries.emplace_back(entry);
}
return 0;
}