BtRegistry.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. #ifndef D_BT_REGISTRY_H
  36. #define D_BT_REGISTRY_H
  37. #include "common.h"
  38. #include <map>
  39. #include "SharedHandle.h"
  40. #include "RequestGroup.h"
  41. namespace aria2 {
  42. class PeerStorage;
  43. class PieceStorage;
  44. class BtAnnounce;
  45. class BtRuntime;
  46. class BtProgressInfoFile;
  47. class DownloadContext;
  48. class LpdMessageReceiver;
  49. class UDPTrackerClient;
  50. struct BtObject {
  51. SharedHandle<DownloadContext> downloadContext;
  52. SharedHandle<PieceStorage> pieceStorage;
  53. SharedHandle<PeerStorage> peerStorage;
  54. SharedHandle<BtAnnounce> btAnnounce;
  55. SharedHandle<BtRuntime> btRuntime;
  56. SharedHandle<BtProgressInfoFile> btProgressInfoFile;
  57. BtObject(const SharedHandle<DownloadContext>& downloadContext,
  58. const SharedHandle<PieceStorage>& pieceStorage,
  59. const SharedHandle<PeerStorage>& peerStorage,
  60. const SharedHandle<BtAnnounce>& btAnnounce,
  61. const SharedHandle<BtRuntime>& btRuntime,
  62. const SharedHandle<BtProgressInfoFile>& btProgressInfoFile);
  63. BtObject();
  64. BtObject(const BtObject& c);
  65. ~BtObject();
  66. BtObject& operator=(const BtObject& c);
  67. };
  68. class BtRegistry {
  69. private:
  70. std::map<a2_gid_t, SharedHandle<BtObject> > pool_;
  71. uint16_t tcpPort_;
  72. // This is IPv4 port for DHT and UDP tracker. No IPv6 udpPort atm.
  73. uint16_t udpPort_;
  74. SharedHandle<LpdMessageReceiver> lpdMessageReceiver_;
  75. SharedHandle<UDPTrackerClient> udpTrackerClient_;
  76. public:
  77. BtRegistry();
  78. ~BtRegistry();
  79. const SharedHandle<DownloadContext>&
  80. getDownloadContext(a2_gid_t gid) const;
  81. const SharedHandle<DownloadContext>&
  82. getDownloadContext(const std::string& infoHash) const;
  83. void put(a2_gid_t gid, const SharedHandle<BtObject>& obj);
  84. const SharedHandle<BtObject>& get(a2_gid_t gid) const;
  85. template<typename OutputIterator>
  86. OutputIterator getAllDownloadContext(OutputIterator dest)
  87. {
  88. for(std::map<a2_gid_t, SharedHandle<BtObject> >::const_iterator i =
  89. pool_.begin(), eoi = pool_.end(); i != eoi; ++i) {
  90. *dest++ = (*i).second->downloadContext;
  91. }
  92. return dest;
  93. }
  94. void removeAll();
  95. bool remove(a2_gid_t gid);
  96. void setTcpPort(uint16_t port)
  97. {
  98. tcpPort_ = port;
  99. }
  100. uint16_t getTcpPort() const
  101. {
  102. return tcpPort_;
  103. }
  104. void setUdpPort(uint16_t port)
  105. {
  106. udpPort_ = port;
  107. }
  108. uint16_t getUdpPort() const
  109. {
  110. return udpPort_;
  111. }
  112. void setLpdMessageReceiver(const SharedHandle<LpdMessageReceiver>& receiver);
  113. const SharedHandle<LpdMessageReceiver>& getLpdMessageReceiver() const
  114. {
  115. return lpdMessageReceiver_;
  116. }
  117. void setUDPTrackerClient(const SharedHandle<UDPTrackerClient>& tracker);
  118. const SharedHandle<UDPTrackerClient>& getUDPTrackerClient() const
  119. {
  120. return udpTrackerClient_;
  121. }
  122. };
  123. } // namespace aria2
  124. #endif // D_BT_REGISTRY_H