瀏覽代碼

2008-05-14 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>

	Made string literals for torrent file processing static const
	std::string.
	* src/BtContext.cc
	* src/BtContext.h
	* src/DefaultBtContext.cc
	* src/DefaultBtContext.h
Tatsuhiro Tsujikawa 17 年之前
父節點
當前提交
9d24736208
共有 7 個文件被更改,包括 135 次插入18 次删除
  1. 9 0
      ChangeLog
  2. 65 0
      src/BtContext.cc
  3. 27 0
      src/BtContext.h
  4. 27 15
      src/DefaultBtContext.cc
  5. 2 0
      src/DefaultBtContext.h
  6. 1 1
      src/Makefile.am
  7. 4 2
      src/Makefile.in

+ 9 - 0
ChangeLog

@@ -1,3 +1,12 @@
+2008-05-14  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
+
+	Made string literals for torrent file processing static const
+	std::string.
+	* src/BtContext.cc
+	* src/BtContext.h
+	* src/DefaultBtContext.cc
+	* src/DefaultBtContext.h
+
 2008-05-14  Tatsuhiro Tsujikawa  <tujikawa at rednoah dot com>
 
 	Made string literal for tracker response static const std::string

+ 65 - 0
src/BtContext.cc

@@ -0,0 +1,65 @@
+/* <!-- copyright */
+/*
+ * aria2 - The high speed download utility
+ *
+ * Copyright (C) 2006 Tatsuhiro Tsujikawa
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * In addition, as a special exception, the copyright holders give
+ * permission to link the code of portions of this program with the
+ * OpenSSL library under certain conditions as described in each
+ * individual source file, and distribute linked combinations
+ * including the two.
+ * You must obey the GNU General Public License in all respects
+ * for all of the code used other than OpenSSL.  If you modify
+ * file(s) with this exception, you may extend this exception to your
+ * version of the file(s), but you are not obligated to do so.  If you
+ * do not wish to do so, delete this exception statement from your
+ * version.  If you delete this exception statement from all source
+ * files in the program, then also delete it here.
+ */
+/* copyright --> */
+#include "BtContext.h"
+
+namespace aria2 {
+
+const std::string BtContext::NAME("name");
+
+const std::string BtContext::FILES("files");
+
+const std::string BtContext::LENGTH("length");
+
+const std::string BtContext::PATH("path");
+
+const std::string BtContext::INFO("info");
+
+const std::string BtContext::PIECES("pieces");
+
+const std::string BtContext::PIECE_LENGTH("piece length");
+
+const std::string BtContext::PRIVATE("private");
+
+const std::string BtContext::PRIVATE_ON("1");
+
+const std::string BtContext::URL_LIST("url-list");
+
+const std::string BtContext::ANNOUNCE("announce");
+
+const std::string BtContext::ANNOUNCE_LIST("announce-list");
+
+const std::string BtContext::NODES("nodes");
+
+} // namespace aria2

+ 27 - 0
src/BtContext.h

@@ -80,6 +80,33 @@ public:
 
   virtual std::deque<std::pair<std::string, uint16_t> >& getNodes() = 0;
 
+  static const std::string NAME;
+
+  static const std::string FILES;
+
+  static const std::string LENGTH;
+
+  static const std::string PATH;
+
+  static const std::string INFO;
+
+  static const std::string PIECES;
+
+  static const std::string PIECE_LENGTH;
+
+  static const std::string PRIVATE;
+
+  // This is just a string "1". Used as a value of "private" flag.
+  static const std::string PRIVATE_ON;
+
+  static const std::string URL_LIST;
+
+  static const std::string ANNOUNCE;
+
+  static const std::string ANNOUNCE_LIST;
+
+  static const std::string NODES;
+
 };
 
 class BtContext;

+ 27 - 15
src/DefaultBtContext.cc

