Delete the echoion Logger adapter (Logger.{hpp,cpp}) and the
ILogger/LoggerRegistry indirection. Core::installLogging() builds the
console + split-file Void::Logging::MultiLogger and installs it as the
process-wide Void::Log sink via Void::Log::make_sink (logSink_ owns it for
the process lifetime). The ~450 ECHOION_LOG_* call sites across EchoionCore
are rewritten to Void::Log::<level>("<Subsystem>", "{}", <expr>) with
per-file module tags (warning->warn, critical->fatal); the pre-formatted
message is passed as the single "{}" argument. Build green.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
204 lines
4.6 KiB
C++
204 lines
4.6 KiB
C++
#include <Void/Log/Log.hpp>
|
|
#include "echoion/core/SessionManager.hpp"
|
|
#include <format>
|
|
#include <iomanip>
|
|
#include <random>
|
|
#include <sstream>
|
|
|
|
namespace Echoion::Core
|
|
{
|
|
|
|
SessionManager::SessionManager() = default;
|
|
|
|
SessionManager::SessionManager(const Config& config) : config_(config) {}
|
|
|
|
bool SessionManager::initialize()
|
|
{
|
|
running_ = true;
|
|
Void::Log::info("SessionManager", "{}", std::format(
|
|
"SessionManager initialized: max {} sessions, timeout {}s",
|
|
config_.maxSessions, config_.sessionTimeout.count()));
|
|
return true;
|
|
}
|
|
|
|
void SessionManager::shutdown()
|
|
{
|
|
running_ = false;
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
sessions_.clear();
|
|
Void::Log::info("SessionManager", "{}", "SessionManager shut down");
|
|
}
|
|
|
|
std::string SessionManager::createSession(uint64_t clientId)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
// Check capacity
|
|
if (sessions_.size() >= config_.maxSessions) {
|
|
Void::Log::warn("SessionManager", "{}",
|
|
"Session capacity reached, rejecting new session");
|
|
return "";
|
|
}
|
|
|
|
// Generate unique token
|
|
std::string token = generateToken();
|
|
|
|
Session session;
|
|
session.clientId = clientId;
|
|
session.authToken = token;
|
|
session.lastActivity = std::chrono::steady_clock::now();
|
|
session.createdAt = session.lastActivity;
|
|
session.isAuthenticated = false;
|
|
|
|
sessions_[token] = std::move(session);
|
|
|
|
Void::Log::info("SessionManager", "{}",
|
|
std::format("Created session for client {}", clientId));
|
|
return token;
|
|
}
|
|
|
|
bool SessionManager::authenticateSession(const std::string& token)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
auto it = sessions_.find(token);
|
|
if (it == sessions_.end()) {
|
|
return false;
|
|
}
|
|
|
|
it->second.isAuthenticated = true;
|
|
it->second.lastActivity = std::chrono::steady_clock::now();
|
|
|
|
return true;
|
|
}
|
|
|
|
bool SessionManager::validateSession(const std::string& token)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
auto it = sessions_.find(token);
|
|
if (it == sessions_.end()) {
|
|
return false;
|
|
}
|
|
|
|
// Check timeout
|
|
auto elapsed =
|
|
std::chrono::steady_clock::now() - it->second.lastActivity;
|
|
if (elapsed > config_.sessionTimeout) {
|
|
sessions_.erase(it);
|
|
return false;
|
|
}
|
|
|
|
return it->second.isAuthenticated;
|
|
}
|
|
|
|
void SessionManager::updateActivity(const std::string& token)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
auto it = sessions_.find(token);
|
|
if (it != sessions_.end()) {
|
|
it->second.lastActivity = std::chrono::steady_clock::now();
|
|
}
|
|
}
|
|
|
|
std::optional<SessionManager::Session>
|
|
SessionManager::getSession(const std::string& token) const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
auto it = sessions_.find(token);
|
|
if (it != sessions_.end()) {
|
|
return it->second;
|
|
}
|
|
|
|
return std::nullopt;
|
|
}
|
|
|
|
void SessionManager::endSession(const std::string& token)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
sessions_.erase(token);
|
|
}
|
|
|
|
void SessionManager::endSessionByClientId(uint64_t clientId)
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
for (auto it = sessions_.begin(); it != sessions_.end();) {
|
|
if (it->second.clientId == clientId) {
|
|
it = sessions_.erase(it);
|
|
} else {
|
|
++it;
|
|
}
|
|
}
|
|
}
|
|
|
|
std::vector<SessionManager::Session> SessionManager::getAllSessions() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
std::vector<Session> result;
|
|
result.reserve(sessions_.size());
|
|
|
|
for (const auto& [token, session] : sessions_) {
|
|
result.push_back(session);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
size_t SessionManager::getSessionCount() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return sessions_.size();
|
|
}
|
|
|
|
size_t SessionManager::cleanupExpiredSessions()
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
size_t cleaned = 0;
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
for (auto it = sessions_.begin(); it != sessions_.end();) {
|
|
auto elapsed = now - it->second.lastActivity;
|
|
if (elapsed > config_.sessionTimeout) {
|
|
it = sessions_.erase(it);
|
|
cleaned++;
|
|
} else {
|
|
++it;
|
|
}
|
|
}
|
|
|
|
if (cleaned > 0) {
|
|
Void::Log::info("SessionManager", "{}",
|
|
std::format("Cleaned up {} expired sessions", cleaned));
|
|
}
|
|
|
|
return cleaned;
|
|
}
|
|
|
|
bool SessionManager::isAtCapacity() const
|
|
{
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
return sessions_.size() >= config_.maxSessions;
|
|
}
|
|
|
|
std::string SessionManager::generateToken()
|
|
{
|
|
// Generate random 32-byte token
|
|
static std::random_device rd;
|
|
static std::mt19937 gen(rd());
|
|
static std::uniform_int_distribution<> dis(0, 255);
|
|
|
|
std::stringstream ss;
|
|
for (int i = 0; i < 32; ++i) {
|
|
ss << std::hex << std::setw(2) << std::setfill('0') << dis(gen);
|
|
}
|
|
|
|
return ss.str();
|
|
}
|
|
|
|
} // namespace Echoion::Core
|