mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-22 08:14:15 +00:00
load from addressbook first
This commit is contained in:
parent
092b445e36
commit
3e3cfa3d68
@ -21,24 +21,18 @@ namespace client
|
|||||||
|
|
||||||
AddressBookFilesystemStorage ();
|
AddressBookFilesystemStorage ();
|
||||||
bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const;
|
bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const;
|
||||||
const i2p::data::IdentHash * FindAddress (const std::string& name) const;
|
|
||||||
void AddAddress (const i2p::data::IdentityEx& address);
|
void AddAddress (const i2p::data::IdentityEx& address);
|
||||||
void AddAddress (std::string& name, const i2p::data::IdentHash& ident);
|
|
||||||
void RemoveAddress (const i2p::data::IdentHash& ident);
|
void RemoveAddress (const i2p::data::IdentHash& ident);
|
||||||
|
|
||||||
int Load ();
|
int Load (std::map<std::string, i2p::data::IdentHash>& addresses);
|
||||||
int Save ();
|
int Save (const std::map<std::string, i2p::data::IdentHash>& addresses);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
boost::filesystem::path GetPath () const { return i2p::util::filesystem::GetDefaultDataDir() / "addressbook"; };
|
boost::filesystem::path GetPath () const { return i2p::util::filesystem::GetDefaultDataDir() / "addressbook"; };
|
||||||
|
|
||||||
private:
|
|
||||||
|
|
||||||
std::map<std::string, i2p::data::IdentHash> m_Addresses;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
AddressBookFilesystemStorage::AddressBookFilesystemStorage ()
|
AddressBookFilesystemStorage::AddressBookFilesystemStorage ()
|
||||||
{
|
{
|
||||||
auto path = GetPath ();
|
auto path = GetPath ();
|
||||||
@ -90,11 +84,6 @@ namespace client
|
|||||||
LogPrint (eLogError, "Can't open file ", filename);
|
LogPrint (eLogError, "Can't open file ", filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
void AddressBookFilesystemStorage::AddAddress (std::string& name, const i2p::data::IdentHash& ident)
|
|
||||||
{
|
|
||||||
m_Addresses[name] = ident;
|
|
||||||
}
|
|
||||||
|
|
||||||
void AddressBookFilesystemStorage::RemoveAddress (const i2p::data::IdentHash& ident)
|
void AddressBookFilesystemStorage::RemoveAddress (const i2p::data::IdentHash& ident)
|
||||||
{
|
{
|
||||||
auto filename = GetPath () / (ident.ToBase32() + ".b32");
|
auto filename = GetPath () / (ident.ToBase32() + ".b32");
|
||||||
@ -102,14 +91,14 @@ namespace client
|
|||||||
boost::filesystem::remove (filename);
|
boost::filesystem::remove (filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
int AddressBookFilesystemStorage::Load ()
|
int AddressBookFilesystemStorage::Load (std::map<std::string, i2p::data::IdentHash>& addresses)
|
||||||
{
|
{
|
||||||
int num = 0;
|
int num = 0;
|
||||||
auto filename = GetPath () / "addresses.csv";
|
auto filename = GetPath () / "addresses.csv";
|
||||||
std::ifstream f (filename.c_str (), std::ofstream::in); // in text mode
|
std::ifstream f (filename.c_str (), std::ofstream::in); // in text mode
|
||||||
if (f.is_open ())
|
if (f.is_open ())
|
||||||
{
|
{
|
||||||
m_Addresses.clear ();
|
addresses.clear ();
|
||||||
while (!f.eof ())
|
while (!f.eof ())
|
||||||
{
|
{
|
||||||
std::string s;
|
std::string s;
|
||||||
@ -125,7 +114,7 @@ namespace client
|
|||||||
|
|
||||||
i2p::data::IdentHash ident;
|
i2p::data::IdentHash ident;
|
||||||
ident.FromBase32 (addr);
|
ident.FromBase32 (addr);
|
||||||
m_Addresses[name] = ident;
|
addresses[name] = ident;
|
||||||
num++;
|
num++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -136,14 +125,14 @@ namespace client
|
|||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
int AddressBookFilesystemStorage::Save ()
|
int AddressBookFilesystemStorage::Save (const std::map<std::string, i2p::data::IdentHash>& addresses)
|
||||||
{
|
{
|
||||||
int num = 0;
|
int num = 0;
|
||||||
auto filename = GetPath () / "addresses.csv";
|
auto filename = GetPath () / "addresses.csv";
|
||||||
std::ofstream f (filename.c_str (), std::ofstream::out); // in text mode
|
std::ofstream f (filename.c_str (), std::ofstream::out); // in text mode
|
||||||
if (f.is_open ())
|
if (f.is_open ())
|
||||||
{
|
{
|
||||||
for (auto it: m_Addresses)
|
for (auto it: addresses)
|
||||||
{
|
{
|
||||||
f << it.first << "," << it.second.ToBase32 () << std::endl;
|
f << it.first << "," << it.second.ToBase32 () << std::endl;
|
||||||
num++;
|
num++;
|
||||||
@ -155,14 +144,6 @@ namespace client
|
|||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
|
||||||
const i2p::data::IdentHash * AddressBookFilesystemStorage::FindAddress (const std::string& name) const
|
|
||||||
{
|
|
||||||
auto it = m_Addresses.find (name);
|
|
||||||
if (it != m_Addresses.end ())
|
|
||||||
return &it->second;
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
//---------------------------------------------------------------------
|
//---------------------------------------------------------------------
|
||||||
AddressBook::AddressBook (): m_IsLoaded (false), m_IsDowloading (false)
|
AddressBook::AddressBook (): m_IsLoaded (false), m_IsDowloading (false)
|
||||||
{
|
{
|
||||||
@ -267,6 +248,16 @@ namespace client
|
|||||||
|
|
||||||
void AddressBook::LoadHosts ()
|
void AddressBook::LoadHosts ()
|
||||||
{
|
{
|
||||||
|
if (!m_Storage)
|
||||||
|
m_Storage = CreateStorage ();
|
||||||
|
int numAddresses = m_Storage->Load (m_Addresses);
|
||||||
|
if (numAddresses > 0)
|
||||||
|
{
|
||||||
|
m_IsLoaded = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// otherwise try hosts.txt
|
||||||
std::ifstream f (i2p::util::filesystem::GetFullPath ("hosts.txt").c_str (), std::ofstream::in); // in text mode
|
std::ifstream f (i2p::util::filesystem::GetFullPath ("hosts.txt").c_str (), std::ofstream::in); // in text mode
|
||||||
if (!f.is_open ())
|
if (!f.is_open ())
|
||||||
{
|
{
|
||||||
@ -280,9 +271,6 @@ namespace client
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!m_Storage)
|
|
||||||
m_Storage = CreateStorage ();
|
|
||||||
int numAddresses = 0;
|
|
||||||
std::string s;
|
std::string s;
|
||||||
while (!f.eof ())
|
while (!f.eof ())
|
||||||
{
|
{
|
||||||
@ -302,12 +290,11 @@ namespace client
|
|||||||
ident.FromBase64(addr);
|
ident.FromBase64(addr);
|
||||||
m_Addresses[name] = ident.GetIdentHash ();
|
m_Addresses[name] = ident.GetIdentHash ();
|
||||||
m_Storage->AddAddress (ident);
|
m_Storage->AddAddress (ident);
|
||||||
m_Storage->AddAddress (name, ident.GetIdentHash ());
|
|
||||||
numAddresses++;
|
numAddresses++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LogPrint (numAddresses, " addresses loaded");
|
LogPrint (numAddresses, " addresses loaded");
|
||||||
m_Storage->Save ();
|
m_Storage->Save (m_Addresses);
|
||||||
m_IsLoaded = true;
|
m_IsLoaded = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,14 +18,12 @@ namespace client
|
|||||||
public:
|
public:
|
||||||
|
|
||||||
virtual ~AddressBookStorage () {};
|
virtual ~AddressBookStorage () {};
|
||||||
virtual bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const = 0;
|
virtual bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const = 0;
|
||||||
virtual const i2p::data::IdentHash * FindAddress (const std::string& name) const = 0;
|
|
||||||
virtual void AddAddress (std::string& name, const i2p::data::IdentHash& ident) = 0;
|
|
||||||
virtual void AddAddress (const i2p::data::IdentityEx& address) = 0;
|
virtual void AddAddress (const i2p::data::IdentityEx& address) = 0;
|
||||||
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
|
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
|
||||||
|
|
||||||
virtual int Load () = 0;
|
virtual int Load (std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
|
||||||
virtual int Save () = 0;
|
virtual int Save (const std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
class AddressBook
|
class AddressBook
|
||||||
|
Loading…
x
Reference in New Issue
Block a user