Send correct informations to a http tracker.
[tairent.git] / src / main / torrentmanager.h
blob298b3876267783f7d704a5b665a55df21362881d
1 /***************************************************************************
2 * *
3 * Copyright (C) 2006 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 Tairent
31 namespace Core
34 class BEncode;
36 }; // namespace Core
38 namespace Main
41 class Storage;
42 class TorrentClient;
44 /** \brief Holds informations about managed torrents.
46 * This class is a singleton.
48 struct TorrentStruct
50 /** Client for this torrent. If there is no such client then it is zero.
52 TorrentClient *client;
54 /** This client's storage. It is used for fast-resuming.
56 Storage *storage;
58 /** Meta info of the torrent. If the metainfo has not been loaded yet then
59 * it is zero.
61 Tairent::Core::BEncode *metaInfo;
63 /** Whether this torrent is completely downloaded.
65 bool complete;
67 /** Filename of torrent.
69 String torrentFile;
72 /** \brief TorrentManager keeps informations about managed torrents.
74 class TorrentManager
76 public:
77 /** Constructs a TorrentManager object; calls loadTorrent().
79 TorrentManager();
81 /** Destroys the object.
83 ~TorrentManager();
85 /** Converts data in bin to its hexadecimal variant.
87 static String binToHex(const String &data);
89 /** Deletes all active clients.
91 void destroy();
93 /** Returns a TorrentClient object associated with given info hash. The
94 * method returns 0 if there is no such client.
96 TorrentClient *getClient(const String &infoHash);
98 /** Returns client ID.
100 const String &getClientID();
102 /** Converts data in hex to its binary original.
104 static String hexToBin(const String &data);
106 /** Saves informations about managed torrents to torrents.xml file in
107 * torrents-directory configuration option.
109 void save();
111 /** Returns pointer to the instance of this class.
113 static TorrentManager *self() {
114 return torrentManager;
117 /** Starts downloading unfinished torrents.
119 void startTorrents();
121 /** Stops downloading torrents.
123 void stopTorrents();
125 private:
126 /** Helper method for converting hex char to an integer.
128 static char hexToBin(char c);
130 /** Loads metainfo from a file with given filename. A new
131 * Tairent::Core::BEncode object is created and filled with
132 * informations from the file. Zero is returned if any error occurs
133 * while loading.
135 Tairent::Core::BEncode *loadMetaInfo(const String &filename);
137 /** Loads informations about one torrent from a node.
139 * \param t Element with informations about torrent.
141 void loadTorrent(TiXmlNode *t);
143 /** Loads informations about torrents from file torrents.xml stored in
144 * a directory whose name is stored in Tairon::Core::Config as
145 * "torrents-directory".
147 void loadTorrents();
149 /** Creates client ID.
151 void makeClientID();
153 private:
154 /** Client ID.
156 String clientID;
158 /** Pointer to the instance of this class.
160 static TorrentManager *torrentManager;
162 /** Mapping hash (raw, not human-readable) -> info about torrent.
164 std::map<String, TorrentStruct *> torrents;
167 }; // namespace Main
169 }; // namespace Tairent
171 #endif
173 // vim: ai sw=4 ts=4 noet fdm=marker