Fix Tairent::Main::TorrentManager::save() method's comment.
[tairent.git] / src / main / torrentmanager.h
blob6325683d1be5448e352a0cc10dc063d691fd4b39
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006-2007 David Brodsky *
4 * *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License as *
7 * published by the Free Software Foundation and appearing *
8 * in the file LICENSE.GPL included in the packaging of this file. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13 * General Public License for more details. *
14 * *
15 ***************************************************************************/
17 #ifndef _main_torrentmanager_h
18 #define _main_torrentmanager_h
20 #include <map>
22 #include <tairon/core/string.h>
24 using Tairon::Core::String;
26 class TiXmlNode;
28 namespace Tairon
31 namespace Net
34 class Timer;
36 }; // namespace Net
38 }; // namespace Tairon
40 namespace Tairent
43 namespace Core
46 class BEncode;
48 }; // namespace Core
50 namespace Main
53 class Storage;
54 class TorrentClient;
56 /** \brief Holds informations about managed torrents.
58 * This class is a singleton.
60 struct TorrentStruct
62 /** Client for this torrent. If there is no such client then it is zero.
64 TorrentClient *client;
66 /** This client's storage. It is used for fast-resuming.
68 Storage *storage;
70 /** Meta info of the torrent. If the metainfo has not been loaded yet then
71 * it is zero.
73 Tairent::Core::BEncode *metaInfo;
75 /** Whether this torrent is completely downloaded.
77 bool complete;
79 /** Filename of torrent.
81 String torrentFile;
84 /** \brief TorrentManager keeps informations about managed torrents.
86 class TorrentManager
88 public:
89 /** Constructs a TorrentManager object; calls loadTorrent().
91 TorrentManager();
93 /** Destroys the object.
95 ~TorrentManager();
97 /** Converts data in bin to its hexadecimal variant.
99 static String binToHex(const String &data);
101 /** Deletes all active clients.
103 void destroy();
105 /** Returns a TorrentClient object associated with given info hash. The
106 * method returns 0 if there is no such client.
108 TorrentClient *getClient(const String &infoHash);
110 /** Returns client ID.
112 const String &getClientID();
114 /** Returns TorrentStruct for given info hash.
116 TorrentStruct *getTorrent(const String &infoHash);
118 /** Returns dictionary with torrents.
120 const std::map<String, TorrentStruct *> &getTorrents() {
121 return torrents;
124 /** Converts data in hex to its binary original.
126 static String hexToBin(const String &data);
128 /** Returns true if the torrent has set the private flag; otherwise
129 * returns false.
131 bool isPrivate(const Tairent::Core::BEncode &metaInfo);
133 /** Saves informations about managed torrents to resume.xml file in
134 * torrents-directory configuration option.
136 void save();
138 /** Returns pointer to the instance of this class.
140 static TorrentManager *self() {
141 return torrentManager;
144 /** Starts downloading unfinished torrents.
146 void startTorrents();
148 /** Stops downloading torrents.
150 void stopTorrents();
152 private:
153 /** Helper method for converting hex char to an integer.
155 static char hexToBin(char c);
157 /** Loads metainfo from a file with given filename. A new
158 * Tairent::Core::BEncode object is created and filled with
159 * informations from the file. Zero is returned if any error occurs
160 * while loading.
162 Tairent::Core::BEncode *loadMetaInfo(const String &filename);
164 /** Loads informations about torrents from file resume.xml stored in a
165 * directory whose name is stored in Tairon::Core::Config as
166 * "torrents-directory". This file is created for fast-resuming
167 * torrents.
169 void loadResume();
171 /** Loads informations about one torrent from a node.
173 * \param t Element with informations about torrent.
175 void loadTorrent(TiXmlNode *t);
177 /** Loads fast-resume data about one torrent from a node.
179 * \param n Element with fast-resume data.
181 void loadTorrentResume(TiXmlNode *n);
183 /** Loads informations about torrents from file torrents.xml stored in
184 * a directory whose name is stored in Tairon::Core::Config as
185 * "torrents-directory".
187 void loadTorrents();
189 /** Creates client ID.
191 void makeClientID();
193 private:
194 /** Client ID.
196 String clientID;
198 /** Timer for calling save() method.
200 Tairon::Net::Timer *saveTimer;
202 /** Pointer to the instance of this class.
204 static TorrentManager *torrentManager;
206 /** Mapping hash (raw, not human-readable) -> info about torrent.
208 std::map<String, TorrentStruct *> torrents;
211 }; // namespace Main
213 }; // namespace Tairent
215 #endif
217 // vim: ai sw=4 ts=4 noet fdm=marker