@@ -58,7 +58,9 @@
 
 namespace aria2 {
 
-DefaultBtContext::DefaultBtContext():_peerIdPrefix("-aria2-"),
+const std::string DefaultBtContext::DEFAULT_PEER_ID_PREFIX("-aria2-");
+
+DefaultBtContext::DefaultBtContext():_peerIdPrefix(DEFAULT_PEER_ID_PREFIX),
 				     _randomizer(SimpleRandomizer::getInstance()),
 				     _ownerRequestGroup(0),
 				     _logger(LogFactory::getInstance()) {}
@@ -113,13 +115,13 @@ void DefaultBtContext::extractPieceHash(const unsigned char* hashData,
 void DefaultBtContext::extractFileEntries(const Dictionary* infoDic,
 					  const std::string& defaultName,
 					  const std::deque<std::string>& urlList) {
-  const Data* nameData = dynamic_cast<const Data*>(infoDic->get("name"));
+  const Data* nameData = dynamic_cast<const Data*>(infoDic->get(BtContext::NAME));
   if(nameData) {
     name = nameData->toString();
   } else {
     name = File(defaultName).getBasename()+".file";
   }
-  const List* files = dynamic_cast<const List*>(infoDic->get("files"));
+  const List* files = dynamic_cast<const List*>(infoDic->get(BtContext::FILES));
   if(files) {
     uint64_t length = 0;
     off_t offset = 0;
@@ -132,14 +134,16 @@ void DefaultBtContext::extractFileEntries(const Dictionary* infoDic,
       if(!fileDic) {
 	continue;
       }
-      const Data* lengthData = dynamic_cast<const Data*>(fileDic->get("length"));
+      const Data* lengthData =
+	dynamic_cast<const Data*>(fileDic->get(BtContext::LENGTH));
       if(lengthData) {
 	length += lengthData->toLLInt();
       } else {
 	throw DlAbortEx
 	  (StringFormat(MSG_SOMETHING_MISSING_IN_TORRENT, "file length").str());
       }
-      const List* pathList = dynamic_cast<const List*>(fileDic->get("path"));
+      const List* pathList =
+	dynamic_cast<const List*>(fileDic->get(BtContext::PATH));
       if(!pathList) {
 	throw DlAbortEx
 	  (StringFormat(MSG_SOMETHING_MISSING_IN_TORRENT, "file path list").str());
@@ -177,7 +181,8 @@ void DefaultBtContext::extractFileEntries(const Dictionary* infoDic,
   } else {
     // single-file mode;
     fileMode = BtContext::SINGLE;
-    const Data* length = dynamic_cast<const Data*>(infoDic->get("length"));
+    const Data* length =
+      dynamic_cast<const Data*>(infoDic->get(BtContext::LENGTH));
     if(length) {
       totalLength = length->toLLInt();
     } else {
@@ -290,7 +295,8 @@ void DefaultBtContext::load(const std::string& torrentFile) {
 void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const std::string& defaultName)
 {
   clear();
-  const Dictionary* infoDic = dynamic_cast<const Dictionary*>(rootDic->get("info"));
+  const Dictionary* infoDic =
+    dynamic_cast<const Dictionary*>(rootDic->get(BtContext::INFO));
   if(!infoDic) {
     throw DlAbortEx
       (StringFormat(MSG_SOMETHING_MISSING_IN_TORRENT, "info directory").str());
@@ -303,7 +309,8 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
 			      v.getBencodedData().size());
   infoHashString = Util::toHex(infoHash, INFO_HASH_LENGTH);
   // calculate the number of pieces
-  const Data* pieceHashData = dynamic_cast<const Data*>(infoDic->get("pieces"));
+  const Data* pieceHashData =
+    dynamic_cast<const Data*>(infoDic->get(BtContext::PIECES));
   if(!pieceHashData) {
     throw DlAbortEx
       (StringFormat(MSG_SOMETHING_MISSING_IN_TORRENT, "pieces").str());
@@ -316,7 +323,8 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
     throw DlAbortEx("The number of pieces is 0.");
   }
   // retrieve piece length
-  const Data* pieceLengthData = dynamic_cast<const Data*>(infoDic->get("piece length"));
+  const Data* pieceLengthData =
+    dynamic_cast<const Data*>(infoDic->get(BtContext::PIECE_LENGTH));
   if(!pieceLengthData) {
     throw DlAbortEx
       (StringFormat(MSG_SOMETHING_MISSING_IN_TORRENT, "piece length").str());
@@ -325,9 +333,10 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
   // retrieve piece hashes
   extractPieceHash(pieceHashData->getData(), pieceHashData->getLen(),
 		   PIECE_HASH_LENGTH);
-  const Data* privateFlag = dynamic_cast<const Data*>(infoDic->get("private"));
+  const Data* privateFlag =
+    dynamic_cast<const Data*>(infoDic->get(BtContext::PRIVATE));
   if(privateFlag) {
-    if(privateFlag->toString() == "1") {
+    if(privateFlag->toString() == BtContext::PRIVATE_ON) {
       _private = true;
     }
   }
@@ -335,22 +344,25 @@ void DefaultBtContext::processRootDictionary(const Dictionary* rootDic, const st
   // This implemantation obeys HTTP-Seeding specification:
   // see http://www.getright.com/seedtorrent.html
   std::deque<std::string> urlList;
-  extractUrlList(urlList, rootDic->get("url-list"));
+  extractUrlList(urlList, rootDic->get(BtContext::URL_LIST));
   // retrieve file entries
   extractFileEntries(infoDic, defaultName, urlList);
   if((totalLength+pieceLength-1)/pieceLength != numPieces) {
     throw DlAbortEx("Too few/many piece hash.");
   }
   // retrieve announce
-  const Data* announceData = dynamic_cast<const Data*>(rootDic->get("announce"));
-  const List* announceListData = dynamic_cast<const List*>(rootDic->get("announce-list"));
+  const Data* announceData =
+    dynamic_cast<const Data*>(rootDic->get(BtContext::ANNOUNCE));
+  const List* announceListData =
+    dynamic_cast<const List*>(rootDic->get(BtContext::ANNOUNCE_LIST));
   if(announceListData) {
     extractAnnounceList(announceListData);
   } else if(announceData) {
     extractAnnounce(announceData);
   }
   // retrieve nodes
-  const List* nodes = dynamic_cast<const List*>(rootDic->get("nodes"));
+  const List* nodes =
+    dynamic_cast<const List*>(rootDic->get(BtContext::NODES));
   if(nodes) {
     extractNodes(nodes);
   }

+ 2 - 0
src/DefaultBtContext.h

@@ -177,6 +177,8 @@ private:
   void setRandomizer(const SharedHandle<Randomizer>& randomizer);
 
   friend std::ostream& operator<<(std::ostream& o, const DefaultBtContext& ctx);
+
+  static const std::string DEFAULT_PEER_ID_PREFIX;
 };
 
 typedef SharedHandle<DefaultBtContext> DefaultBtContextHandle;

+ 1 - 1
src/Makefile.am

@@ -232,7 +232,7 @@ SRCS += MetaEntry.h\
 	DelegatingPeerListProcessor.cc DelegatingPeerListProcessor.h\
 	AnnounceTier.h\
 	AnnounceList.h AnnounceList.cc\
-	BtContext.h\
+	BtContext.cc BtContext.h\
 	DefaultBtContext.cc DefaultBtContext.h\
 	PeerStorage.h\
 	DefaultPeerStorage.cc DefaultPeerStorage.h\

+ 4 - 2
src/Makefile.in

@@ -76,7 +76,7 @@ bin_PROGRAMS = aria2c$(EXEEXT)
 @ENABLE_BITTORRENT_TRUE@	DelegatingPeerListProcessor.cc DelegatingPeerListProcessor.h\
 @ENABLE_BITTORRENT_TRUE@	AnnounceTier.h\
 @ENABLE_BITTORRENT_TRUE@	AnnounceList.h AnnounceList.cc\
-@ENABLE_BITTORRENT_TRUE@	BtContext.h\
+@ENABLE_BITTORRENT_TRUE@	BtContext.cc BtContext.h\
 @ENABLE_BITTORRENT_TRUE@	DefaultBtContext.cc DefaultBtContext.h\
 @ENABLE_BITTORRENT_TRUE@	PeerStorage.h\
 @ENABLE_BITTORRENT_TRUE@	DefaultPeerStorage.cc DefaultPeerStorage.h\
@@ -433,7 +433,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
 	DefaultPeerListProcessor.h CompactPeerListProcessor.cc \
 	CompactPeerListProcessor.h DelegatingPeerListProcessor.cc \
 	DelegatingPeerListProcessor.h AnnounceTier.h AnnounceList.h \
-	AnnounceList.cc BtContext.h DefaultBtContext.cc \
+	AnnounceList.cc BtContext.cc BtContext.h DefaultBtContext.cc \
 	DefaultBtContext.h PeerStorage.h DefaultPeerStorage.cc \
 	DefaultPeerStorage.h BtAnnounce.cc BtAnnounce.h \
 	DefaultBtAnnounce.cc DefaultBtAnnounce.h BtRuntime.h \
@@ -590,6 +590,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
 @ENABLE_BITTORRENT_TRUE@	CompactPeerListProcessor.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	DelegatingPeerListProcessor.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	AnnounceList.$(OBJEXT) \
+@ENABLE_BITTORRENT_TRUE@	BtContext.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	DefaultBtContext.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	DefaultPeerStorage.$(OBJEXT) \
 @ENABLE_BITTORRENT_TRUE@	BtAnnounce.$(OBJEXT) \
@@ -1252,6 +1253,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtCancelMessage.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtCheckIntegrityEntry.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtChokeMessage.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtContext.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtContextAwareCommand.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtDependency.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/BtExtendedMessage.Po@am__quote@