3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
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
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef CLIENTMEDIA_HEADER
21 #define CLIENTMEDIA_HEADER
23 #include "irrlichttypes.h"
24 #include "filecache.h"
31 struct HTTPFetchResult;
33 #define MTHASHSET_FILE_SIGNATURE 0x4d544853 // 'MTHS'
34 #define MTHASHSET_FILE_NAME "index.mth"
36 class ClientMediaDownloader
39 ClientMediaDownloader();
40 ~ClientMediaDownloader();
42 float getProgress() const {
43 if (m_uncached_count >= 1)
44 return 1.0 * m_uncached_received_count /
50 bool isStarted() const {
51 return m_initial_step_done;
54 // If this returns true, the downloader is done and can be deleted
56 return m_initial_step_done &&
57 m_uncached_received_count == m_uncached_count;
60 // Add a file to the list of required file (but don't fetch it yet)
61 void addFile(std::string name, std::string sha1);
63 // Add a remote server to the list; ignored if not built with cURL
64 void addRemoteServer(std::string baseurl);
66 // Steps the media downloader:
67 // - May load media into client by calling client->loadMedia()
68 // - May check media cache for files
69 // - May add files to media cache
70 // - May start remote transfers by calling httpfetch_async
71 // - May check for completion of current remote transfers
72 // - May start conventional transfers by calling client->request_media()
73 // - May inform server that all media has been loaded
74 // by calling client->received_media()
75 // After step has been called once, don't call addFile/addRemoteServer.
76 void step(Client *client);
78 // Must be called for each file received through TOCLIENT_MEDIA
79 void conventionalTransferDone(
80 const std::string &name,
81 const std::string &data,
89 std::vector<s32> available_remotes;
92 struct RemoteServerStatus {
95 bool request_by_filename;
98 void initialStep(Client *client);
99 void remoteHashSetReceived(const HTTPFetchResult &fetch_result);
100 void remoteMediaReceived(const HTTPFetchResult &fetch_result,
102 s32 selectRemoteServer(FileStatus *filestatus);
103 void startRemoteMediaTransfers();
104 void startConventionalTransfers(Client *client);
106 bool checkAndLoad(const std::string &name, const std::string &sha1,
107 const std::string &data, bool is_from_cache,
110 std::string serializeRequiredHashSet();
111 static void deSerializeHashSet(const std::string &data,
112 std::set<std::string> &result);
114 // Maps filename to file status
115 std::map<std::string, FileStatus*> m_files;
117 // Array of remote media servers
118 std::vector<RemoteServerStatus*> m_remotes;
120 // Filesystem-based media cache
121 FileCache m_media_cache;
123 // Has an attempt been made to load media files from the file cache?
124 // Have hash sets been requested from remote servers?
125 bool m_initial_step_done;
127 // Total number of media files to load
128 s32 m_uncached_count;
130 // Number of media files that have been received
131 s32 m_uncached_received_count;
133 // Status of remote transfers
134 unsigned long m_httpfetch_caller;
135 unsigned long m_httpfetch_next_id;
136 long m_httpfetch_timeout;
137 s32 m_httpfetch_active;
138 s32 m_httpfetch_active_limit;
139 s32 m_outstanding_hash_sets;
140 std::map<unsigned long, std::string> m_remote_file_transfers;
142 // All files up to this name have either been received from a
143 // remote server or failed on all remote servers, so those files
144 // don't need to be looked at again
145 // (use m_files.upper_bound(m_name_bound) to get an iterator)
146 std::string m_name_bound;
150 #endif // !CLIENTMEDIA_HEADER