From 898b807ba2cf2c1ec1e64848c560e9dc09a903ce Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 13 May 2008 15:55:05 +0000 Subject: [PATCH] 2008-05-13 Tatsuhiro Tsujikawa Made string literal to static const std::string * src/DHTAbstractMessage.cc * src/DHTAnnouncePeerMessage.cc * src/DHTAnnouncePeerMessage.h * src/DHTAnnouncePeerReplyMessage.cc * src/DHTAnnouncePeerReplyMessage.h * src/DHTFindNodeMessage.cc * src/DHTFindNodeMessage.h * src/DHTFindNodeReplyMessage.cc * src/DHTFindNodeReplyMessage.h * src/DHTGetPeersMessage.cc * src/DHTGetPeersMessage.h * src/DHTGetPeersReplyMessage.cc * src/DHTGetPeersReplyMessage.h * src/DHTMessage.cc * src/DHTMessage.h * src/DHTMessageFactoryImpl.cc * src/DHTMessageReceiver.cc * src/DHTPingMessage.cc * src/DHTPingMessage.h * src/DHTPingReplyMessage.cc * src/DHTPingReplyMessage.h * src/DHTQueryMessage.cc * src/DHTQueryMessage.h * src/DHTResponseMessage.cc * src/DHTResponseMessage.h * src/DHTUnknownMessage.cc * src/DHTUnknownMessage.h --- ChangeLog | 31 +++++++++++++ src/DHTAbstractMessage.cc | 4 +- src/DHTAnnouncePeerMessage.cc | 18 +++++--- src/DHTAnnouncePeerMessage.h | 8 ++++ src/DHTAnnouncePeerReplyMessage.cc | 6 ++- src/DHTAnnouncePeerReplyMessage.h | 2 + src/DHTFindNodeMessage.cc | 10 +++-- src/DHTFindNodeMessage.h | 5 +++ src/DHTFindNodeReplyMessage.cc | 10 +++-- src/DHTFindNodeReplyMessage.h | 4 ++ src/DHTGetPeersMessage.cc | 10 +++-- src/DHTGetPeersMessage.h | 5 +++ src/DHTGetPeersReplyMessage.cc | 18 +++++--- src/DHTGetPeersReplyMessage.h | 8 ++++ src/DHTMessage.cc | 7 ++- src/DHTMessage.h | 6 +++ src/DHTMessageFactoryImpl.cc | 71 +++++++++++++++--------------- src/DHTMessageReceiver.cc | 7 ++- src/DHTPingMessage.cc | 6 ++- src/DHTPingMessage.h | 2 + src/DHTPingReplyMessage.cc | 6 ++- src/DHTPingReplyMessage.h | 2 + src/DHTQueryMessage.cc | 10 +++-- src/DHTQueryMessage.h | 4 ++ src/DHTResponseMessage.cc | 6 ++- src/DHTResponseMessage.h | 2 + src/DHTUnknownMessage.cc | 6 ++- src/DHTUnknownMessage.h | 4 ++ 28 files changed, 207 insertions(+), 71 deletions(-) diff --git a/ChangeLog b/ChangeLog index 055b38ba..c91a8f1a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,34 @@ +2008-05-13 Tatsuhiro Tsujikawa + + Made string literal to static const std::string + * src/DHTAbstractMessage.cc + * src/DHTAnnouncePeerMessage.cc + * src/DHTAnnouncePeerMessage.h + * src/DHTAnnouncePeerReplyMessage.cc + * src/DHTAnnouncePeerReplyMessage.h + * src/DHTFindNodeMessage.cc + * src/DHTFindNodeMessage.h + * src/DHTFindNodeReplyMessage.cc + * src/DHTFindNodeReplyMessage.h + * src/DHTGetPeersMessage.cc + * src/DHTGetPeersMessage.h + * src/DHTGetPeersReplyMessage.cc + * src/DHTGetPeersReplyMessage.h + * src/DHTMessage.cc + * src/DHTMessage.h + * src/DHTMessageFactoryImpl.cc + * src/DHTMessageReceiver.cc + * src/DHTPingMessage.cc + * src/DHTPingMessage.h + * src/DHTPingReplyMessage.cc + * src/DHTPingReplyMessage.h + * src/DHTQueryMessage.cc + * src/DHTQueryMessage.h + * src/DHTResponseMessage.cc + * src/DHTResponseMessage.h + * src/DHTUnknownMessage.cc + * src/DHTUnknownMessage.h + 2008-05-13 Tatsuhiro Tsujikawa Made string literal to static const std::string diff --git a/src/DHTAbstractMessage.cc b/src/DHTAbstractMessage.cc index 85d1fe72..fe6b46b9 100644 --- a/src/DHTAbstractMessage.cc +++ b/src/DHTAbstractMessage.cc @@ -55,8 +55,8 @@ DHTAbstractMessage::~DHTAbstractMessage() {} std::string DHTAbstractMessage::getBencodedMessage() { SharedHandle msg(new Dictionary()); - msg->put(std::string("t"), new Data(_transactionID)); - msg->put(std::string("y"), new Data(getType())); + msg->put(DHTMessage::T, new Data(_transactionID)); + msg->put(DHTMessage::Y, new Data(getType())); fillMessage(msg.get()); BencodeVisitor v; diff --git a/src/DHTAnnouncePeerMessage.cc b/src/DHTAnnouncePeerMessage.cc index 802d361f..21422c43 100644 --- a/src/DHTAnnouncePeerMessage.cc +++ b/src/DHTAnnouncePeerMessage.cc @@ -50,6 +50,14 @@ namespace aria2 { +const std::string DHTAnnouncePeerMessage::ANNOUNCE_PEER("announce_peer"); + +const std::string DHTAnnouncePeerMessage::INFO_HASH("info_hash"); + +const std::string DHTAnnouncePeerMessage::PORT("port"); + +const std::string DHTAnnouncePeerMessage::TOKEN("token"); + DHTAnnouncePeerMessage::DHTAnnouncePeerMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const unsigned char* infoHash, @@ -78,19 +86,19 @@ void DHTAnnouncePeerMessage::doReceivedAction() Dictionary* DHTAnnouncePeerMessage::getArgument() { Dictionary* a = new Dictionary(); - a->put("id", new Data(reinterpret_cast(_localNode->getID()), + a->put(DHTMessage::ID, new Data(reinterpret_cast(_localNode->getID()), DHT_ID_LENGTH)); - a->put("info_hash", new Data(reinterpret_cast(_infoHash), + a->put(INFO_HASH, new Data(reinterpret_cast(_infoHash), DHT_ID_LENGTH)); - a->put("port", new Data(Util::uitos(_tcpPort), true)); - a->put("token", new Data(_token)); + a->put(PORT, new Data(Util::uitos(_tcpPort), true)); + a->put(TOKEN, new Data(_token)); return a; } std::string DHTAnnouncePeerMessage::getMessageType() const { - return "announce_peer"; + return ANNOUNCE_PEER; } void DHTAnnouncePeerMessage::validate() const diff --git a/src/DHTAnnouncePeerMessage.h b/src/DHTAnnouncePeerMessage.h index 55380d53..ec00c6f1 100644 --- a/src/DHTAnnouncePeerMessage.h +++ b/src/DHTAnnouncePeerMessage.h @@ -93,6 +93,14 @@ public: void setPeerAnnounceStorage(const WeakHandle& storage); void setTokenTracker(const WeakHandle& tokenTracker); + + static const std::string ANNOUNCE_PEER; + + static const std::string INFO_HASH; + + static const std::string PORT; + + static const std::string TOKEN; }; } // namespace aria2 diff --git a/src/DHTAnnouncePeerReplyMessage.cc b/src/DHTAnnouncePeerReplyMessage.cc index 5dc68ddb..edd99641 100644 --- a/src/DHTAnnouncePeerReplyMessage.cc +++ b/src/DHTAnnouncePeerReplyMessage.cc @@ -39,6 +39,8 @@ namespace aria2 { +const std::string DHTAnnouncePeerReplyMessage::ANNOUNCE_PEER("announce_peer"); + DHTAnnouncePeerReplyMessage::DHTAnnouncePeerReplyMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const std::string& transactionID): @@ -51,13 +53,13 @@ void DHTAnnouncePeerReplyMessage::doReceivedAction() {} Dictionary* DHTAnnouncePeerReplyMessage::getResponse() { Dictionary* r = new Dictionary(); - r->put("id", new Data(_localNode->getID(), DHT_ID_LENGTH)); + r->put(DHTMessage::ID, new Data(_localNode->getID(), DHT_ID_LENGTH)); return r; } std::string DHTAnnouncePeerReplyMessage::getMessageType() const { - return "announce_peer"; + return ANNOUNCE_PEER; } void DHTAnnouncePeerReplyMessage::validate() const {} diff --git a/src/DHTAnnouncePeerReplyMessage.h b/src/DHTAnnouncePeerReplyMessage.h index 28309aba..98e50682 100644 --- a/src/DHTAnnouncePeerReplyMessage.h +++ b/src/DHTAnnouncePeerReplyMessage.h @@ -54,6 +54,8 @@ public: virtual std::string getMessageType() const; virtual void validate() const; + + static const std::string ANNOUNCE_PEER; }; } // namespace aria2 diff --git a/src/DHTFindNodeMessage.cc b/src/DHTFindNodeMessage.cc index 951c14e3..f5b0ab2c 100644 --- a/src/DHTFindNodeMessage.cc +++ b/src/DHTFindNodeMessage.cc @@ -45,6 +45,10 @@ namespace aria2 { +const std::string DHTFindNodeMessage::FIND_NODE("find_node"); + +const std::string DHTFindNodeMessage::TARGET_NODE("target"); + DHTFindNodeMessage::DHTFindNodeMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const unsigned char* targetNodeID, @@ -68,16 +72,16 @@ void DHTFindNodeMessage::doReceivedAction() Dictionary* DHTFindNodeMessage::getArgument() { Dictionary* a = new Dictionary(); - a->put("id", new Data(reinterpret_cast(_localNode->getID()), + a->put(DHTMessage::ID, new Data(reinterpret_cast(_localNode->getID()), DHT_ID_LENGTH)); - a->put("target", new Data(reinterpret_cast(_targetNodeID), + a->put(TARGET_NODE, new Data(reinterpret_cast(_targetNodeID), DHT_ID_LENGTH)); return a; } std::string DHTFindNodeMessage::getMessageType() const { - return "find_node"; + return FIND_NODE; } void DHTFindNodeMessage::validate() const {} diff --git a/src/DHTFindNodeMessage.h b/src/DHTFindNodeMessage.h index 1fc9d97c..95486ac8 100644 --- a/src/DHTFindNodeMessage.h +++ b/src/DHTFindNodeMessage.h @@ -65,6 +65,11 @@ public: { return _targetNodeID; } + + static const std::string FIND_NODE; + + // We want "TARGET", but it is defined by macro. + static const std::string TARGET_NODE; }; } // namespace aria2 diff --git a/src/DHTFindNodeReplyMessage.cc b/src/DHTFindNodeReplyMessage.cc index 67570618..47f06db1 100644 --- a/src/DHTFindNodeReplyMessage.cc +++ b/src/DHTFindNodeReplyMessage.cc @@ -47,6 +47,10 @@ namespace aria2 { +const std::string DHTFindNodeReplyMessage::FIND_NODE("find_node"); + +const std::string DHTFindNodeReplyMessage::NODES("nodes"); + DHTFindNodeReplyMessage::DHTFindNodeReplyMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const std::string& transactionID): @@ -66,7 +70,7 @@ void DHTFindNodeReplyMessage::doReceivedAction() Dictionary* DHTFindNodeReplyMessage::getResponse() { Dictionary* a = new Dictionary(); - a->put("id", new Data(_localNode->getID(), DHT_ID_LENGTH)); + a->put(DHTMessage::ID, new Data(_localNode->getID(), DHT_ID_LENGTH)); size_t offset = 0; unsigned char buffer[DHTBucket::K*26]; // TODO if _closestKNodes.size() > DHTBucket::K ?? @@ -77,13 +81,13 @@ Dictionary* DHTFindNodeReplyMessage::getResponse() offset += 26; } } - a->put("nodes", new Data(buffer, offset)); + a->put(NODES, new Data(buffer, offset)); return a; } std::string DHTFindNodeReplyMessage::getMessageType() const { - return "find_node"; + return FIND_NODE; } void DHTFindNodeReplyMessage::validate() const {} diff --git a/src/DHTFindNodeReplyMessage.h b/src/DHTFindNodeReplyMessage.h index 3d301579..c3ef632c 100644 --- a/src/DHTFindNodeReplyMessage.h +++ b/src/DHTFindNodeReplyMessage.h @@ -63,6 +63,10 @@ public: const std::deque >& getClosestKNodes() const; void setClosestKNodes(const std::deque >& closestKNodes); + + static const std::string FIND_NODE; + + static const std::string NODES; }; } // namespace aria2 diff --git a/src/DHTGetPeersMessage.cc b/src/DHTGetPeersMessage.cc index f0333580..5a4a6291 100644 --- a/src/DHTGetPeersMessage.cc +++ b/src/DHTGetPeersMessage.cc @@ -49,6 +49,10 @@ namespace aria2 { +const std::string DHTGetPeersMessage::GET_PEERS("get_peers"); + +const std::string DHTGetPeersMessage::INFO_HASH("info_hash"); + DHTGetPeersMessage::DHTGetPeersMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const unsigned char* infoHash, @@ -86,14 +90,14 @@ void DHTGetPeersMessage::doReceivedAction() Dictionary* DHTGetPeersMessage::getArgument() { Dictionary* a = new Dictionary(); - a->put("id", new Data(_localNode->getID(), DHT_ID_LENGTH)); - a->put("info_hash", new Data(_infoHash, DHT_ID_LENGTH)); + a->put(DHTMessage::ID, new Data(_localNode->getID(), DHT_ID_LENGTH)); + a->put(INFO_HASH, new Data(_infoHash, DHT_ID_LENGTH)); return a; } std::string DHTGetPeersMessage::getMessageType() const { - return "get_peers"; + return GET_PEERS; } void DHTGetPeersMessage::validate() const {} diff --git a/src/DHTGetPeersMessage.h b/src/DHTGetPeersMessage.h index 42b4d6ce..20ed3474 100644 --- a/src/DHTGetPeersMessage.h +++ b/src/DHTGetPeersMessage.h @@ -78,6 +78,11 @@ public: void setTokenTracker(const WeakHandle& tokenTracker); + + static const std::string GET_PEERS; + + static const std::string INFO_HASH; + }; } // namespace aria2 diff --git a/src/DHTGetPeersReplyMessage.cc b/src/DHTGetPeersReplyMessage.cc index 725a9670..6c0fb423 100644 --- a/src/DHTGetPeersReplyMessage.cc +++ b/src/DHTGetPeersReplyMessage.cc @@ -50,6 +50,14 @@ 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, @@ -67,11 +75,11 @@ void DHTGetPeersReplyMessage::doReceivedAction() Dictionary* DHTGetPeersReplyMessage::getResponse() { Dictionary* r = new Dictionary(); - r->put("id", new Data(_localNode->getID(), DHT_ID_LENGTH)); - r->put("token", new Data(_token)); + r->put(DHTMessage::ID, new Data(_localNode->getID(), DHT_ID_LENGTH)); + r->put(TOKEN, new Data(_token)); if(_values.size()) { List* valuesList = new List(); - r->put("values", valuesList); + r->put(VALUES, valuesList); for(std::deque >::const_iterator i = _values.begin(); i != _values.end(); ++i) { const SharedHandle& peer = *i; unsigned char buffer[6]; @@ -89,14 +97,14 @@ Dictionary* DHTGetPeersReplyMessage::getResponse() offset += 26; } } - r->put("nodes", new Data(buffer, offset)); + r->put(NODES, new Data(buffer, offset)); } return r; } std::string DHTGetPeersReplyMessage::getMessageType() const { - return "get_peers"; + return GET_PEERS; } void DHTGetPeersReplyMessage::validate() const {} diff --git a/src/DHTGetPeersReplyMessage.h b/src/DHTGetPeersReplyMessage.h index daedc27a..5b526546 100644 --- a/src/DHTGetPeersReplyMessage.h +++ b/src/DHTGetPeersReplyMessage.h @@ -80,6 +80,14 @@ public: { return _token; } + + static const std::string GET_PEERS; + + static const std::string TOKEN; + + static const std::string VALUES; + + static const std::string NODES; }; } // namespace aria2 diff --git a/src/DHTMessage.cc b/src/DHTMessage.cc index 5b1d07e4..0ecc2fe2 100644 --- a/src/DHTMessage.cc +++ b/src/DHTMessage.cc @@ -38,6 +38,12 @@ namespace aria2 { +const std::string DHTMessage::Y("y"); + +const std::string DHTMessage::T("t"); + +const std::string DHTMessage::ID("id"); + DHTMessage::DHTMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const std::string& transactionID): @@ -67,5 +73,4 @@ SharedHandle DHTMessage::getRemoteNode() const return _remoteNode; } - } // namespace aria2 diff --git a/src/DHTMessage.h b/src/DHTMessage.h index 53bde055..bc018fdd 100644 --- a/src/DHTMessage.h +++ b/src/DHTMessage.h @@ -80,6 +80,12 @@ public: virtual std::string getMessageType() const = 0; virtual std::string toString() const = 0; + + static const std::string Y; + + static const std::string T; + + static const std::string ID; }; } // namespace aria2 diff --git a/src/DHTMessageFactoryImpl.cc b/src/DHTMessageFactoryImpl.cc index cbe083e9..e2621708 100644 --- a/src/DHTMessageFactoryImpl.cc +++ b/src/DHTMessageFactoryImpl.cc @@ -160,36 +160,36 @@ SharedHandle DHTMessageFactoryImpl::createQueryMessage(const Diction const std::string& ipaddr, uint16_t port) { - const Data* q = getData(d, "q"); - const Data* t = getData(d, "t"); - const Data* y = getData(d, "y"); - const Dictionary* a = getDictionary(d, "a"); - if(y->toString() != "q") { + const Data* q = getData(d, DHTQueryMessage::Q); + const Data* t = getData(d, DHTMessage::T); + const Data* y = getData(d, DHTMessage::Y); + const Dictionary* a = getDictionary(d, DHTQueryMessage::A); + if(y->toString() != DHTQueryMessage::Q) { throw DlAbortEx("Malformed DHT message. y != q"); } - const Data* id = getData(getDictionary(d, "a"), "id"); + const Data* id = getData(getDictionary(d, DHTQueryMessage::A), DHTMessage::ID); validateID(id); SharedHandle remoteNode = getRemoteNode(id->getData(), ipaddr, port); std::string messageType = q->toString(); std::string transactionID = t->toString(); - if(messageType == "ping") { + if(messageType == DHTPingMessage::PING) { return createPingMessage(remoteNode, transactionID); - } else if(messageType == "find_node") { - const Data* targetNodeID = getData(a, "target"); + } else if(messageType == DHTFindNodeMessage::FIND_NODE) { + const Data* targetNodeID = getData(a, DHTFindNodeMessage::TARGET_NODE); validateID(targetNodeID); return createFindNodeMessage(remoteNode, targetNodeID->getData(), transactionID); - } else if(messageType == "get_peers") { - const Data* infoHash = getData(a, "info_hash"); + } else if(messageType == DHTGetPeersMessage::GET_PEERS) { + const Data* infoHash = getData(a, DHTGetPeersMessage::INFO_HASH); validateID(infoHash); return createGetPeersMessage(remoteNode, infoHash->getData(), transactionID); - } else if(messageType == "announce_peer") { - const Data* infoHash = getData(a, "info_hash"); + } else if(messageType == DHTAnnouncePeerMessage::ANNOUNCE_PEER) { + const Data* infoHash = getData(a, DHTAnnouncePeerMessage::INFO_HASH); validateID(infoHash); - const Data* port = getData(a, "port"); + const Data* port = getData(a, DHTAnnouncePeerMessage::PORT); validatePort(port); - const Data* token = getData(a, "token"); + const Data* token = getData(a, DHTAnnouncePeerMessage::TOKEN); return createAnnouncePeerMessage(remoteNode, infoHash->getData(), static_cast(port->toInt()), token->toString(), transactionID); @@ -204,11 +204,11 @@ DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType, const Dictionary* d, const SharedHandle& remoteNode) { - const Data* t = getData(d, "t"); - const Data* y = getData(d, "y"); - if(y->toString() == "e") { + const Data* t = getData(d, DHTMessage::T); + const Data* y = getData(d, DHTMessage::Y); + if(y->toString() == DHTUnknownMessage::E) { // for now, just report error message arrived and throw exception. - const List* e = getList(d, "e"); + const List* e = getList(d, DHTUnknownMessage::E); if(e->getList().size() == 2) { _logger->info("Received Error DHT message. code=%s, msg=%s", Util::urlencode(getData(e, 0)->toString()).c_str(), @@ -217,34 +217,35 @@ DHTMessageFactoryImpl::createResponseMessage(const std::string& messageType, _logger->debug("e doesn't have 2 elements."); } throw DlAbortEx("Received Error DHT message."); - } else if(y->toString() != "r") { + } else if(y->toString() != DHTResponseMessage::R) { throw DlAbortEx (StringFormat("Malformed DHT message. y != r: y=%s", Util::urlencode(y->toString()).c_str()).str()); } - const Dictionary* r = getDictionary(d, "r"); - const Data* id = getData(r, "id"); + const Dictionary* r = getDictionary(d, DHTResponseMessage::R); + const Data* id = getData(r, DHTMessage::ID); validateID(id); validateIDMatch(remoteNode->getID(), id->getData()); std::string transactionID = t->toString(); - if(messageType == "ping") { + if(messageType == DHTPingReplyMessage::PING) { return createPingReplyMessage(remoteNode, id->getData(), transactionID); - } else if(messageType == "find_node") { + } else if(messageType == DHTFindNodeReplyMessage::FIND_NODE) { return createFindNodeReplyMessage(remoteNode, d, transactionID); - } else if(messageType == "get_peers") { - const List* values = dynamic_cast(r->get("values")); + } else if(messageType == DHTGetPeersReplyMessage::GET_PEERS) { + const List* values = + dynamic_cast(r->get(DHTGetPeersReplyMessage::VALUES)); if(values) { return createGetPeersReplyMessageWithValues(remoteNode, d, transactionID); } else { - const Data* nodes = dynamic_cast(r->get("nodes")); + const Data* nodes = dynamic_cast(r->get(DHTGetPeersReplyMessage::NODES)); if(nodes) { return createGetPeersReplyMessageWithNodes(remoteNode, d, transactionID); } else { throw DlAbortEx("Malformed DHT message: missing nodes/values"); } } - } else if(messageType == "announce_peer") { + } else if(messageType == DHTAnnouncePeerReplyMessage::ANNOUNCE_PEER) { return createAnnouncePeerReplyMessage(remoteNode, transactionID); } else { throw DlAbortEx @@ -325,7 +326,7 @@ DHTMessageFactoryImpl::createFindNodeReplyMessage(const SharedHandle& r const Dictionary* d, const std::string& transactionID) { - const Data* nodesData = getData(getDictionary(d, "r"), "nodes"); + const Data* nodesData = getData(getDictionary(d, DHTResponseMessage::R), DHTFindNodeReplyMessage::NODES); std::deque > nodes = extractNodes(nodesData->getData(), nodesData->getLen()); return createFindNodeReplyMessage(remoteNode, nodes, transactionID); } @@ -350,10 +351,10 @@ DHTMessageFactoryImpl::createGetPeersReplyMessageWithNodes(const SharedHandle > nodes = extractNodes(nodesData->getData(), nodesData->getLen()); - const Data* token = getData(r, "token"); + const Data* token = getData(r, DHTGetPeersReplyMessage::TOKEN); return createGetPeersReplyMessage(remoteNode, nodes, token->toString(), transactionID); } @@ -376,8 +377,8 @@ DHTMessageFactoryImpl::createGetPeersReplyMessageWithValues(const SharedHandle > peers; for(std::deque::const_iterator i = valuesList->getList().begin(); i != valuesList->getList().end(); ++i) { const Data* data = dynamic_cast(*i); @@ -387,7 +388,7 @@ DHTMessageFactoryImpl::createGetPeersReplyMessageWithValues(const SharedHandletoString(), transactionID); } diff --git a/src/DHTMessageReceiver.cc b/src/DHTMessageReceiver.cc index ff3a159d..8fcfaefa 100644 --- a/src/DHTMessageReceiver.cc +++ b/src/DHTMessageReceiver.cc @@ -36,6 +36,8 @@ #include "DHTMessageTracker.h" #include "DHTConnection.h" #include "DHTMessage.h" +#include "DHTResponseMessage.h" +#include "DHTUnknownMessage.h" #include "DHTMessageFactory.h" #include "DHTRoutingTable.h" #include "DHTNode.h" @@ -74,9 +76,10 @@ SharedHandle DHTMessageReceiver::receiveMessage() MetaEntryHandle msgroot(MetaFileUtil::bdecoding(data, length)); const Dictionary* d = dynamic_cast(msgroot.get()); if(d) { - const Data* y = dynamic_cast(d->get("y")); + const Data* y = dynamic_cast(d->get(DHTMessage::Y)); if(y) { - if(y->toString() == "r" || y->toString() == "e") { + if(y->toString() == DHTResponseMessage::R || + y->toString() == DHTUnknownMessage::E) { isReply = true; } } else { diff --git a/src/DHTPingMessage.cc b/src/DHTPingMessage.cc index 45fa1ee5..9e396625 100644 --- a/src/DHTPingMessage.cc +++ b/src/DHTPingMessage.cc @@ -43,6 +43,8 @@ namespace aria2 { +const std::string DHTPingMessage::PING("ping"); + DHTPingMessage::DHTPingMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const std::string& transactionID): @@ -60,14 +62,14 @@ void DHTPingMessage::doReceivedAction() Dictionary* DHTPingMessage::getArgument() { Dictionary* a = new Dictionary(); - a->put("id", new Data(reinterpret_cast(_localNode->getID()), + a->put(DHTMessage::ID, new Data(reinterpret_cast(_localNode->getID()), DHT_ID_LENGTH)); return a; } std::string DHTPingMessage::getMessageType() const { - return "ping"; + return PING; } void DHTPingMessage::validate() const {} diff --git a/src/DHTPingMessage.h b/src/DHTPingMessage.h index f1be9fe3..8b458cbc 100644 --- a/src/DHTPingMessage.h +++ b/src/DHTPingMessage.h @@ -55,6 +55,8 @@ public: virtual std::string getMessageType() const; virtual void validate() const; + + static const std::string PING; }; } // namespace aria2 diff --git a/src/DHTPingReplyMessage.cc b/src/DHTPingReplyMessage.cc index 068483fb..b4c75984 100644 --- a/src/DHTPingReplyMessage.cc +++ b/src/DHTPingReplyMessage.cc @@ -40,6 +40,8 @@ namespace aria2 { +const std::string DHTPingReplyMessage::PING("ping"); + DHTPingReplyMessage::DHTPingReplyMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const unsigned char* id, @@ -56,13 +58,13 @@ void DHTPingReplyMessage::doReceivedAction() {} Dictionary* DHTPingReplyMessage::getResponse() { Dictionary* r = new Dictionary(); - r->put("id", new Data(_id, DHT_ID_LENGTH)); + r->put(DHTMessage::ID, new Data(_id, DHT_ID_LENGTH)); return r; } std::string DHTPingReplyMessage::getMessageType() const { - return "ping"; + return PING; } void DHTPingReplyMessage::validate() const {} diff --git a/src/DHTPingReplyMessage.h b/src/DHTPingReplyMessage.h index b95eaa01..5c4130cf 100644 --- a/src/DHTPingReplyMessage.h +++ b/src/DHTPingReplyMessage.h @@ -63,6 +63,8 @@ public: { return _id; } + + static const std::string PING; }; } // namespace aria2 diff --git a/src/DHTQueryMessage.cc b/src/DHTQueryMessage.cc index 1ded98ab..895a2dc5 100644 --- a/src/DHTQueryMessage.cc +++ b/src/DHTQueryMessage.cc @@ -40,6 +40,10 @@ namespace aria2 { +const std::string DHTQueryMessage::Q("q"); + +const std::string DHTQueryMessage::A("a"); + DHTQueryMessage::DHTQueryMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const std::string& transactionID): @@ -49,13 +53,13 @@ DHTQueryMessage::~DHTQueryMessage() {} std::string DHTQueryMessage::getType() const { - return "q"; + return Q; } void DHTQueryMessage::fillMessage(Dictionary* message) { - message->put("q", new Data(getMessageType())); - message->put("a", getArgument()); + message->put(Q, new Data(getMessageType())); + message->put(A, getArgument()); } bool DHTQueryMessage::isReply() const diff --git a/src/DHTQueryMessage.h b/src/DHTQueryMessage.h index 76505239..dc9cb22f 100644 --- a/src/DHTQueryMessage.h +++ b/src/DHTQueryMessage.h @@ -59,6 +59,10 @@ public: virtual bool isReply() const; virtual std::string toString() const; + + static const std::string Q; + + static const std::string A; }; } // namespace aria2 diff --git a/src/DHTResponseMessage.cc b/src/DHTResponseMessage.cc index 5a66daae..b9cd374b 100644 --- a/src/DHTResponseMessage.cc +++ b/src/DHTResponseMessage.cc @@ -40,6 +40,8 @@ namespace aria2 { +const std::string DHTResponseMessage::R("r"); + DHTResponseMessage::DHTResponseMessage(const SharedHandle& localNode, const SharedHandle& remoteNode, const std::string& transactionID): @@ -49,12 +51,12 @@ DHTResponseMessage::~DHTResponseMessage() {} std::string DHTResponseMessage::getType() const { - return "r"; + return R; } void DHTResponseMessage::fillMessage(Dictionary* message) { - message->put("r", getResponse()); + message->put(R, getResponse()); } bool DHTResponseMessage::isReply() const diff --git a/src/DHTResponseMessage.h b/src/DHTResponseMessage.h index 2c4fb03a..8a2ecc79 100644 --- a/src/DHTResponseMessage.h +++ b/src/DHTResponseMessage.h @@ -59,6 +59,8 @@ public: virtual bool isReply() const; virtual std::string toString() const; + + static const std::string R; }; } // namespace aria2 diff --git a/src/DHTUnknownMessage.cc b/src/DHTUnknownMessage.cc index d188ab08..ab2b0fa2 100644 --- a/src/DHTUnknownMessage.cc +++ b/src/DHTUnknownMessage.cc @@ -39,6 +39,10 @@ namespace aria2 { +const std::string DHTUnknownMessage::E("e"); + +const std::string DHTUnknownMessage::UNKNOWN("unknown"); + DHTUnknownMessage::DHTUnknownMessage(const SharedHandle& localNode, const unsigned char* data, size_t length, const std::string& ipaddr, uint16_t port): @@ -73,7 +77,7 @@ void DHTUnknownMessage::validate() const {} std::string DHTUnknownMessage::getMessageType() const { - return "unknown"; + return UNKNOWN; } std::string DHTUnknownMessage::toString() const diff --git a/src/DHTUnknownMessage.h b/src/DHTUnknownMessage.h index 76dce6d2..47c37584 100644 --- a/src/DHTUnknownMessage.h +++ b/src/DHTUnknownMessage.h @@ -69,6 +69,10 @@ public: // show some sample bytes virtual std::string toString() const; + + static const std::string E; + + static const std::string UNKNOWN; }; } // namespace aria2