/* */ #include "DHTMessageTracker.h" #include #include "DHTMessage.h" #include "DHTMessageCallback.h" #include "DHTMessageTrackerEntry.h" #include "DHTNode.h" #include "DHTRoutingTable.h" #include "DHTMessageFactory.h" #include "util.h" #include "LogFactory.h" #include "Logger.h" #include "DlAbortEx.h" #include "DHTConstants.h" #include "StringFormat.h" namespace aria2 { DHTMessageTracker::DHTMessageTracker(): logger_(LogFactory::getInstance()) {} DHTMessageTracker::~DHTMessageTracker() {} void DHTMessageTracker::addMessage(const SharedHandle& message, time_t timeout, const SharedHandle& callback) { SharedHandle e(new DHTMessageTrackerEntry(message, timeout, callback)); entries_.push_back(e); } std::pair, SharedHandle > DHTMessageTracker::messageArrived (const Dict* dict, const std::string& ipaddr, uint16_t port) { const String* tid = asString(dict->get(DHTMessage::T)); if(!tid) { throw DL_ABORT_EX(StringFormat("Malformed DHT message. From:%s:%u", ipaddr.c_str(), port).str()); } if(logger_->debug()) { logger_->debug("Searching tracker entry for TransactionID=%s, Remote=%s:%u", util::toHex(tid->s()).c_str(), ipaddr.c_str(), port); } for(std::deque >::iterator i = entries_.begin(), eoi = entries_.end(); i != eoi; ++i) { if((*i)->match(tid->s(), ipaddr, port)) { SharedHandle entry = *i; entries_.erase(i); if(logger_->debug()) { logger_->debug("Tracker entry found."); } SharedHandle targetNode = entry->getTargetNode(); SharedHandle message = factory_->createResponseMessage(entry->getMessageType(), dict, targetNode->getIPAddress(), targetNode->getPort()); int64_t rtt = entry->getElapsedMillis(); if(logger_->debug()) { logger_->debug("RTT is %s", util::itos(rtt).c_str()); } message->getRemoteNode()->updateRTT(rtt); SharedHandle callback = entry->getCallback(); return std::make_pair(message, callback); } } if(logger_->debug()) { logger_->debug("Tracker entry not found."); } return std::pair, SharedHandle >(); } void DHTMessageTracker::handleTimeout() { for(std::deque >::iterator i = entries_.begin(), eoi = entries_.end(); i != eoi;) { if((*i)->isTimeout()) { try { SharedHandle entry = *i; i = entries_.erase(i); eoi = entries_.end(); SharedHandle node = entry->getTargetNode(); if(logger_->debug()) { logger_->debug("Message timeout: To:%s:%u", node->getIPAddress().c_str(), node->getPort()); } node->updateRTT(entry->getElapsedMillis()); node->timeout(); if(node->isBad()) { if(logger_->debug()) { logger_->debug("Marked bad: %s:%u", node->getIPAddress().c_str(), node->getPort()); } routingTable_->dropNode(node); } SharedHandle callback = entry->getCallback(); if(!callback.isNull()) { callback->onTimeout(node); } } catch(RecoverableException& e) { logger_->info("Exception thrown while handling timeouts.", e); } } else { ++i; } } } SharedHandle DHTMessageTracker::getEntryFor(const SharedHandle& message) const { for(std::deque >::const_iterator i = entries_.begin(), eoi = entries_.end(); i != eoi; ++i) { if((*i)->match(message->getTransactionID(), message->getRemoteNode()->getIPAddress(), message->getRemoteNode()->getPort())) { return *i; } } return SharedHandle(); } size_t DHTMessageTracker::countEntry() const { return entries_.size(); } void DHTMessageTracker::setRoutingTable (const SharedHandle& routingTable) { routingTable_ = routingTable; } void DHTMessageTracker::setMessageFactory (const SharedHandle& factory) { factory_ = factory; } } // namespace aria2