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