1 /***************************************************************************
3 * Copyright (C) 2006-2007 David Brodsky *
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. *
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. *
15 ***************************************************************************/
17 #ifndef _main_torrentmanager_h
18 #define _main_torrentmanager_h
22 #include <tairon/core/string.h>
24 using Tairon::Core::String
;
38 }; // namespace Tairon
56 /** \brief Holds informations about managed torrents.
58 * This class is a singleton.
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.
70 /** Meta info of the torrent. If the metainfo has not been loaded yet then
73 Tairent::Core::BEncode
*metaInfo
;
75 /** Whether this torrent is completely downloaded.
79 /** Filename of torrent.
84 /** \brief TorrentManager keeps informations about managed torrents.
89 /** Constructs a TorrentManager object; calls loadTorrent().
93 /** Destroys the object.
97 /** Converts data in bin to its hexadecimal variant.
99 static String
binToHex(const String
&data
);
101 /** Deletes all active clients.
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() {
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
131 bool isPrivate(const Tairent::Core::BEncode
&metaInfo
);
133 /** Saves informations about managed torrents to resume.xml file in
134 * torrents-directory configuration option.
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.
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
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
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".
189 /** Creates client ID.
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
;
213 }; // namespace Tairent
217 // vim: ai sw=4 ts=4 noet fdm=marker