|
|
@ -11,7 +11,7 @@ |
|
|
|
#include <list> |
|
|
|
#include <list> |
|
|
|
#include <thread> |
|
|
|
#include <thread> |
|
|
|
#include <boost/property_tree/ptree.hpp> |
|
|
|
#include <boost/property_tree/ptree.hpp> |
|
|
|
#include <boost/property_tree/ini_parser.hpp> |
|
|
|
#include <boost/property_tree/json_parser.hpp> |
|
|
|
#include "Base.h" |
|
|
|
#include "Base.h" |
|
|
|
#include "FS.h" |
|
|
|
#include "FS.h" |
|
|
|
#include "Log.h" |
|
|
|
#include "Log.h" |
|
|
@ -22,159 +22,119 @@ namespace i2p |
|
|
|
{ |
|
|
|
{ |
|
|
|
namespace data |
|
|
|
namespace data |
|
|
|
{ |
|
|
|
{ |
|
|
|
static i2p::fs::HashedStorage g_ProfilesStorage("peerProfiles", "p", "profile-", "txt"); |
|
|
|
|
|
|
|
static std::unordered_map<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > g_Profiles; |
|
|
|
static std::unordered_map<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > g_Profiles; |
|
|
|
static std::mutex g_ProfilesMutex; |
|
|
|
static std::mutex g_ProfilesMutex; |
|
|
|
|
|
|
|
|
|
|
|
static boost::posix_time::ptime GetTime () |
|
|
|
static uint64_t GetTime () |
|
|
|
{ |
|
|
|
{ |
|
|
|
return boost::posix_time::second_clock::local_time(); |
|
|
|
return i2p::util::GetSecondsSinceEpoch (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
RouterProfile::RouterProfile (): |
|
|
|
RouterProfile::RouterProfile (): |
|
|
|
m_LastUpdateTime (GetTime ()), m_IsUpdated (false), |
|
|
|
m_LastUpdateTime (0), m_LastDeclineTime (0), m_LastUnreachableTime (0), |
|
|
|
m_LastDeclineTime (0), m_LastUnreachableTime (0), |
|
|
|
|
|
|
|
m_NumTunnelsAgreed (0), m_NumTunnelsDeclined (0), m_NumTunnelsNonReplied (0), |
|
|
|
m_NumTunnelsAgreed (0), m_NumTunnelsDeclined (0), m_NumTunnelsNonReplied (0), |
|
|
|
m_NumTimesTaken (0), m_NumTimesRejected (0), m_HasConnected (false) |
|
|
|
m_NumTimesTaken (0), m_NumTimesRejected (0), m_HasConnected (false) |
|
|
|
{ |
|
|
|
{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void RouterProfile::UpdateTime () |
|
|
|
std::string RouterProfile::Dump (const std::string& peerid) |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_LastUpdateTime = GetTime (); |
|
|
|
|
|
|
|
m_IsUpdated = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void RouterProfile::Save (const IdentHash& identHash) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// fill sections
|
|
|
|
|
|
|
|
boost::property_tree::ptree participation; |
|
|
|
|
|
|
|
participation.put (PEER_PROFILE_PARTICIPATION_AGREED, m_NumTunnelsAgreed); |
|
|
|
|
|
|
|
participation.put (PEER_PROFILE_PARTICIPATION_DECLINED, m_NumTunnelsDeclined); |
|
|
|
|
|
|
|
participation.put (PEER_PROFILE_PARTICIPATION_NON_REPLIED, m_NumTunnelsNonReplied); |
|
|
|
|
|
|
|
boost::property_tree::ptree usage; |
|
|
|
|
|
|
|
usage.put (PEER_PROFILE_USAGE_TAKEN, m_NumTimesTaken); |
|
|
|
|
|
|
|
usage.put (PEER_PROFILE_USAGE_REJECTED, m_NumTimesRejected); |
|
|
|
|
|
|
|
usage.put (PEER_PROFILE_USAGE_CONNECTED, m_HasConnected); |
|
|
|
|
|
|
|
// fill property tree
|
|
|
|
|
|
|
|
boost::property_tree::ptree pt; |
|
|
|
boost::property_tree::ptree pt; |
|
|
|
pt.put (PEER_PROFILE_LAST_UPDATE_TIME, boost::posix_time::to_simple_string (m_LastUpdateTime)); |
|
|
|
std::stringstream ss; |
|
|
|
if (m_LastUnreachableTime) |
|
|
|
|
|
|
|
pt.put (PEER_PROFILE_LAST_UNREACHABLE_TIME, m_LastUnreachableTime); |
|
|
|
|
|
|
|
pt.put_child (PEER_PROFILE_SECTION_PARTICIPATION, participation); |
|
|
|
|
|
|
|
pt.put_child (PEER_PROFILE_SECTION_USAGE, usage); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// save to file
|
|
|
|
pt.put(PEER_PROFILE_PEER_ID, peerid); |
|
|
|
std::string ident = identHash.ToBase64 (); |
|
|
|
/* "times" hash */ |
|
|
|
std::string path = g_ProfilesStorage.Path(ident); |
|
|
|
pt.put(PEER_PROFILE_LAST_UPDATE_TIME, m_LastUpdateTime); |
|
|
|
|
|
|
|
pt.put(PEER_PROFILE_LAST_DECLINE_TIME, m_LastDeclineTime); |
|
|
|
|
|
|
|
pt.put(PEER_PROFILE_LAST_UNREACHABLE_TIME, m_LastUnreachableTime); |
|
|
|
|
|
|
|
/* "tunnels" hash */ |
|
|
|
|
|
|
|
pt.put(PEER_PROFILE_PARTICIPATION_AGREED, m_NumTunnelsAgreed); |
|
|
|
|
|
|
|
pt.put(PEER_PROFILE_PARTICIPATION_DECLINED, m_NumTunnelsDeclined); |
|
|
|
|
|
|
|
pt.put(PEER_PROFILE_PARTICIPATION_NON_REPLIED, m_NumTunnelsNonReplied); |
|
|
|
|
|
|
|
/* "usage" hash */ |
|
|
|
|
|
|
|
pt.put(PEER_PROFILE_USAGE_TAKEN, m_NumTimesTaken); |
|
|
|
|
|
|
|
pt.put(PEER_PROFILE_USAGE_REJECTED, m_NumTimesRejected); |
|
|
|
|
|
|
|
pt.put(PEER_PROFILE_USAGE_CONNECTED, m_HasConnected); |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
try { |
|
|
|
boost::property_tree::write_ini (path, pt); |
|
|
|
/* convert ptree to single line json string */ |
|
|
|
|
|
|
|
boost::property_tree::write_json (ss, pt, false); |
|
|
|
} catch (std::exception& ex) { |
|
|
|
} catch (std::exception& ex) { |
|
|
|
/* boost exception verbose enough */ |
|
|
|
/* boost exception verbose enough */ |
|
|
|
LogPrint (eLogError, "Profiling: ", ex.what ()); |
|
|
|
LogPrint (eLogError, "Profiling: can't serialize data to json -- ", ex.what ()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return ss.str(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void RouterProfile::Load (const IdentHash& identHash) |
|
|
|
std::string RouterProfile::Load (const std::string& jsondata) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::string ident = identHash.ToBase64 (); |
|
|
|
|
|
|
|
std::string path = g_ProfilesStorage.Path(ident); |
|
|
|
|
|
|
|
boost::property_tree::ptree pt; |
|
|
|
boost::property_tree::ptree pt; |
|
|
|
|
|
|
|
std::stringstream ss(jsondata); |
|
|
|
|
|
|
|
std::string peerid = ""; |
|
|
|
|
|
|
|
|
|
|
|
if (!i2p::fs::Exists(path)) |
|
|
|
try { |
|
|
|
{ |
|
|
|
boost::property_tree::read_json (ss, pt); |
|
|
|
LogPrint(eLogWarning, "Profiling: No profile yet for ", ident); |
|
|
|
} catch (std::exception& ex) { |
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
boost::property_tree::read_ini (path, pt); |
|
|
|
|
|
|
|
} catch (std::exception& ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
/* boost exception verbose enough */ |
|
|
|
/* boost exception verbose enough */ |
|
|
|
LogPrint (eLogError, "Profiling: ", ex.what ()); |
|
|
|
LogPrint (eLogError, "Profiling: can't parse json data -- ", ex.what ()); |
|
|
|
return; |
|
|
|
return std::string(""); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
try |
|
|
|
try { |
|
|
|
{ |
|
|
|
peerid = pt.get<std::string>(PEER_PROFILE_PEER_ID); |
|
|
|
auto t = pt.get (PEER_PROFILE_LAST_UPDATE_TIME, ""); |
|
|
|
} catch (std::exception& ex) { |
|
|
|
if (t.length () > 0) |
|
|
|
LogPrint (eLogError, "Profiling: Can't read profile data: missing peerid"); |
|
|
|
m_LastUpdateTime = boost::posix_time::time_from_string (t); |
|
|
|
return std::string(""); |
|
|
|
if ((GetTime () - m_LastUpdateTime).hours () < PEER_PROFILE_EXPIRATION_TIMEOUT) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_LastUnreachableTime = pt.get (PEER_PROFILE_LAST_UNREACHABLE_TIME, 0); |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// read participations
|
|
|
|
|
|
|
|
auto participations = pt.get_child (PEER_PROFILE_SECTION_PARTICIPATION); |
|
|
|
|
|
|
|
m_NumTunnelsAgreed = participations.get (PEER_PROFILE_PARTICIPATION_AGREED, 0); |
|
|
|
|
|
|
|
m_NumTunnelsDeclined = participations.get (PEER_PROFILE_PARTICIPATION_DECLINED, 0); |
|
|
|
|
|
|
|
m_NumTunnelsNonReplied = participations.get (PEER_PROFILE_PARTICIPATION_NON_REPLIED, 0); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (boost::property_tree::ptree_bad_path& ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint (eLogWarning, "Profiling: Missing section ", PEER_PROFILE_SECTION_PARTICIPATION, " in profile for ", ident); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
try |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
// read usage
|
|
|
|
|
|
|
|
auto usage = pt.get_child (PEER_PROFILE_SECTION_USAGE); |
|
|
|
|
|
|
|
m_NumTimesTaken = usage.get (PEER_PROFILE_USAGE_TAKEN, 0); |
|
|
|
|
|
|
|
m_NumTimesRejected = usage.get (PEER_PROFILE_USAGE_REJECTED, 0); |
|
|
|
|
|
|
|
m_HasConnected = usage.get (PEER_PROFILE_USAGE_CONNECTED, false); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
catch (boost::property_tree::ptree_bad_path& ex) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
LogPrint (eLogWarning, "Profiling: Missing section ", PEER_PROFILE_SECTION_USAGE, " in profile for ", ident); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
*this = RouterProfile (); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
catch (std::exception& ex) |
|
|
|
try { |
|
|
|
{ |
|
|
|
/* "lasttime" hash */ |
|
|
|
LogPrint (eLogError, "Profiling: Can't read profile ", ident, " :", ex.what ()); |
|
|
|
m_LastUpdateTime = pt.get<int>(PEER_PROFILE_LAST_UPDATE_TIME, 0); |
|
|
|
|
|
|
|
m_LastDeclineTime = pt.get<int>(PEER_PROFILE_LAST_DECLINE_TIME, 0); |
|
|
|
|
|
|
|
m_LastUnreachableTime = pt.get<int>(PEER_PROFILE_LAST_UNREACHABLE_TIME, 0); |
|
|
|
|
|
|
|
/* "tunnels" hash */ |
|
|
|
|
|
|
|
m_NumTunnelsAgreed = pt.get<int>(PEER_PROFILE_PARTICIPATION_AGREED, 0); |
|
|
|
|
|
|
|
m_NumTunnelsDeclined = pt.get<int>(PEER_PROFILE_PARTICIPATION_DECLINED, 0); |
|
|
|
|
|
|
|
m_NumTunnelsNonReplied = pt.get<int>(PEER_PROFILE_PARTICIPATION_NON_REPLIED, 0); |
|
|
|
|
|
|
|
/* "usage" hash */ |
|
|
|
|
|
|
|
m_NumTimesTaken = pt.get<int>(PEER_PROFILE_USAGE_TAKEN, 0); |
|
|
|
|
|
|
|
m_NumTimesRejected = pt.get<int>(PEER_PROFILE_USAGE_REJECTED, 0); |
|
|
|
|
|
|
|
m_HasConnected = pt.get<bool>(PEER_PROFILE_USAGE_CONNECTED, false); |
|
|
|
|
|
|
|
} catch (boost::property_tree::ptree_bad_path& ex) { |
|
|
|
|
|
|
|
LogPrint (eLogError, "Profiling: Can't read profile data: ", ex.what()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return peerid; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void RouterProfile::TunnelBuildResponse (uint8_t ret) |
|
|
|
void RouterProfile::TunnelBuildResponse (uint8_t ret) |
|
|
|
{ |
|
|
|
{ |
|
|
|
UpdateTime (); |
|
|
|
if (ret > 0) { |
|
|
|
if (ret > 0) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
m_NumTunnelsDeclined++; |
|
|
|
m_NumTunnelsDeclined++; |
|
|
|
m_LastDeclineTime = i2p::util::GetSecondsSinceEpoch (); |
|
|
|
m_LastDeclineTime = GetTime (); |
|
|
|
} |
|
|
|
} else { |
|
|
|
else |
|
|
|
m_NumTunnelsAgreed++; |
|
|
|
{ |
|
|
|
|
|
|
|
m_NumTunnelsAgreed++; |
|
|
|
|
|
|
|
m_LastDeclineTime = 0; |
|
|
|
m_LastDeclineTime = 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
m_LastUpdateTime = GetTime (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void RouterProfile::TunnelNonReplied () |
|
|
|
void RouterProfile::TunnelNonReplied () |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_NumTunnelsNonReplied++; |
|
|
|
m_NumTunnelsNonReplied++; |
|
|
|
UpdateTime (); |
|
|
|
|
|
|
|
if (m_NumTunnelsNonReplied > 2*m_NumTunnelsAgreed && m_NumTunnelsNonReplied > 3) |
|
|
|
if (m_NumTunnelsNonReplied > 2*m_NumTunnelsAgreed && m_NumTunnelsNonReplied > 3) |
|
|
|
{ |
|
|
|
m_LastDeclineTime = GetTime (); |
|
|
|
m_LastDeclineTime = i2p::util::GetSecondsSinceEpoch (); |
|
|
|
m_LastUpdateTime = GetTime (); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void RouterProfile::Unreachable () |
|
|
|
void RouterProfile::Unreachable () |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_LastUnreachableTime = i2p::util::GetSecondsSinceEpoch (); |
|
|
|
m_LastUnreachableTime = GetTime (); |
|
|
|
UpdateTime (); |
|
|
|
m_LastUpdateTime = GetTime (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void RouterProfile::Connected () |
|
|
|
void RouterProfile::Connected () |
|
|
|
{ |
|
|
|
{ |
|
|
|
m_HasConnected = true; |
|
|
|
m_HasConnected = true; |
|
|
|
UpdateTime (); |
|
|
|
m_LastUpdateTime = GetTime (); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bool RouterProfile::IsLowPartcipationRate () const |
|
|
|
bool RouterProfile::IsLowPartcipationRate () const |
|
|
@ -241,84 +201,101 @@ namespace data |
|
|
|
if (it != g_Profiles.end ()) |
|
|
|
if (it != g_Profiles.end ()) |
|
|
|
return it->second; |
|
|
|
return it->second; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
LogPrint(eLogDebug, "Profiling: creating new profile for ", identHash.ToBase64()); |
|
|
|
auto profile = std::make_shared<RouterProfile> (); |
|
|
|
auto profile = std::make_shared<RouterProfile> (); |
|
|
|
profile->Load (identHash); // if possible
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(g_ProfilesMutex); |
|
|
|
std::unique_lock<std::mutex> l(g_ProfilesMutex); |
|
|
|
g_Profiles.emplace (identHash, profile); |
|
|
|
g_Profiles.emplace (identHash, profile); |
|
|
|
return profile; |
|
|
|
return profile; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void InitProfilesStorage () |
|
|
|
void LoadProfilesDB () { |
|
|
|
{ |
|
|
|
unsigned int loaded = 0, linenum = 0; |
|
|
|
g_ProfilesStorage.SetPlace(i2p::fs::GetDataDir()); |
|
|
|
static std::unordered_map<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > new_db; |
|
|
|
g_ProfilesStorage.Init(i2p::data::GetBase64SubstitutionTable(), 64); |
|
|
|
IdentHash identHash; |
|
|
|
} |
|
|
|
std::string oldDBDir = i2p::fs::DataDirPath("peerProfiles"); |
|
|
|
|
|
|
|
auto DBPath = i2p::fs::DataDirPath(PEER_PROFILES_DB_FILENAME); |
|
|
|
|
|
|
|
if (i2p::fs::Exists(oldDBDir)) { |
|
|
|
|
|
|
|
std::string oldDBBak = oldDBDir + ".bak"; |
|
|
|
|
|
|
|
LogPrint(eLogInfo, "Profiling: old peerProfiles/ directory still exists, you may safely remove it"); |
|
|
|
|
|
|
|
std::rename(oldDBDir.c_str(), oldDBBak.c_str()); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!i2p::fs::Exists(DBPath)) |
|
|
|
|
|
|
|
return; /* no database yet */ |
|
|
|
|
|
|
|
|
|
|
|
void PersistProfiles () |
|
|
|
std::ifstream in (DBPath); |
|
|
|
{ |
|
|
|
if (!in.is_open()) { |
|
|
|
auto ts = GetTime (); |
|
|
|
LogPrint (eLogError, "Profiling: can't open profiles database ", DBPath); |
|
|
|
std::list<std::pair<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > > tmp; |
|
|
|
return; |
|
|
|
{ |
|
|
|
} |
|
|
|
std::unique_lock<std::mutex> l(g_ProfilesMutex); |
|
|
|
|
|
|
|
for (auto it = g_Profiles.begin (); it != g_Profiles.end ();) |
|
|
|
std::string line; |
|
|
|
{ |
|
|
|
while (!(in.eof() || in.fail())) { |
|
|
|
if ((ts - it->second->GetLastUpdateTime ()).total_seconds () > PEER_PROFILE_PERSIST_INTERVAL) |
|
|
|
std::getline(in, line); linenum++; |
|
|
|
{ |
|
|
|
if (line.empty()) continue; |
|
|
|
if (it->second->IsUpdated ()) |
|
|
|
if (line[0] != '{') { |
|
|
|
tmp.push_back (std::make_pair (it->first, it->second)); |
|
|
|
LogPrint(eLogError, "Profiling: ignore profile data at line ", linenum); |
|
|
|
it = g_Profiles.erase (it); |
|
|
|
continue; |
|
|
|
} |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
it++; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
auto profile = std::make_shared<RouterProfile> (); |
|
|
|
|
|
|
|
std::string peerid = profile->Load(line); |
|
|
|
|
|
|
|
if (peerid.empty()) |
|
|
|
|
|
|
|
continue; /* load failed, errors logged */ |
|
|
|
|
|
|
|
identHash.FromBase64(peerid); |
|
|
|
|
|
|
|
new_db.emplace(identHash, profile); |
|
|
|
|
|
|
|
loaded++; |
|
|
|
} |
|
|
|
} |
|
|
|
for (auto& it: tmp) |
|
|
|
LogPrint (eLogInfo, "Profiling: loaded ", loaded, " profiles"); |
|
|
|
if (it.second) it.second->Save (it.first); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void SaveProfiles () |
|
|
|
{ /* replace exiting database with just loaded */ |
|
|
|
{ |
|
|
|
|
|
|
|
std::unordered_map<i2p::data::IdentHash, std::shared_ptr<RouterProfile> > tmp; |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
std::unique_lock<std::mutex> l(g_ProfilesMutex); |
|
|
|
std::unique_lock<std::mutex> l(g_ProfilesMutex); |
|
|
|
tmp = g_Profiles; |
|
|
|
|
|
|
|
g_Profiles.clear (); |
|
|
|
g_Profiles.clear (); |
|
|
|
|
|
|
|
g_Profiles = new_db; |
|
|
|
} |
|
|
|
} |
|
|
|
auto ts = GetTime (); |
|
|
|
return; |
|
|
|
for (auto& it: tmp) |
|
|
|
|
|
|
|
if (it.second->IsUseful() && it.second->IsUpdated () && (ts - it.second->GetLastUpdateTime ()).total_seconds () < PEER_PROFILE_EXPIRATION_TIMEOUT*3600) |
|
|
|
|
|
|
|
it.second->Save (it.first); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void DeleteObsoleteProfiles () |
|
|
|
void PruneExpiredProfiles () { |
|
|
|
{ |
|
|
|
unsigned int pruned = 0; |
|
|
|
{ |
|
|
|
auto ts = GetTime (); |
|
|
|
auto ts = GetTime (); |
|
|
|
std::unique_lock<std::mutex> l(g_ProfilesMutex); |
|
|
|
std::unique_lock<std::mutex> l(g_ProfilesMutex); |
|
|
|
for (auto it = g_Profiles.begin (); it != g_Profiles.end (); ) { |
|
|
|
for (auto it = g_Profiles.begin (); it != g_Profiles.end ();) |
|
|
|
if ((ts - it->second->GetLastUpdateTime ()) >= PEER_PROFILE_EXPIRATION_TIMEOUT * 3600) { |
|
|
|
{ |
|
|
|
it = g_Profiles.erase (it); |
|
|
|
if ((ts - it->second->GetLastUpdateTime ()).total_seconds () >= PEER_PROFILE_EXPIRATION_TIMEOUT*3600) |
|
|
|
pruned++; |
|
|
|
it = g_Profiles.erase (it); |
|
|
|
} else { |
|
|
|
else |
|
|
|
it++; |
|
|
|
it++; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
LogPrint(eLogInfo, "Profiling: pruned ", pruned, " expired peer profiles, ", g_Profiles.size(), " remains"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
struct stat st; |
|
|
|
void SaveProfilesDB () { |
|
|
|
std::time_t now = std::time(nullptr); |
|
|
|
unsigned int saved = 0; |
|
|
|
|
|
|
|
auto DBPath = i2p::fs::DataDirPath(PEER_PROFILES_DB_FILENAME); |
|
|
|
std::vector<std::string> files; |
|
|
|
auto DBPathNew = DBPath + ".new"; |
|
|
|
g_ProfilesStorage.Traverse(files); |
|
|
|
std::ofstream out (DBPathNew); |
|
|
|
for (const auto& path: files) { |
|
|
|
std::unique_lock<std::mutex> l(g_ProfilesMutex); |
|
|
|
if (stat(path.c_str(), &st) != 0) { |
|
|
|
if (!out.is_open()) { |
|
|
|
LogPrint(eLogWarning, "Profiling: Can't stat(): ", path); |
|
|
|
LogPrint(eLogError, "Profiling: can't open database file ", DBPathNew); |
|
|
|
continue; |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (now - st.st_mtime >= PEER_PROFILE_EXPIRATION_TIMEOUT*3600) { |
|
|
|
|
|
|
|
LogPrint(eLogDebug, "Profiling: Removing expired peer profile: ", path); |
|
|
|
|
|
|
|
i2p::fs::Remove(path); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
auto ts = GetTime (); |
|
|
|
|
|
|
|
/* save "old enough" profiles */ |
|
|
|
|
|
|
|
for (auto& it : g_Profiles) { |
|
|
|
|
|
|
|
if (it.second->IsUseful() && (ts - it.second->GetLastUpdateTime ()) < PEER_PROFILE_PERSIST_INTERVAL) |
|
|
|
|
|
|
|
continue; /* too new */ |
|
|
|
|
|
|
|
out << it.second->Dump(it.first.ToBase64()); |
|
|
|
|
|
|
|
saved++; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
out.flush(); |
|
|
|
|
|
|
|
out.close(); |
|
|
|
|
|
|
|
LogPrint(eLogDebug, "Profiling: db path is", DBPath); |
|
|
|
|
|
|
|
std::rename(DBPathNew.c_str(), DBPath.c_str()); |
|
|
|
|
|
|
|
LogPrint(eLogInfo, "Profiling: saved ", saved, " peer profiles"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ClearProfilesDB () { |
|
|
|
|
|
|
|
g_Profiles.clear(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|