BtRegistry.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 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 "BtRegistry.h"
  36. #include "DlAbortEx.h"
  37. #include "DownloadContext.h"
  38. #include "PeerStorage.h"
  39. #include "PieceStorage.h"
  40. #include "BtAnnounce.h"
  41. #include "BtRuntime.h"
  42. #include "BtProgressInfoFile.h"
  43. #include "bittorrent_helper.h"
  44. #include "LpdMessageReceiver.h"
  45. #include "UDPTrackerClient.h"
  46. #include "NullHandle.h"
  47. namespace aria2 {
  48. BtRegistry::BtRegistry() : tcpPort_{0}, udpPort_{0} {}
  49. const std::shared_ptr<DownloadContext>&
  50. BtRegistry::getDownloadContext(a2_gid_t gid) const
  51. {
  52. auto res = get(gid);
  53. if (res) {
  54. return res->downloadContext;
  55. }
  56. else {
  57. return getNull<DownloadContext>();
  58. }
  59. }
  60. const std::shared_ptr<DownloadContext>&
  61. BtRegistry::getDownloadContext(const std::string& infoHash) const
  62. {
  63. for (auto& kv : pool_) {
  64. if (bittorrent::getTorrentAttrs(kv.second->downloadContext)->infoHash ==
  65. infoHash) {
  66. return kv.second->downloadContext;
  67. }
  68. }
  69. return getNull<DownloadContext>();
  70. }
  71. void BtRegistry::put(a2_gid_t gid, std::unique_ptr<BtObject> obj)
  72. {
  73. pool_[gid] = std::move(obj);
  74. }
  75. BtObject* BtRegistry::get(a2_gid_t gid) const
  76. {
  77. auto i = pool_.find(gid);
  78. if (i == std::end(pool_)) {
  79. return nullptr;
  80. }
  81. else {
  82. return (*i).second.get();
  83. }
  84. }
  85. bool BtRegistry::remove(a2_gid_t gid) { return pool_.erase(gid); }
  86. void BtRegistry::removeAll() { pool_.clear(); }
  87. void BtRegistry::setLpdMessageReceiver(
  88. const std::shared_ptr<LpdMessageReceiver>& receiver)
  89. {
  90. lpdMessageReceiver_ = receiver;
  91. }
  92. void BtRegistry::setUDPTrackerClient(
  93. const std::shared_ptr<UDPTrackerClient>& tracker)
  94. {
  95. udpTrackerClient_ = tracker;
  96. }
  97. BtObject::BtObject(
  98. const std::shared_ptr<DownloadContext>& downloadContext,
  99. const std::shared_ptr<PieceStorage>& pieceStorage,
  100. const std::shared_ptr<PeerStorage>& peerStorage,
  101. const std::shared_ptr<BtAnnounce>& btAnnounce,
  102. const std::shared_ptr<BtRuntime>& btRuntime,
  103. const std::shared_ptr<BtProgressInfoFile>& btProgressInfoFile)
  104. : downloadContext{downloadContext},
  105. pieceStorage{pieceStorage},
  106. peerStorage{peerStorage},
  107. btAnnounce{btAnnounce},
  108. btRuntime{btRuntime},
  109. btProgressInfoFile{btProgressInfoFile}
  110. {
  111. }
  112. BtObject::BtObject() {}
  113. } // namespace aria2