Browse Source

LeaseSet request complete callback

pull/120/head
orignal 10 years ago
parent
commit
6fb5fa1c52
  1. 18
      AddressBook.cpp
  2. 46
      Destination.cpp
  3. 9
      Destination.h

18
AddressBook.cpp

@ -430,12 +430,22 @@ namespace client
i2p::data::IdentHash ident; i2p::data::IdentHash ident;
if (m_Book.GetIdentHash (u.host_, ident)) if (m_Book.GetIdentHash (u.host_, ident))
{ {
std::condition_variable newDataReceived;
std::mutex newDataReceivedMutex;
const i2p::data::LeaseSet * leaseSet = i2p::data::netdb.FindLeaseSet (ident); const i2p::data::LeaseSet * leaseSet = i2p::data::netdb.FindLeaseSet (ident);
if (!leaseSet) if (!leaseSet)
{ {
i2p::client::context.GetSharedLocalDestination ()->RequestDestination (ident); bool found = false;
std::this_thread::sleep_for (std::chrono::seconds (5)); // wait for 5 seconds std::unique_lock<std::mutex> l(newDataReceivedMutex);
leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (ident); i2p::client::context.GetSharedLocalDestination ()->RequestDestination (ident,
[&newDataReceived, &found](bool success)
{
found = success;
newDataReceived.notify_all ();
});
newDataReceived.wait (l);
if (found)
leaseSet = i2p::client::context.GetSharedLocalDestination ()->FindLeaseSet (ident);
} }
if (leaseSet) if (leaseSet)
{ {
@ -455,8 +465,6 @@ namespace client
bool end = false; bool end = false;
while (!end) while (!end)
{ {
std::condition_variable newDataReceived;
std::mutex newDataReceivedMutex;
stream->AsyncReceive (boost::asio::buffer (buf, 4096), stream->AsyncReceive (boost::asio::buffer (buf, 4096),
[&](const boost::system::error_code& ecode, std::size_t bytes_transferred) [&](const boost::system::error_code& ecode, std::size_t bytes_transferred)
{ {

46
Destination.cpp

@ -211,8 +211,9 @@ namespace client
auto it1 = m_LeaseSetRequests.find (msg->key); auto it1 = m_LeaseSetRequests.find (msg->key);
if (it1 != m_LeaseSetRequests.end ()) if (it1 != m_LeaseSetRequests.end ())
{ {
if (it1->second->requestComplete) it1->second->requestComplete (true);
delete it1->second; delete it1->second;
m_LeaseSetRequests.erase (it1); m_LeaseSetRequests.erase (it1);
} }
size_t offset = sizeof (I2NPDatabaseStoreMsg); size_t offset = sizeof (I2NPDatabaseStoreMsg);
if (msg->replyToken) // TODO: if (msg->replyToken) // TODO:
@ -242,10 +243,11 @@ namespace client
int num = buf[32]; // num int num = buf[32]; // num
LogPrint ("DatabaseSearchReply for ", key.ToBase64 (), " num=", num); LogPrint ("DatabaseSearchReply for ", key.ToBase64 (), " num=", num);
auto it = m_LeaseSetRequests.find (key); auto it = m_LeaseSetRequests.find (key);
if (it != m_LeaseSetRequests.end ()) if (it != m_LeaseSetRequests.end ())
{ {
LeaseSetRequest * request = it->second;
bool found = false; bool found = false;
if (it->second->excluded.size () < MAX_NUM_FLOODFILLS_PER_REQUEST) if (request->excluded.size () < MAX_NUM_FLOODFILLS_PER_REQUEST)
{ {
for (int i = 0; i < num; i++) for (int i = 0; i < num; i++)
{ {
@ -253,8 +255,9 @@ namespace client
auto floodfill = i2p::data::netdb.FindRouter (peerHash); auto floodfill = i2p::data::netdb.FindRouter (peerHash);
if (floodfill) if (floodfill)
{ {
found = true; LogPrint (eLogInfo, "Requesting ", key.ToBase64 (), " at ", peerHash.ToBase64 ());
SendLeaseSetRequest (key, floodfill, it->second); if (SendLeaseSetRequest (key, floodfill, request))
found = true;
} }
else else
{ {
@ -262,14 +265,16 @@ namespace client
i2p::data::netdb.RequestDestination (peerHash); i2p::data::netdb.RequestDestination (peerHash);
} }
} }
if (!found)
LogPrint (eLogError, "Suggested floodfills are not presented in netDb");
} }
else else
LogPrint (eLogInfo, key.ToBase64 (), " was not found on ", MAX_NUM_FLOODFILLS_PER_REQUEST," floodfills"); LogPrint (eLogInfo, key.ToBase64 (), " was not found on ", MAX_NUM_FLOODFILLS_PER_REQUEST," floodfills");
if (!found) if (!found)
{ {
LogPrint (eLogError, "Suggested floodfills are not presented in netDb"); if (request->requestComplete) request->requestComplete (false);
delete it->second; delete request;
m_LeaseSetRequests.erase (it); m_LeaseSetRequests.erase (key);
} }
} }
else else
@ -407,28 +412,35 @@ namespace client
return m_DatagramDestination; return m_DatagramDestination;
} }
bool ClientDestination::RequestDestination (const i2p::data::IdentHash& dest) bool ClientDestination::RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete)
{ {
if (!m_Pool || !IsReady ()) return false; if (!m_Pool || !IsReady ()) return false;
m_Service.post (std::bind (&ClientDestination::RequestLeaseSet, this, dest)); m_Service.post (std::bind (&ClientDestination::RequestLeaseSet, this, dest, requestComplete));
return true; return true;
} }
void ClientDestination::RequestLeaseSet (const i2p::data::IdentHash& dest) void ClientDestination::RequestLeaseSet (const i2p::data::IdentHash& dest, RequestComplete requestComplete)
{ {
std::set<i2p::data::IdentHash> excluded; std::set<i2p::data::IdentHash> excluded;
auto floodfill = i2p::data::netdb.GetClosestFloodfill (dest, excluded); auto floodfill = i2p::data::netdb.GetClosestFloodfill (dest, excluded);
if (floodfill) if (floodfill)
{ {
LeaseSetRequest * request = new LeaseSetRequest (m_Service); LeaseSetRequest * request = new LeaseSetRequest (m_Service);
request->requestComplete = requestComplete;
m_LeaseSetRequests[dest] = request; m_LeaseSetRequests[dest] = request;
SendLeaseSetRequest (dest, floodfill, request); if (!SendLeaseSetRequest (dest, floodfill, request))
{
// request failed
if (request->requestComplete) request->requestComplete (false);
delete request;
m_LeaseSetRequests.erase (dest);
}
} }
else else
LogPrint (eLogError, "No floodfills found"); LogPrint (eLogError, "No floodfills found");
} }
void ClientDestination::SendLeaseSetRequest (const i2p::data::IdentHash& dest, bool ClientDestination::SendLeaseSetRequest (const i2p::data::IdentHash& dest,
std::shared_ptr<const i2p::data::RouterInfo> nextFloodfill, LeaseSetRequest * request) std::shared_ptr<const i2p::data::RouterInfo> nextFloodfill, LeaseSetRequest * request)
{ {
auto replyTunnel = m_Pool->GetNextInboundTunnel (); auto replyTunnel = m_Pool->GetNextInboundTunnel ();
@ -459,11 +471,8 @@ namespace client
this, std::placeholders::_1, dest)); this, std::placeholders::_1, dest));
} }
else else
{ return false;
// request failed return true;
delete request;
m_LeaseSetRequests.erase (dest);
}
} }
void ClientDestination::HandleRequestTimoutTimer (const boost::system::error_code& ecode, const i2p::data::IdentHash& dest) void ClientDestination::HandleRequestTimoutTimer (const boost::system::error_code& ecode, const i2p::data::IdentHash& dest)
@ -491,6 +500,7 @@ namespace client
if (done) if (done)
{ {
if (it->second->requestComplete) it->second->requestComplete (false);
delete it->second; delete it->second;
m_LeaseSetRequests.erase (it); m_LeaseSetRequests.erase (it);
} }

9
Destination.h

@ -7,6 +7,7 @@
#include <map> #include <map>
#include <set> #include <set>
#include <string> #include <string>
#include <functional>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "Identity.h" #include "Identity.h"
#include "TunnelPool.h" #include "TunnelPool.h"
@ -37,12 +38,14 @@ namespace client
class ClientDestination: public i2p::garlic::GarlicDestination class ClientDestination: public i2p::garlic::GarlicDestination
{ {
typedef std::function<void (bool success)> RequestComplete;
struct LeaseSetRequest struct LeaseSetRequest
{ {
LeaseSetRequest (boost::asio::io_service& service): requestTime (0), requestTimeoutTimer (service) {}; LeaseSetRequest (boost::asio::io_service& service): requestTime (0), requestTimeoutTimer (service) {};
std::set<i2p::data::IdentHash> excluded; std::set<i2p::data::IdentHash> excluded;
uint64_t requestTime; uint64_t requestTime;
boost::asio::deadline_timer requestTimeoutTimer; boost::asio::deadline_timer requestTimeoutTimer;
RequestComplete requestComplete;
}; };
public: public:
@ -57,7 +60,7 @@ namespace client
i2p::tunnel::TunnelPool * GetTunnelPool () { return m_Pool; }; i2p::tunnel::TunnelPool * GetTunnelPool () { return m_Pool; };
bool IsReady () const { return m_LeaseSet && m_LeaseSet->HasNonExpiredLeases (); }; bool IsReady () const { return m_LeaseSet && m_LeaseSet->HasNonExpiredLeases (); };
const i2p::data::LeaseSet * FindLeaseSet (const i2p::data::IdentHash& ident); const i2p::data::LeaseSet * FindLeaseSet (const i2p::data::IdentHash& ident);
bool RequestDestination (const i2p::data::IdentHash& dest); bool RequestDestination (const i2p::data::IdentHash& dest, RequestComplete requestComplete = nullptr);
// streaming // streaming
i2p::stream::StreamingDestination * GetStreamingDestination () const { return m_StreamingDestination; }; i2p::stream::StreamingDestination * GetStreamingDestination () const { return m_StreamingDestination; };
@ -98,8 +101,8 @@ namespace client
void HandleDatabaseSearchReplyMessage (const uint8_t * buf, size_t len); void HandleDatabaseSearchReplyMessage (const uint8_t * buf, size_t len);
void HandleDeliveryStatusMessage (I2NPMessage * msg); void HandleDeliveryStatusMessage (I2NPMessage * msg);
void RequestLeaseSet (const i2p::data::IdentHash& dest); void RequestLeaseSet (const i2p::data::IdentHash& dest, RequestComplete requestComplete);
void SendLeaseSetRequest (const i2p::data::IdentHash& dest, std::shared_ptr<const i2p::data::RouterInfo> nextFloodfill, LeaseSetRequest * request); bool SendLeaseSetRequest (const i2p::data::IdentHash& dest, std::shared_ptr<const i2p::data::RouterInfo> nextFloodfill, LeaseSetRequest * request);
void HandleRequestTimoutTimer (const boost::system::error_code& ecode, const i2p::data::IdentHash& dest); void HandleRequestTimoutTimer (const boost::system::error_code& ecode, const i2p::data::IdentHash& dest);
private: private:

Loading…
Cancel
Save