Implement hash checking.
[tairent.git] / src / main / torrentmanager.h
blobfc3c100cdf6c1ff8816f9378ed0765a5119e1681
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 TorrentClient;
43 /** \brief Holds informations about managed torrents.
45 * This class is a singleton.
47 struct TorrentStruct
49 /** Client for this torrent. If there is no such client then it is zero.
51 TorrentClient *client;
53 /** Meta info of the torrent. If the metainfo has not been loaded yet then
54 * it is zero.
56 Tairent::Core::BEncode *metaInfo;
58 /** Whether this torrent is completely downloaded.
60 bool complete;
62 /** Filename of torrent.
64 String torrentFile;
67 /** \brief TorrentManager keeps informations about managed torrents.
69 class TorrentManager
71 public:
72 /** Constructs a TorrentManager object; calls loadTorrent().
74 TorrentManager();
76 /** Destroys the object.
78 ~TorrentManager();
80 /** Deletes all active clients.
82 void destroy();
84 TorrentClient *getClient(const String &infoHash);
86 /** Returns client ID.
88 const String &getClientID();
90 /** Converts data in hex to its binary original.
92 static String hexToBin(const String &data);
94 /** Returns pointer to the instance of this class.
96 static TorrentManager *self() {
97 return torrentManager;
100 /** Starts downloading unfinished torrents.
102 void startTorrents();
104 /** Stops downloading torrents.
106 void stopTorrents();
108 private:
109 /** Helper method for converting hex char to an integer.
111 static char hexToBin(char c);
113 /** Loads metainfo from a file with given filename. A new
114 * Tairent::Core::BEncode object is created and filled with
115 * informations from the file. Zero is returned if any error occurs
116 * while loading.
118 Tairent::Core::BEncode *loadMetaInfo(const String &filename);
120 /** Loads informations about one torrent from a node.
122 * \param t Element with informations about torrent.
124 void loadTorrent(TiXmlNode *t);
126 /** Loads informations about torrents from file torrents.xml stored in
127 * a directory whose name is stored in Tairon::Core::Config as
128 * "torrents-directory".
130 void loadTorrents();
132 /** Creates client ID.
134 void makeClientID();
136 private:
137 /** Client ID.
139 String clientID;
141 /** Pointer to the instance of this class.
143 static TorrentManager *torrentManager;
145 /** Mapping hash (raw, not human-readable) -> info about torrent.
147 std::map<String, TorrentStruct *> torrents;
150 }; // namespace Main
152 }; // namespace Tairent
154 #endif
156 // vim: ai sw=4 ts=4 noet fdm=marker