2020-05-22 16:18:41 +03:00
|
|
|
/*
|
2024-01-15 01:16:31 +03:00
|
|
|
* Copyright (c) 2013-2024, The PurpleI2P Project
|
2020-05-22 16:18:41 +03:00
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2015-04-09 19:45:00 +03:00
|
|
|
#ifndef NETDB_REQUESTS_H__
|
|
|
|
#define NETDB_REQUESTS_H__
|
|
|
|
|
2024-01-15 01:16:31 +03:00
|
|
|
#include <inttypes.h>
|
2015-04-09 19:45:00 +03:00
|
|
|
#include <memory>
|
|
|
|
#include <set>
|
2024-01-15 01:16:31 +03:00
|
|
|
#include <unordered_map>
|
2015-04-09 19:45:00 +03:00
|
|
|
#include "Identity.h"
|
|
|
|
#include "RouterInfo.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
|
|
|
{
|
2024-01-15 01:16:31 +03:00
|
|
|
const size_t MAX_NUM_REQUEST_ATTEMPTS = 7;
|
2024-04-24 16:44:24 +03:00
|
|
|
const uint64_t MANAGE_REQUESTS_INTERVAL = 1; // in seconds
|
2024-01-15 01:16:31 +03:00
|
|
|
const uint64_t MIN_REQUEST_TIME = 5; // in seconds
|
2024-04-24 16:44:24 +03:00
|
|
|
const uint64_t MAX_REQUEST_TIME = MAX_NUM_REQUEST_ATTEMPTS * (MIN_REQUEST_TIME + MANAGE_REQUESTS_INTERVAL);
|
2024-01-15 01:16:31 +03:00
|
|
|
|
2015-04-09 19:45:00 +03:00
|
|
|
class RequestedDestination
|
2018-01-06 06:48:51 +03:00
|
|
|
{
|
2015-04-09 19:45:00 +03:00
|
|
|
public:
|
|
|
|
|
|
|
|
typedef std::function<void (std::shared_ptr<RouterInfo>)> RequestComplete;
|
|
|
|
|
2024-01-16 03:32:17 +03:00
|
|
|
RequestedDestination (const IdentHash& destination, bool isExploratory = false, bool direct = true);
|
|
|
|
~RequestedDestination ();
|
2015-04-09 19:45:00 +03:00
|
|
|
|
|
|
|
const IdentHash& GetDestination () const { return m_Destination; };
|
|
|
|
int GetNumExcludedPeers () const { return m_ExcludedPeers.size (); };
|
|
|
|
const std::set<IdentHash>& GetExcludedPeers () { return m_ExcludedPeers; };
|
|
|
|
void ClearExcludedPeers ();
|
|
|
|
bool IsExploratory () const { return m_IsExploratory; };
|
2024-01-15 01:16:31 +03:00
|
|
|
bool IsDirect () const { return m_IsDirect; };
|
2024-04-26 01:52:10 +03:00
|
|
|
bool IsActive () const { return m_IsActive; };
|
2015-04-09 19:45:00 +03:00
|
|
|
bool IsExcluded (const IdentHash& ident) const { return m_ExcludedPeers.count (ident); };
|
|
|
|
uint64_t GetCreationTime () const { return m_CreationTime; };
|
2024-01-16 03:32:17 +03:00
|
|
|
uint64_t GetLastRequestTime () const { return m_LastRequestTime; };
|
2015-06-22 22:47:45 +03:00
|
|
|
std::shared_ptr<I2NPMessage> CreateRequestMessage (std::shared_ptr<const RouterInfo>, std::shared_ptr<const i2p::tunnel::InboundTunnel> replyTunnel);
|
2015-06-17 19:25:02 +03:00
|
|
|
std::shared_ptr<I2NPMessage> CreateRequestMessage (const IdentHash& floodfill);
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2015-04-09 19:45:00 +03:00
|
|
|
void SetRequestComplete (const RequestComplete& requestComplete) { m_RequestComplete = requestComplete; };
|
2024-01-15 02:54:21 +03:00
|
|
|
RequestComplete GetRequestComplete () const { return m_RequestComplete; };
|
2015-04-09 19:45:00 +03:00
|
|
|
bool IsRequestComplete () const { return m_RequestComplete != nullptr; };
|
|
|
|
void Success (std::shared_ptr<RouterInfo> r);
|
|
|
|
void Fail ();
|
2018-01-06 06:48:51 +03:00
|
|
|
|
2015-04-09 19:45:00 +03:00
|
|
|
private:
|
|
|
|
|
|
|
|
IdentHash m_Destination;
|
2024-04-26 01:52:10 +03:00
|
|
|
bool m_IsExploratory, m_IsDirect, m_IsActive;
|
2015-04-09 19:45:00 +03:00
|
|
|
std::set<IdentHash> m_ExcludedPeers;
|
2024-01-16 03:32:17 +03:00
|
|
|
uint64_t m_CreationTime, m_LastRequestTime; // in seconds
|
2015-04-09 19:45:00 +03:00
|
|
|
RequestComplete m_RequestComplete;
|
2018-01-06 06:48:51 +03:00
|
|
|
};
|
2015-04-09 19:45:00 +03:00
|
|
|
|
|
|
|
class NetDbRequests
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
void Start ();
|
|
|
|
void Stop ();
|
|
|
|
|
2024-01-15 01:16:31 +03:00
|
|
|
std::shared_ptr<RequestedDestination> CreateRequest (const IdentHash& destination, bool isExploratory,
|
|
|
|
bool direct = false, RequestedDestination::RequestComplete requestComplete = nullptr);
|
2015-04-09 19:45:00 +03:00
|
|
|
void RequestComplete (const IdentHash& ident, std::shared_ptr<RouterInfo> r);
|
|
|
|
std::shared_ptr<RequestedDestination> FindRequest (const IdentHash& ident) const;
|
|
|
|
void ManageRequests ();
|
2024-01-15 01:16:31 +03:00
|
|
|
bool SendNextRequest (std::shared_ptr<RequestedDestination> dest);
|
|
|
|
|
2015-04-09 19:45:00 +03:00
|
|
|
private:
|
|
|
|
|
2016-08-16 05:36:58 +03:00
|
|
|
mutable std::mutex m_RequestedDestinationsMutex;
|
2024-01-15 01:16:31 +03:00
|
|
|
std::unordered_map<IdentHash, std::shared_ptr<RequestedDestination> > m_RequestedDestinations;
|
2015-04-09 19:45:00 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|