Browse Source

process addressbook request response

pull/118/head
orignal 10 years ago
parent
commit
f4f6e74ea2
  1. 38
      AddressBook.cpp
  2. 8
      AddressBook.h

38
AddressBook.cpp

@ -252,11 +252,11 @@ namespace client
f_save.close(); f_save.close();
} }
else else
LogPrint("Can't write hosts.txt"); LogPrint (eLogError, "Can't write hosts.txt");
m_IsLoaded = false; m_IsLoaded = false;
} }
else else
LogPrint ("Failed to download hosts.txt"); LogPrint (eLogError, "Failed to download hosts.txt");
m_IsDowloading = false; m_IsDowloading = false;
return; return;
@ -266,8 +266,7 @@ namespace client
{ {
if (!m_Storage) if (!m_Storage)
m_Storage = CreateStorage (); m_Storage = CreateStorage ();
int numAddresses = m_Storage->Load (m_Addresses); if (m_Storage->Load (m_Addresses) > 0)
if (numAddresses > 0)
{ {
m_IsLoaded = true; m_IsLoaded = true;
return; return;
@ -277,7 +276,7 @@ namespace client
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 ())
{ {
LogPrint ("hosts.txt not found. Try to load..."); LogPrint (eLogInfo, "hosts.txt not found. Try to load...");
if (!m_IsDowloading) if (!m_IsDowloading)
{ {
m_IsDowloading = true; m_IsDowloading = true;
@ -287,6 +286,14 @@ namespace client
return; return;
} }
LoadHostsFromStream (f);
m_Storage->Save (m_Addresses);
m_IsLoaded = true;
}
void AddressBook::LoadHostsFromStream (std::istream& f)
{
int numAddresses = 0;
std::string s; std::string s;
while (!f.eof ()) while (!f.eof ())
{ {
@ -309,9 +316,7 @@ namespace client
numAddresses++; numAddresses++;
} }
} }
LogPrint (numAddresses, " addresses loaded"); LogPrint (eLogInfo, numAddresses, " addresses loaded");
m_Storage->Save (m_Addresses);
m_IsLoaded = true;
} }
AddressBookSubscription::AddressBookSubscription (AddressBook& book, const std::string& link): AddressBookSubscription::AddressBookSubscription (AddressBook& book, const std::string& link):
@ -369,6 +374,23 @@ namespace client
if (!end) if (!end)
end = !stream->IsOpen (); end = !stream->IsOpen ();
} }
// parse response
std::string version;
response >> version; // HTTP version
int status = 0;
response >> status; // status
if (status == 200) // OK
{
std::string header, statusMessage;
std::getline (response, statusMessage);
// read until new line meaning end of header
while (!response.eof () && header != "\r")
std::getline (response, header);
if (!response.eof ())
m_Book.LoadHostsFromStream (response);
}
else
LogPrint (eLogWarning, "Adressbook HTTP response ", status);
} }
else else
LogPrint (eLogError, "Address ", u.host_, " not found"); LogPrint (eLogError, "Address ", u.host_, " not found");

8
AddressBook.h

@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include <string> #include <string>
#include <map> #include <map>
#include <iostream>
#include "base64.h" #include "base64.h"
#include "util.h" #include "util.h"
#include "Identity.h" #include "Identity.h"
@ -26,6 +27,7 @@ namespace client
virtual int Save (const std::map<std::string, i2p::data::IdentHash>& addresses) = 0; virtual int Save (const std::map<std::string, i2p::data::IdentHash>& addresses) = 0;
}; };
class AddressBookSubscription;
class AddressBook class AddressBook
{ {
public: public:
@ -38,15 +40,17 @@ namespace client
void InsertAddress (const std::string& address, const std::string& base64); // for jump service void InsertAddress (const std::string& address, const std::string& base64); // for jump service
void InsertAddress (const i2p::data::IdentityEx& address); void InsertAddress (const i2p::data::IdentityEx& address);
void LoadHostsFromStream (std::istream& f);
private: private:
AddressBookStorage * CreateStorage (); AddressBookStorage * CreateStorage ();
private:
void LoadHosts (); void LoadHosts ();
void LoadHostsFromI2P (); void LoadHostsFromI2P ();
private:
std::map<std::string, i2p::data::IdentHash> m_Addresses; std::map<std::string, i2p::data::IdentHash> m_Addresses;
AddressBookStorage * m_Storage; AddressBookStorage * m_Storage;
bool m_IsLoaded, m_IsDowloading; bool m_IsLoaded, m_IsDowloading;

Loading…
Cancel
Save