load from addressbook first

This commit is contained in:
orignal 2014-11-28 09:40:27 -05:00
parent 092b445e36
commit 3e3cfa3d68
2 changed files with 21 additions and 36 deletions

View File

@ -21,24 +21,18 @@ namespace client
AddressBookFilesystemStorage ();
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 (std::string& name, const i2p::data::IdentHash& ident);
void RemoveAddress (const i2p::data::IdentHash& ident);
int Load ();
int Save ();
int Load (std::map<std::string, i2p::data::IdentHash>& addresses);
int Save (const std::map<std::string, i2p::data::IdentHash>& addresses);
private:
boost::filesystem::path GetPath () const { return i2p::util::filesystem::GetDefaultDataDir() / "addressbook"; };
private:
std::map<std::string, i2p::data::IdentHash> m_Addresses;
};
AddressBookFilesystemStorage::AddressBookFilesystemStorage ()
{
auto path = GetPath ();
@ -90,11 +84,6 @@ namespace client
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)
{
auto filename = GetPath () / (ident.ToBase32() + ".b32");
@ -102,14 +91,14 @@ namespace client
boost::filesystem::remove (filename);
}
int AddressBookFilesystemStorage::Load ()
int AddressBookFilesystemStorage::Load (std::map<std::string, i2p::data::IdentHash>& addresses)
{
int num = 0;
auto filename = GetPath () / "addresses.csv";
std::ifstream f (filename.c_str (), std::ofstream::in); // in text mode
if (f.is_open ())
{
m_Addresses.clear ();
addresses.clear ();
while (!f.eof ())
{
std::string s;
@ -125,7 +114,7 @@ namespace client
i2p::data::IdentHash ident;
ident.FromBase32 (addr);
m_Addresses[name] = ident;
addresses[name] = ident;
num++;
}
}
@ -136,14 +125,14 @@ namespace client
return num;
}
int AddressBookFilesystemStorage::Save ()
int AddressBookFilesystemStorage::Save (const std::map<std::string, i2p::data::IdentHash>& addresses)
{
int num = 0;
auto filename = GetPath () / "addresses.csv";
std::ofstream f (filename.c_str (), std::ofstream::out); // in text mode
if (f.is_open ())
{
for (auto it: m_Addresses)
for (auto it: addresses)
{
f << it.first << "," << it.second.ToBase32 () << std::endl;
num++;
@ -155,14 +144,6 @@ namespace client
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)
{
@ -267,6 +248,16 @@ namespace client
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
if (!f.is_open ())
{
@ -280,9 +271,6 @@ namespace client
return;
}
if (!m_Storage)
m_Storage = CreateStorage ();
int numAddresses = 0;
std::string s;
while (!f.eof ())
{
@ -302,12 +290,11 @@ namespace client
ident.FromBase64(addr);
m_Addresses[name] = ident.GetIdentHash ();
m_Storage->AddAddress (ident);
m_Storage->AddAddress (name, ident.GetIdentHash ());
numAddresses++;
}
}
LogPrint (numAddresses, " addresses loaded");
m_Storage->Save ();
m_Storage->Save (m_Addresses);
m_IsLoaded = true;
}

View File

@ -18,14 +18,12 @@ namespace client
public:
virtual ~AddressBookStorage () {};
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 bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const = 0;
virtual void AddAddress (const i2p::data::IdentityEx& address) = 0;
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
virtual int Load () = 0;
virtual int Save () = 0;
virtual int Load (std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
virtual int Save (const std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
};
class AddressBook