170 lines
4.9 KiB
C++
170 lines
4.9 KiB
C++
#pragma once
|
|
|
|
#include "rublon/memory.hpp"
|
|
#include <memory>
|
|
#include <memory_resource>
|
|
#include <rapidjson/document.h>
|
|
#include <rublon/bits.hpp>
|
|
#include <rublon/configuration.hpp>
|
|
#include <rublon/json.hpp>
|
|
#include <rublon/utils.hpp>
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
|
|
namespace rublon {
|
|
|
|
|
|
// class LoggingMemoryResource : public std::pmr::memory_resource {
|
|
// public:
|
|
// LoggingMemoryResource(std::pmr::memory_resource* upstream = std::pmr::get_default_resource())
|
|
// : upstream_(upstream), allocated_bytes_(0)
|
|
// {
|
|
// pid_t pid = getpid();
|
|
// std::ostringstream filename;
|
|
// filename << "/tmp/memory" << pid << ".log";
|
|
// log_file_.open(filename.str(), std::ios::out | std::ios::trunc);
|
|
|
|
// if (!log_file_) {
|
|
// throw std::runtime_error("Failed to open log file");
|
|
// }
|
|
// log("Memory logging started.");
|
|
// }
|
|
|
|
// ~LoggingMemoryResource() override {
|
|
// log("Memory logging ended.");
|
|
// log_file_.close();
|
|
// }
|
|
|
|
// protected:
|
|
// void* do_allocate(std::size_t bytes, std::size_t alignment) override {
|
|
// std::lock_guard<std::mutex> lock(mutex_);
|
|
// void* ptr = upstream_->allocate(bytes, alignment);
|
|
// allocated_bytes_ += bytes;
|
|
// active_allocations_[ptr] = bytes;
|
|
|
|
// log("ALLOC", ptr, bytes, alignment);
|
|
// return ptr;
|
|
// }
|
|
|
|
// void do_deallocate(void* ptr, std::size_t bytes, std::size_t alignment) override {
|
|
// std::lock_guard<std::mutex> lock(mutex_);
|
|
// auto it = active_allocations_.find(ptr);
|
|
// if (it != active_allocations_.end()) {
|
|
// allocated_bytes_ -= it->second;
|
|
// active_allocations_.erase(it);
|
|
// }
|
|
|
|
// log("FREE", ptr, bytes, alignment);
|
|
// upstream_->deallocate(ptr, bytes, alignment);
|
|
// }
|
|
|
|
// bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
|
|
// return this == &other;
|
|
// }
|
|
|
|
// private:
|
|
// std::pmr::memory_resource* upstream_;
|
|
// std::ofstream log_file_;
|
|
// std::mutex mutex_;
|
|
// std::size_t allocated_bytes_;
|
|
// std::map<void*, std::size_t> active_allocations_;
|
|
|
|
// void log(const std::string& action, void* ptr = nullptr, std::size_t bytes = 0, std::size_t alignment = 0) {
|
|
// auto now = std::chrono::system_clock::now();
|
|
// auto now_time = std::chrono::system_clock::to_time_t(now);
|
|
// log_file_ << std::put_time(std::localtime(&now_time), "%F %T")
|
|
// << " | " << std::setw(6) << action;
|
|
|
|
// if (ptr) {
|
|
// log_file_ << " | ptr=" << ptr << " bytes=" << bytes << " align=" << alignment;
|
|
// }
|
|
|
|
// log_file_ << " | total=" << allocated_bytes_ << " bytes\n";
|
|
// log_file_.flush();
|
|
// }
|
|
// };
|
|
|
|
// std::pmr::unsynchronized_pool_resource resource{};
|
|
// LoggingMemoryResource mr{&resource};
|
|
|
|
class DefaultResource {
|
|
public:
|
|
DefaultResource() {
|
|
// memory::set_default_resource(&mr);
|
|
}
|
|
};
|
|
|
|
class Session : public DefaultResource {
|
|
std::pmr::memory_resource * mr;
|
|
const Pam_t & _pam;
|
|
Configuration _config;
|
|
std::pmr::string _tid;
|
|
std::pmr::string _accessToken;
|
|
|
|
public:
|
|
Session(const Pam_t & pam) : DefaultResource{}, mr{memory::default_resource()}, _pam{pam}, _config{mr}, _tid{mr}, _accessToken{mr} {
|
|
details::initLog();
|
|
}
|
|
|
|
Session(Session &&) noexcept = delete;
|
|
Session(const Session &) = delete;
|
|
|
|
Session & operator=(Session &&) noexcept = delete;
|
|
Session & operator=(const Session &) = delete;
|
|
|
|
const auto & pam() const {
|
|
return _pam;
|
|
}
|
|
auto & config() {
|
|
return _config;
|
|
}
|
|
const auto & config() const {
|
|
return _config;
|
|
}
|
|
|
|
std::string_view systemToken() const {
|
|
return _config.systemToken;
|
|
}
|
|
const char * csystemToken() const {
|
|
return systemToken().data();
|
|
}
|
|
|
|
bool inInteractiveMode() const {
|
|
return _config.nonInteractiveMode == false;
|
|
}
|
|
|
|
void updateTransactionId(const Value * tid) {
|
|
log(LogLevel::Debug, "Set transaction ID %s", tid->GetString());
|
|
_tid = tid->GetString();
|
|
}
|
|
|
|
void updateAccessToken(const Value * accessToken) {
|
|
log(LogLevel::Debug, "AccessToken %s", accessToken->GetString());
|
|
_accessToken = accessToken->GetString();
|
|
}
|
|
bool hasAccessToken() const {
|
|
return not _accessToken.empty();
|
|
}
|
|
std::string_view accessToken() const {
|
|
return _accessToken;
|
|
}
|
|
const char * caccessToken() const {
|
|
return _accessToken.c_str();
|
|
}
|
|
|
|
std::string_view transactionID() const {
|
|
if(_tid.empty()) {
|
|
log(LogLevel::Warning, "Transaction ID is not defined, but requested");
|
|
return "";
|
|
} else {
|
|
return _tid;
|
|
}
|
|
}
|
|
const char * ctransactionID() const {
|
|
return transactionID().data();
|
|
}
|
|
};
|
|
|
|
} // namespace rublon
|