clientmedia.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #ifndef CLIENTMEDIA_HEADER
  17. #define CLIENTMEDIA_HEADER
  18. #include "irrlichttypes.h"
  19. #include "filecache.h"
  20. #include <ostream>
  21. #include <map>
  22. #include <set>
  23. #include <vector>
  24. class Client;
  25. struct HTTPFetchResult;
  26. #define MTHASHSET_FILE_SIGNATURE 0x4d544853 // 'MTHS'
  27. #define MTHASHSET_FILE_NAME "index.mth"
  28. class ClientMediaDownloader
  29. {
  30. public:
  31. ClientMediaDownloader();
  32. ~ClientMediaDownloader();
  33. float getProgress() const {
  34. if (m_uncached_count >= 1)
  35. return 1.0 * m_uncached_received_count /
  36. m_uncached_count;
  37. else
  38. return 0.0;
  39. }
  40. bool isStarted() const {
  41. return m_initial_step_done;
  42. }
  43. // If this returns true, the downloader is done and can be deleted
  44. bool isDone() const {
  45. return m_initial_step_done &&
  46. m_uncached_received_count == m_uncached_count;
  47. }
  48. // Add a file to the list of required file (but don't fetch it yet)
  49. void addFile(std::string name, std::string sha1);
  50. // Add a remote server to the list; ignored if not built with cURL
  51. void addRemoteServer(std::string baseurl);
  52. // Steps the media downloader:
  53. // - May load media into client by calling client->loadMedia()
  54. // - May check media cache for files
  55. // - May add files to media cache
  56. // - May start remote transfers by calling httpfetch_async
  57. // - May check for completion of current remote transfers
  58. // - May start conventional transfers by calling client->request_media()
  59. // - May inform server that all media has been loaded
  60. // by calling client->received_media()
  61. // After step has been called once, don't call addFile/addRemoteServer.
  62. void step(Client *client);
  63. // Must be called for each file received through TOCLIENT_MEDIA
  64. void conventionalTransferDone(
  65. const std::string &name,
  66. const std::string &data,
  67. Client *client);
  68. private:
  69. struct FileStatus {
  70. bool received;
  71. std::string sha1;
  72. s32 current_remote;
  73. std::vector<s32> available_remotes;
  74. };
  75. struct RemoteServerStatus {
  76. std::string baseurl;
  77. s32 active_count;
  78. bool request_by_filename;
  79. };
  80. void initialStep(Client *client);
  81. void remoteHashSetReceived(const HTTPFetchResult &fetch_result);
  82. void remoteMediaReceived(const HTTPFetchResult &fetch_result,
  83. Client *client);
  84. s32 selectRemoteServer(FileStatus *filestatus);
  85. void startRemoteMediaTransfers();
  86. void startConventionalTransfers(Client *client);
  87. bool checkAndLoad(const std::string &name, const std::string &sha1,
  88. const std::string &data, bool is_from_cache,
  89. Client *client);
  90. std::string serializeRequiredHashSet();
  91. static void deSerializeHashSet(const std::string &data,
  92. std::set<std::string> &result);
  93. // Maps filename to file status
  94. std::map<std::string, FileStatus*> m_files;
  95. // Array of remote media servers
  96. std::vector<RemoteServerStatus*> m_remotes;
  97. // Filesystem-based media cache
  98. FileCache m_media_cache;
  99. // Has an attempt been made to load media files from the file cache?
  100. // Have hash sets been requested from remote servers?
  101. bool m_initial_step_done;
  102. // Total number of media files to load
  103. s32 m_uncached_count;
  104. // Number of media files that have been received
  105. s32 m_uncached_received_count;
  106. // Status of remote transfers
  107. unsigned long m_httpfetch_caller;
  108. unsigned long m_httpfetch_next_id;
  109. long m_httpfetch_timeout;
  110. s32 m_httpfetch_active;
  111. s32 m_httpfetch_active_limit;
  112. s32 m_outstanding_hash_sets;
  113. std::map<unsigned long, std::string> m_remote_file_transfers;
  114. // All files up to this name have either been received from a
  115. // remote server or failed on all remote servers, so those files
  116. // don't need to be looked at again
  117. // (use m_files.upper_bound(m_name_bound) to get an iterator)
  118. std::string m_name_bound;
  119. };
  120. #endif // !CLIENTMEDIA_HEADER