Teaspeak-Server/license/server/LicenseManager.h

80 lines
3.1 KiB
C
Raw Normal View History

#pragma once
#include <sql/SqlQuery.h>
#include <shared/License.h>
#include <LicenseRequest.pb.h>
namespace license {
namespace server {
class LicenseManager {
public:
class KeyIdCache {
struct CacheEntry {
std::string key;
size_t keyId;
std::chrono::system_clock::time_point age;
};
public:
KeyIdCache(LicenseManager*);
void cache_keys(const std::deque<std::string>& /* keys */);
std::string getKey(size_t keyId);
size_t getKeyId(const std::string&);
private:
int insert_entry(int, std::string*, std::string*);
LicenseManager* handle;
threads::Mutex entry_lock;
threads::Mutex entry_update_lock;
std::deque<CacheEntry*> entries;
};
struct UserStatistics {
std::chrono::system_clock::time_point timestamp;
uint64_t clients_online;
uint64_t web_clients_online;
uint64_t bots_online;
uint64_t queries_online;
uint64_t servers_online;
};
struct ExtendedUserStatistics : public UserStatistics{
std::string ip_address;
};
struct GlobalUserStatistics : public UserStatistics {
uint64_t instance_online;
};
struct GlobalVersionsStatistic {
std::chrono::system_clock::time_point timestamp;
std::map<std::string, uint32_t> versions;
};
public:
LicenseManager(sql::SqlManager* database);
~LicenseManager();
bool setup(std::string&);
bool validLicenseKey(const std::string& /* key */);
std::shared_ptr<LicenseInfo> licenseInfo(const std::string&);
std::map<std::string, std::shared_ptr<LicenseInfo>> listLicenses(int offset = 0, int limit = -1);
bool registerLicense(const std::string& /* key */, const std::shared_ptr<LicenseInfo>& /* info */, const std::string& /* issuer */);
bool updateLicenseInfo(const std::string&, const std::shared_ptr<LicenseInfo>&);
bool deleteLicense(const std::string& /* key */, bool /* full */ = false);
bool logRequest(const std::string& /* key */, const std::string& /* unique_id */, const std::string& /* ip */, const std::string& /* version */, int /* result */);
bool logStatistic(const std::string& /* key */, const std::string& /* unique_id */, const std::string& /* ip */, const ts::proto::license::PropertyUpdateRequest&);
//std::deque<std::unique_ptr<ExtendedUserStatistics>> list_statistics_user(const std::string& key, const std::chrono::system_clock::time_point& begin, const std::chrono::system_clock::time_point& end);
std::deque<std::unique_ptr<GlobalUserStatistics>> list_statistics_user(const std::chrono::system_clock::time_point& begin, const std::chrono::system_clock::time_point& end, const std::chrono::milliseconds& /* interval */);
std::deque<std::unique_ptr<GlobalVersionsStatistic>> list_statistics_version(const std::chrono::system_clock::time_point& begin, const std::chrono::system_clock::time_point& end, const std::chrono::milliseconds& /* interval */);
inline sql::SqlManager* sql() { return this->database; }
private:
std::shared_ptr<KeyIdCache> id_cache;
sql::SqlManager* database;
};
}
}