/* */ #include "DHTGetPeersReplyMessage.h" #include #include "DHTNode.h" #include "DHTBucket.h" #include "DHTRoutingTable.h" #include "DHTMessageFactory.h" #include "DHTMessageDispatcher.h" #include "DHTMessageCallback.h" #include "PeerMessageUtil.h" #include "Peer.h" #include "DHTUtil.h" #include "Util.h" #include "bencode.h" namespace aria2 { const std::string DHTGetPeersReplyMessage::GET_PEERS("get_peers"); const std::string DHTGetPeersReplyMessage::TOKEN("token"); const std::string DHTGetPeersReplyMessage::VALUES("values"); const std::string DHTGetPeersReplyMessage::NODES("nodes"); DHTGetPeersReplyMessage::DHTGetPeersReplyMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const std::string& token, const std::string& transactionID): DHTResponseMessage(localNode, remoteNode, transactionID), _token(token) {} DHTGetPeersReplyMessage::~DHTGetPeersReplyMessage() {} void DHTGetPeersReplyMessage::doReceivedAction() { // Returned peers and nodes are handled in DHTPeerLookupTask. } BDE DHTGetPeersReplyMessage::getResponse() { BDE rDict = BDE::dict(); rDict[DHTMessage::ID] = BDE(_localNode->getID(), DHT_ID_LENGTH); rDict[TOKEN] = _token; if(_values.empty()) { size_t offset = 0; unsigned char buffer[DHTBucket::K*26]; for(std::deque >::const_iterator i = _closestKNodes.begin(); i != _closestKNodes.end() && offset < DHTBucket::K*26; ++i) { SharedHandle node = *i; memcpy(buffer+offset, node->getID(), DHT_ID_LENGTH); if(PeerMessageUtil::createcompact(buffer+20+offset, node->getIPAddress(), node->getPort())) { offset += 26; } } rDict[NODES] = BDE(buffer, offset); } else { BDE valuesList = BDE::list(); for(std::deque >::const_iterator i = _values.begin(); i != _values.end(); ++i) { const SharedHandle& peer = *i; unsigned char buffer[6]; if(PeerMessageUtil::createcompact(buffer, peer->ipaddr, peer->port)) { valuesList << BDE(buffer, sizeof(buffer)); } } rDict[VALUES] = valuesList; } return rDict; } std::string DHTGetPeersReplyMessage::getMessageType() const { return GET_PEERS; } void DHTGetPeersReplyMessage::validate() const {} const std::deque >& DHTGetPeersReplyMessage::getClosestKNodes() const { return _closestKNodes; } void DHTGetPeersReplyMessage::setClosestKNodes(const std::deque >& closestKNodes) { _closestKNodes = closestKNodes; } const std::deque >& DHTGetPeersReplyMessage::getValues() const { return _values; } void DHTGetPeersReplyMessage::setValues(const std::deque >& peers) { _values = peers; } std::string DHTGetPeersReplyMessage::toStringOptional() const { return "token="+Util::toHex(_token)+", values="+Util::uitos(_values.size())+ ", nodes="+Util::uitos(_closestKNodes.size()); } } // namespace aria2