mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-11-03 23:15:52 +00:00
b81f7025c9
Replaces custom logging system with spdlog-based oxen logging. This commit mainly replaces the backend logging with the spdlog-based system, but doesn't (yet) convert all the existing LogWarn, etc. to use the new format-based logging. New logging statements will look like: llarp::log::warning(cat, "blah: {}", val); where `cat` should be set up in each .cpp or cluster of .cpp files, as described in the oxen-logging README. As part of spdlog we get fmt, which gives us nice format strings, where are applied generously in this commit. Making types printable now requires two steps: - add a ToString() method - add this specialization: template <> constexpr inline bool llarp::IsToStringFormattable<llarp::Whatever> = true; This will then allow the type to be printed as a "{}" value in a fmt::format string. This is applied to all our printable types here, and all of the `operator<<` are removed. This commit also: - replaces various uses of `operator<<` to ToString() - replaces various uses of std::stringstream with either fmt::format or plain std::string - Rename some to_string and toString() methods to ToString() for consistency (and to work with fmt) - Replace `stringify(...)` and `make_exception` usage with fmt::format (and remove stringify/make_exception from util/str.hpp).
128 lines
2.8 KiB
C++
128 lines
2.8 KiB
C++
#include "context.hpp"
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
|
|
namespace llarp
|
|
{
|
|
namespace exit
|
|
{
|
|
Context::Context(AbstractRouter* r) : m_Router(r)
|
|
{}
|
|
Context::~Context() = default;
|
|
|
|
void
|
|
Context::Tick(llarp_time_t now)
|
|
{
|
|
{
|
|
auto itr = m_Exits.begin();
|
|
while (itr != m_Exits.end())
|
|
{
|
|
itr->second->Tick(now);
|
|
++itr;
|
|
}
|
|
}
|
|
{
|
|
auto itr = m_Closed.begin();
|
|
while (itr != m_Closed.end())
|
|
{
|
|
if ((*itr)->ShouldRemove())
|
|
itr = m_Closed.erase(itr);
|
|
else
|
|
++itr;
|
|
}
|
|
}
|
|
}
|
|
|
|
void
|
|
Context::Stop()
|
|
{
|
|
auto itr = m_Exits.begin();
|
|
while (itr != m_Exits.end())
|
|
{
|
|
itr->second->Stop();
|
|
m_Closed.emplace_back(std::move(itr->second));
|
|
itr = m_Exits.erase(itr);
|
|
}
|
|
}
|
|
|
|
util::StatusObject
|
|
Context::ExtractStatus() const
|
|
{
|
|
util::StatusObject obj{};
|
|
auto itr = m_Exits.begin();
|
|
while (itr != m_Exits.end())
|
|
{
|
|
obj[itr->first] = itr->second->ExtractStatus();
|
|
++itr;
|
|
}
|
|
return obj;
|
|
}
|
|
|
|
void
|
|
Context::CalculateExitTraffic(TrafficStats& stats)
|
|
{
|
|
auto itr = m_Exits.begin();
|
|
while (itr != m_Exits.end())
|
|
{
|
|
itr->second->CalculateTrafficStats(stats);
|
|
++itr;
|
|
}
|
|
}
|
|
|
|
exit::Endpoint*
|
|
Context::FindEndpointForPath(const PathID_t& path) const
|
|
{
|
|
auto itr = m_Exits.begin();
|
|
while (itr != m_Exits.end())
|
|
{
|
|
auto ep = itr->second->FindEndpointByPath(path);
|
|
if (ep)
|
|
return ep;
|
|
++itr;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
bool
|
|
Context::ObtainNewExit(const PubKey& pk, const PathID_t& path, bool permitInternet)
|
|
{
|
|
auto itr = m_Exits.begin();
|
|
while (itr != m_Exits.end())
|
|
{
|
|
if (itr->second->AllocateNewExit(pk, path, permitInternet))
|
|
return true;
|
|
++itr;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
std::shared_ptr<handlers::ExitEndpoint>
|
|
Context::GetExitEndpoint(std::string name) const
|
|
{
|
|
if (auto itr = m_Exits.find(name); itr != m_Exits.end())
|
|
{
|
|
return itr->second;
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
void
|
|
Context::AddExitEndpoint(
|
|
const std::string& name, const NetworkConfig& networkConfig, const DnsConfig& dnsConfig)
|
|
{
|
|
if (m_Exits.find(name) != m_Exits.end())
|
|
throw std::invalid_argument{fmt::format("An exit with name {} already exists", name)};
|
|
|
|
auto endpoint = std::make_unique<handlers::ExitEndpoint>(name, m_Router);
|
|
endpoint->Configure(networkConfig, dnsConfig);
|
|
|
|
// add endpoint
|
|
if (!endpoint->Start())
|
|
throw std::runtime_error{fmt::format("Failed to start endpoint {}", name)};
|
|
|
|
m_Exits.emplace(name, std::move(endpoint));
|
|
}
|
|
|
|
} // namespace exit
|
|
} // namespace llarp
|