clientmedia.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes.h"
  18. #include "filecache.h"
  19. #include "util/basic_macros.h"
  20. #include <ostream>
  21. #include <map>
  22. #include <set>
  23. #include <vector>
  24. #include <unordered_map>
  25. class Client;
  26. struct HTTPFetchResult;
  27. #define MTHASHSET_FILE_SIGNATURE 0x4d544853 // 'MTHS'
  28. #define MTHASHSET_FILE_NAME "index.mth"
  29. // Store file into media cache (unless it exists already)
  30. // Validating the hash is responsibility of the caller
  31. bool clientMediaUpdateCache(const std::string &raw_hash,
  32. const std::string &filedata);
  33. // more of a base class than an interface but this name was most convenient...
  34. class IClientMediaDownloader
  35. {
  36. public:
  37. DISABLE_CLASS_COPY(IClientMediaDownloader)
  38. virtual bool isStarted() const = 0;
  39. // If this returns true, the downloader is done and can be deleted
  40. virtual bool isDone() const = 0;
  41. // Add a file to the list of required file (but don't fetch it yet)
  42. virtual void addFile(const std::string &name, const std::string &sha1) = 0;
  43. // Add a remote server to the list; ignored if not built with cURL
  44. virtual void addRemoteServer(const std::string &baseurl) = 0;
  45. // Steps the media downloader:
  46. // - May load media into client by calling client->loadMedia()
  47. // - May check media cache for files
  48. // - May add files to media cache
  49. // - May start remote transfers by calling httpfetch_async
  50. // - May check for completion of current remote transfers
  51. // - May start conventional transfers by calling client->request_media()
  52. // - May inform server that all media has been loaded
  53. // by calling client->received_media()
  54. // After step has been called once, don't call addFile/addRemoteServer.
  55. virtual void step(Client *client) = 0;
  56. // Must be called for each file received through TOCLIENT_MEDIA
  57. // returns true if this file belongs to this downloader
  58. virtual bool conventionalTransferDone(const std::string &name,
  59. const std::string &data, Client *client) = 0;
  60. protected:
  61. IClientMediaDownloader();
  62. virtual ~IClientMediaDownloader() = default;
  63. // Forwards the call to the appropriate Client method
  64. virtual bool loadMedia(Client *client, const std::string &data,
  65. const std::string &name) = 0;
  66. void createCacheDirs();
  67. bool tryLoadFromCache(const std::string &name, const std::string &sha1,
  68. Client *client);
  69. bool checkAndLoad(const std::string &name, const std::string &sha1,
  70. const std::string &data, bool is_from_cache, Client *client);
  71. // Filesystem-based media cache
  72. FileCache m_media_cache;
  73. bool m_write_to_cache;
  74. };
  75. class ClientMediaDownloader : public IClientMediaDownloader
  76. {
  77. public:
  78. ClientMediaDownloader();
  79. ~ClientMediaDownloader();
  80. float getProgress() const {
  81. if (m_uncached_count >= 1)
  82. return 1.0f * m_uncached_received_count /
  83. m_uncached_count;
  84. return 0.0f;
  85. }
  86. bool isStarted() const override {
  87. return m_initial_step_done;
  88. }
  89. bool isDone() const override {
  90. return m_initial_step_done &&
  91. m_uncached_received_count == m_uncached_count;
  92. }
  93. void addFile(const std::string &name, const std::string &sha1) override;
  94. void addRemoteServer(const std::string &baseurl) override;
  95. void step(Client *client) override;
  96. bool conventionalTransferDone(
  97. const std::string &name,
  98. const std::string &data,
  99. Client *client) override;
  100. protected:
  101. bool loadMedia(Client *client, const std::string &data,
  102. const std::string &name) override;
  103. private:
  104. struct FileStatus {
  105. bool received;
  106. std::string sha1;
  107. s32 current_remote;
  108. std::vector<s32> available_remotes;
  109. };
  110. struct RemoteServerStatus {
  111. std::string baseurl;
  112. s32 active_count;
  113. };
  114. void initialStep(Client *client);
  115. void remoteHashSetReceived(const HTTPFetchResult &fetch_result);
  116. void remoteMediaReceived(const HTTPFetchResult &fetch_result,
  117. Client *client);
  118. s32 selectRemoteServer(FileStatus *filestatus);
  119. void startRemoteMediaTransfers();
  120. void startConventionalTransfers(Client *client);
  121. static void deSerializeHashSet(const std::string &data,
  122. std::set<std::string> &result);
  123. std::string serializeRequiredHashSet();
  124. // Maps filename to file status
  125. std::map<std::string, FileStatus*> m_files;
  126. // Array of remote media servers
  127. std::vector<RemoteServerStatus*> m_remotes;
  128. // Has an attempt been made to load media files from the file cache?
  129. // Have hash sets been requested from remote servers?
  130. bool m_initial_step_done = false;
  131. // Total number of media files to load
  132. s32 m_uncached_count = 0;
  133. // Number of media files that have been received
  134. s32 m_uncached_received_count = 0;
  135. // Status of remote transfers
  136. unsigned long m_httpfetch_caller;
  137. unsigned long m_httpfetch_next_id = 0;
  138. s32 m_httpfetch_active = 0;
  139. s32 m_httpfetch_active_limit = 0;
  140. s32 m_outstanding_hash_sets = 0;
  141. std::unordered_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 = "";
  147. };
  148. // A media downloader that only downloads a single file.
  149. // It does/doesn't do several things the normal downloader does:
  150. // - won't fetch hash sets from remote servers
  151. // - will mark loaded media as coming from file push
  152. // - writing to file cache is optional
  153. class SingleMediaDownloader : public IClientMediaDownloader
  154. {
  155. public:
  156. SingleMediaDownloader(bool write_to_cache);
  157. ~SingleMediaDownloader();
  158. bool isStarted() const override {
  159. return m_stage > STAGE_INIT;
  160. }
  161. bool isDone() const override {
  162. return m_stage >= STAGE_DONE;
  163. }
  164. void addFile(const std::string &name, const std::string &sha1) override;
  165. void addRemoteServer(const std::string &baseurl) override;
  166. void step(Client *client) override;
  167. bool conventionalTransferDone(const std::string &name,
  168. const std::string &data, Client *client) override;
  169. protected:
  170. bool loadMedia(Client *client, const std::string &data,
  171. const std::string &name) override;
  172. private:
  173. void initialStep(Client *client);
  174. void remoteMediaReceived(const HTTPFetchResult &fetch_result, Client *client);
  175. void startRemoteMediaTransfer();
  176. void startConventionalTransfer(Client *client);
  177. enum Stage {
  178. STAGE_INIT,
  179. STAGE_CACHE_CHECKED, // we have tried to load the file from cache
  180. STAGE_DONE
  181. };
  182. // Information about the one file we want to fetch
  183. std::string m_file_name;
  184. std::string m_file_sha1;
  185. s32 m_current_remote;
  186. // Array of remote media servers
  187. std::vector<std::string> m_remotes;
  188. enum Stage m_stage = STAGE_INIT;
  189. // Status of remote transfers
  190. unsigned long m_httpfetch_caller;
  191. unsigned long m_httpfetch_next_id = 0;
  192. };