i2pd/AddressBook.h

107 lines
3.3 KiB
C
Raw Normal View History

2014-03-13 18:22:32 +00:00
#ifndef ADDRESS_BOOK_H__
#define ADDRESS_BOOK_H__
#include <string.h>
#include <string>
#include <map>
#include <vector>
2014-12-20 03:03:34 +00:00
#include <iostream>
2014-12-21 14:33:02 +00:00
#include <mutex>
2015-11-03 14:15:49 +00:00
#include <memory>
#include <boost/asio.hpp>
2015-11-03 14:15:49 +00:00
#include "Base.h"
2014-03-13 18:22:32 +00:00
#include "Identity.h"
#include "Log.h"
namespace i2p
{
2014-10-24 19:22:36 +00:00
namespace client
2014-03-13 18:22:32 +00:00
{
2014-12-21 14:33:02 +00:00
const char DEFAULT_SUBSCRIPTION_ADDRESS[] = "http://udhdrtrcetjm5sxzskjyr5ztpeszydbh4dpl3pl4utgqqw2v4jna.b32.i2p/hosts.txt";
const int INITIAL_SUBSCRIPTION_UPDATE_TIMEOUT = 3; // in minutes
const int INITIAL_SUBSCRIPTION_RETRY_TIMEOUT = 1; // in minutes
const int CONTINIOUS_SUBSCRIPTION_UPDATE_TIMEOUT = 720; // in minutes (12 hours)
const int CONTINIOUS_SUBSCRIPTION_RETRY_TIMEOUT = 5; // in minutes
const int SUBSCRIPTION_REQUEST_TIMEOUT = 60; //in second
2015-01-07 20:50:12 +00:00
inline std::string GetB32Address(const i2p::data::IdentHash& ident) { return ident.ToBase32().append(".b32.i2p"); }
2014-11-26 21:19:36 +00:00
class AddressBookStorage // interface for storage
{
public:
virtual ~AddressBookStorage () {};
2015-11-03 14:15:49 +00:00
virtual std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const i2p::data::IdentHash& ident) const = 0;
virtual void AddAddress (std::shared_ptr<const i2p::data::IdentityEx> address) = 0;
2014-11-26 21:19:36 +00:00
virtual void RemoveAddress (const i2p::data::IdentHash& ident) = 0;
2014-11-27 21:26:55 +00:00
virtual bool Init () = 0;
2014-11-28 14:40:27 +00: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-26 21:19:36 +00:00
};
2014-12-20 03:03:34 +00:00
class AddressBookSubscription;
2014-03-13 18:22:32 +00:00
class AddressBook
{
public:
2014-04-01 19:18:14 +00:00
AddressBook ();
2014-11-26 21:19:36 +00:00
~AddressBook ();
2015-03-30 14:21:52 +00:00
void Start ();
void Stop ();
2014-10-24 19:22:36 +00:00
bool GetIdentHash (const std::string& address, i2p::data::IdentHash& ident);
2015-11-03 14:15:49 +00:00
std::shared_ptr<const i2p::data::IdentityEx> GetAddress (const std::string& address);
2014-10-24 19:22:36 +00:00
const i2p::data::IdentHash * FindAddress (const std::string& address);
2014-09-23 19:38:56 +00:00
void InsertAddress (const std::string& address, const std::string& base64); // for jump service
2015-11-03 14:15:49 +00:00
void InsertAddress (std::shared_ptr<const i2p::data::IdentityEx> address);
2014-12-20 03:03:34 +00:00
2014-12-23 02:20:39 +00:00
void LoadHostsFromStream (std::istream& f);
void DownloadComplete (bool success);
//This method returns the ".b32.i2p" address
2015-01-07 20:50:12 +00:00
std::string ToAddress(const i2p::data::IdentHash& ident) { return GetB32Address(ident); }
2015-11-03 14:15:49 +00:00
std::string ToAddress(std::shared_ptr<const i2p::data::IdentityEx> ident) { return ToAddress(ident->GetIdentHash ()); }
2014-03-13 18:22:32 +00:00
private:
2014-11-26 21:51:36 +00:00
2015-03-30 14:21:52 +00:00
void StartSubscriptions ();
void StopSubscriptions ();
2014-04-01 19:18:14 +00:00
void LoadHosts ();
2014-12-22 20:06:54 +00:00
void LoadSubscriptions ();
2014-03-13 18:22:32 +00:00
void HandleSubscriptionsUpdateTimer (const boost::system::error_code& ecode);
2014-12-20 03:03:34 +00:00
private:
2014-12-21 14:33:02 +00:00
std::mutex m_AddressBookMutex;
2014-10-24 19:22:36 +00:00
std::map<std::string, i2p::data::IdentHash> m_Addresses;
2014-11-26 21:19:36 +00:00
AddressBookStorage * m_Storage;
2014-12-21 14:33:02 +00:00
volatile bool m_IsLoaded, m_IsDownloading;
std::vector<AddressBookSubscription *> m_Subscriptions;
std::unique_ptr<AddressBookSubscription> m_DefaultSubscription; // in case if we don't know any addresses yet
boost::asio::deadline_timer * m_SubscriptionsUpdateTimer;
2014-03-13 18:22:32 +00:00
};
class AddressBookSubscription
{
public:
AddressBookSubscription (AddressBook& book, const std::string& link);
void CheckSubscription ();
private:
void Request ();
2016-02-18 18:19:31 +00:00
bool ProcessResponse (std::stringstream& s, bool isGzip = false);
private:
AddressBook& m_Book;
2014-12-22 20:06:54 +00:00
std::string m_Link, m_Etag, m_LastModified;
};
2014-03-13 18:22:32 +00:00
}
}
#endif