2014-03-13 22:22:32 +04:00
|
|
|
#ifndef ADDRESS_BOOK_H__
|
|
|
|
#define ADDRESS_BOOK_H__
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
2014-12-22 23:06:54 +03:00
|
|
|
#include <list>
|
2014-12-20 06:03:34 +03:00
|
|
|
#include <iostream>
|
2014-12-21 17:33:02 +03:00
|
|
|
#include <mutex>
|
2014-03-13 22:22:32 +04:00
|
|
|
#include "base64.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "Identity.h"
|
|
|
|
#include "Log.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
2014-10-24 23:22:36 +04:00
|
|
|
namespace client
|
2014-03-13 22:22:32 +04:00
|
|
|
{
|
2014-12-21 17:33:02 +03:00
|
|
|
const char DEFAULT_SUBSCRIPTION_ADDRESS[] = "http://udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p/hosts.txt";
|
2014-12-22 23:06:54 +03:00
|
|
|
// TODO: move http fields to common http code
|
|
|
|
const char HTTP_FIELD_ETAG[] = "ETag";
|
|
|
|
const char HTTP_FIELD_IF_MODIFIED_SINCE[] = "If-Modified-Since";
|
|
|
|
const char HTTP_FIELD_LAST_MODIFIED[] = "Last-Modified";
|
2014-12-21 17:33:02 +03:00
|
|
|
|
2014-11-27 00:19:36 +03:00
|
|
|
class AddressBookStorage // interface for storage
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
virtual ~AddressBookStorage () {};
|
2014-11-28 17:40:27 +03:00
|
|
|
virtual bool GetAddress (const i2p::data::IdentHash& ident, i2p::data::IdentityEx& address) const = 0;
|
2014-11-27 00:19:36 +03:00
|
|
|
virtual void AddAddress (const i2p::data::IdentityEx& address) = 0;
|
|
|
|
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
|
2014-11-28 00:26:55 +03:00
|
|
|
|
2014-11-28 17:40:27 +03:00
|
|
|
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;
|
2014-11-27 00:19:36 +03:00
|
|
|
};
|
|
|
|
|
2014-12-20 06:03:34 +03:00
|
|
|
class AddressBookSubscription;
|
2014-03-13 22:22:32 +04:00
|
|
|
class AddressBook
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2014-04-01 23:18:14 +04:00
|
|
|
AddressBook ();
|
2014-11-27 00:19:36 +03:00
|
|
|
~AddressBook ();
|
2014-10-24 23:22:36 +04:00
|
|
|
bool GetIdentHash (const std::string& address, i2p::data::IdentHash& ident);
|
2014-11-27 00:19:36 +03:00
|
|
|
bool GetAddress (const std::string& address, i2p::data::IdentityEx& identity);
|
2014-10-24 23:22:36 +04:00
|
|
|
const i2p::data::IdentHash * FindAddress (const std::string& address);
|
2014-09-23 23:38:56 +04:00
|
|
|
void InsertAddress (const std::string& address, const std::string& base64); // for jump service
|
2014-11-27 00:51:36 +03:00
|
|
|
void InsertAddress (const i2p::data::IdentityEx& address);
|
2014-12-20 06:03:34 +03:00
|
|
|
|
|
|
|
void LoadHostsFromStream (std::istream& f);
|
2014-12-21 17:33:02 +03:00
|
|
|
void SetIsDownloading (bool isDownloading) { m_IsDownloading = isDownloading; };
|
2014-12-20 06:03:34 +03:00
|
|
|
|
2014-03-13 22:22:32 +04:00
|
|
|
private:
|
2014-11-27 00:51:36 +03:00
|
|
|
|
|
|
|
AddressBookStorage * CreateStorage ();
|
2014-04-01 23:18:14 +04:00
|
|
|
void LoadHosts ();
|
2014-12-22 23:06:54 +03:00
|
|
|
void LoadSubscriptions ();
|
2014-03-13 22:22:32 +04:00
|
|
|
|
2014-12-20 06:03:34 +03:00
|
|
|
private:
|
2014-12-21 17:33:02 +03:00
|
|
|
|
|
|
|
std::mutex m_AddressBookMutex;
|
2014-10-24 23:22:36 +04:00
|
|
|
std::map<std::string, i2p::data::IdentHash> m_Addresses;
|
2014-11-27 00:19:36 +03:00
|
|
|
AddressBookStorage * m_Storage;
|
2014-12-21 17:33:02 +03:00
|
|
|
volatile bool m_IsLoaded, m_IsDownloading;
|
2014-12-22 23:06:54 +03:00
|
|
|
std::list<AddressBookSubscription *> m_Subscriptions;
|
2014-12-21 17:33:02 +03:00
|
|
|
AddressBookSubscription * m_DefaultSubscription; // in case if we don't know any addresses yet
|
2014-03-13 22:22:32 +04:00
|
|
|
};
|
2014-12-19 22:40:02 +03:00
|
|
|
|
|
|
|
class AddressBookSubscription
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
AddressBookSubscription (AddressBook& book, const std::string& link);
|
|
|
|
void CheckSubscription ();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void Request ();
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
AddressBook& m_Book;
|
2014-12-22 23:06:54 +03:00
|
|
|
std::string m_Link, m_Etag, m_LastModified;
|
2014-12-19 22:40:02 +03:00
|
|
|
};
|
2014-03-13 22:22:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|