UTMetadataDataExtensionMessage.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2009 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "UTMetadataDataExtensionMessage.h"
  36. #include "bencode2.h"
  37. #include "util.h"
  38. #include "a2functional.h"
  39. #include "DownloadContext.h"
  40. #include "UTMetadataRequestTracker.h"
  41. #include "PieceStorage.h"
  42. #include "BtConstants.h"
  43. #include "MessageDigest.h"
  44. #include "message_digest_helper.h"
  45. #include "bittorrent_helper.h"
  46. #include "DiskAdaptor.h"
  47. #include "Piece.h"
  48. #include "Logger.h"
  49. #include "LogFactory.h"
  50. #include "DlAbortEx.h"
  51. #include "fmt.h"
  52. namespace aria2 {
  53. UTMetadataDataExtensionMessage::UTMetadataDataExtensionMessage
  54. (uint8_t extensionMessageID)
  55. : UTMetadataExtensionMessage(extensionMessageID),
  56. tracker_(0)
  57. {}
  58. UTMetadataDataExtensionMessage::~UTMetadataDataExtensionMessage() {}
  59. std::string UTMetadataDataExtensionMessage::getPayload()
  60. {
  61. Dict dict;
  62. dict.put("msg_type", Integer::g(1));
  63. dict.put("piece", Integer::g(getIndex()));
  64. dict.put("total_size", Integer::g(totalSize_));
  65. return bencode2::encode(&dict)+data_;
  66. }
  67. std::string UTMetadataDataExtensionMessage::toString() const
  68. {
  69. return strconcat("ut_metadata data piece=", util::uitos(getIndex()));
  70. }
  71. void UTMetadataDataExtensionMessage::doReceivedAction()
  72. {
  73. if(tracker_->tracks(getIndex())) {
  74. A2_LOG_DEBUG(fmt("ut_metadata index=%lu found in tracking list",
  75. static_cast<unsigned long>(getIndex())));
  76. tracker_->remove(getIndex());
  77. pieceStorage_->getDiskAdaptor()->writeData
  78. (reinterpret_cast<const unsigned char*>(data_.c_str()), data_.size(),
  79. getIndex()*METADATA_PIECE_SIZE);
  80. pieceStorage_->completePiece(pieceStorage_->getPiece(getIndex()));
  81. if(pieceStorage_->downloadFinished()) {
  82. std::string metadata = util::toString(pieceStorage_->getDiskAdaptor());
  83. unsigned char infoHash[INFO_HASH_LENGTH];
  84. message_digest::digest(infoHash, INFO_HASH_LENGTH,
  85. MessageDigest::sha1(),
  86. metadata.data(), metadata.size());
  87. if(memcmp(infoHash, bittorrent::getInfoHash(dctx_),
  88. INFO_HASH_LENGTH) == 0) {
  89. A2_LOG_INFO("Got ut_metadata");
  90. } else {
  91. A2_LOG_INFO("Got wrong ut_metadata");
  92. for(size_t i = 0; i < dctx_->getNumPieces(); ++i) {
  93. pieceStorage_->markPieceMissing(i);
  94. }
  95. throw DL_ABORT_EX("Got wrong ut_metadata");
  96. }
  97. }
  98. } else {
  99. A2_LOG_DEBUG(fmt("ut_metadata index=%lu is not tracked",
  100. static_cast<unsigned long>(getIndex())));
  101. }
  102. }
  103. void UTMetadataDataExtensionMessage::setData(const std::string& data)
  104. {
  105. data_ = data;
  106. }
  107. void UTMetadataDataExtensionMessage::setPieceStorage
  108. (const SharedHandle<PieceStorage>& pieceStorage)
  109. {
  110. pieceStorage_ = pieceStorage;
  111. }
  112. void UTMetadataDataExtensionMessage::setDownloadContext
  113. (const SharedHandle<DownloadContext>& dctx)
  114. {
  115. dctx_ = dctx;
  116. }
  117. } // namespace aria2