Initial commit
This commit is contained in:
commit
a583c17916
56 files changed
+11025
No files matched your search
@@ -0,0 +1,115 @@
|
||||
#include "echoion/core/SubscriptionManager.hpp"
|
||||
#include "echoion/core/Logger.hpp"
|
||||
|
||||
namespace Echoion::Core {
|
||||
|
||||
SubscriptionManager::SubscriptionManager() = default;
|
||||
|
||||
SubscriptionManager::~SubscriptionManager() {
|
||||
shutdown();
|
||||
}
|
||||
|
||||
Api::Result<void> SubscriptionManager::initialize(std::shared_ptr<Api::Database> database) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
if (initialized_) {
|
||||
return {};
|
||||
}
|
||||
|
||||
database_ = std::move(database);
|
||||
if (!database_) {
|
||||
return std::unexpected(Api::Error(Api::ErrorCode::ServiceUnavailable,
|
||||
"Database not available"));
|
||||
}
|
||||
|
||||
// Load existing subscriptions from database
|
||||
auto result = database_->getAllSubscribers();
|
||||
if (!result) {
|
||||
ECHOION_LOG_WARNING(std::format("Failed to load subscriptions: {}", result.error().message));
|
||||
// Not fatal - start with empty subscriptions
|
||||
} else {
|
||||
for (auto clientId : *result) {
|
||||
subscribedClients_.insert(clientId);
|
||||
}
|
||||
}
|
||||
|
||||
initialized_ = true;
|
||||
ECHOION_LOG_INFO(std::format("SubscriptionManager initialized ({} subscribers)",
|
||||
subscribedClients_.size()));
|
||||
return {};
|
||||
}
|
||||
|
||||
void SubscriptionManager::shutdown() {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
if (!initialized_) {
|
||||
return;
|
||||
}
|
||||
|
||||
initialized_ = false;
|
||||
database_.reset();
|
||||
ECHOION_LOG_INFO("SubscriptionManager shutdown");
|
||||
}
|
||||
|
||||
bool SubscriptionManager::subscribe(uint64_t clientId) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
// Check if already subscribed (ensures uniqueness)
|
||||
if (subscribedClients_.contains(clientId)) {
|
||||
return false; // Already subscribed
|
||||
}
|
||||
|
||||
// Add to database first
|
||||
if (database_) {
|
||||
auto result = database_->addSubscriber(clientId);
|
||||
if (!result) {
|
||||
ECHOION_LOG_ERROR(std::format("Failed to add subscriber {} to database: {}",
|
||||
clientId, result.error().message));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
subscribedClients_.insert(clientId);
|
||||
ECHOION_LOG_INFO(std::format("Client {} subscribed", clientId));
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SubscriptionManager::unsubscribe(uint64_t clientId) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
bool removed = subscribedClients_.erase(clientId) > 0;
|
||||
if (removed) {
|
||||
// Remove from database
|
||||
if (database_) {
|
||||
auto result = database_->removeSubscriber(clientId);
|
||||
if (!result) {
|
||||
ECHOION_LOG_ERROR(std::format("Failed to remove subscriber {} from database: {}",
|
||||
clientId, result.error().message));
|
||||
}
|
||||
}
|
||||
ECHOION_LOG_INFO(std::format("Client {} unsubscribed", clientId));
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
bool SubscriptionManager::isSubscribed(uint64_t clientId) const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return subscribedClients_.contains(clientId);
|
||||
}
|
||||
|
||||
std::set<uint64_t> SubscriptionManager::getSubscribedClients() const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return subscribedClients_;
|
||||
}
|
||||
|
||||
bool SubscriptionManager::hasSubscribers() const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return !subscribedClients_.empty();
|
||||
}
|
||||
|
||||
size_t SubscriptionManager::getSubscriberCount() const {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return subscribedClients_.size();
|
||||
}
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user