TextureDownloadModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Threading;
  30. using OpenMetaverse;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Scenes;
  35. using OpenSim.Framework.Communications.Cache;
  36. using BlockingQueue = OpenSim.Framework.BlockingQueue<OpenSim.Region.Environment.Interfaces.ITextureSender>;
  37. namespace OpenSim.Region.Environment.Modules.Agent.TextureDownload
  38. {
  39. public class TextureDownloadModule : IRegionModule
  40. {
  41. private static readonly log4net.ILog m_log
  42. = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  43. /// <summary>
  44. /// There is one queue for all textures waiting to be sent, regardless of the requesting user.
  45. /// </summary>
  46. private readonly OpenSim.Framework.BlockingQueue<ITextureSender> m_queueSenders
  47. = new OpenSim.Framework.BlockingQueue<ITextureSender>();
  48. /// <summary>
  49. /// Each user has their own texture download service.
  50. /// </summary>
  51. private readonly Dictionary<UUID, UserTextureDownloadService> m_userTextureServices =
  52. new Dictionary<UUID, UserTextureDownloadService>();
  53. private Scene m_scene;
  54. private List<Scene> m_scenes = new List<Scene>();
  55. private Thread m_thread;
  56. public TextureDownloadModule()
  57. {
  58. }
  59. #region IRegionModule Members
  60. public void Initialise(Scene scene, IConfigSource config)
  61. {
  62. if (m_scene == null)
  63. {
  64. //Console.WriteLine("Creating Texture download module");
  65. m_scene = scene;
  66. m_thread = new Thread(new ThreadStart(ProcessTextureSenders));
  67. m_thread.Name = "ProcessTextureSenderThread";
  68. m_thread.IsBackground = true;
  69. m_thread.Start();
  70. ThreadTracker.Add(m_thread);
  71. }
  72. if (!m_scenes.Contains(scene))
  73. {
  74. m_scenes.Add(scene);
  75. m_scene = scene;
  76. m_scene.EventManager.OnNewClient += NewClient;
  77. m_scene.EventManager.OnRemovePresence += EventManager_OnRemovePresence;
  78. }
  79. }
  80. public void PostInitialise()
  81. {
  82. }
  83. public void Close()
  84. {
  85. }
  86. public string Name
  87. {
  88. get { return "TextureDownloadModule"; }
  89. }
  90. public bool IsSharedModule
  91. {
  92. get { return false; }
  93. }
  94. #endregion
  95. /// <summary>
  96. /// Cleanup the texture service related objects for the removed presence.
  97. /// </summary>
  98. /// <param name="agentId"> </param>
  99. private void EventManager_OnRemovePresence(UUID agentId)
  100. {
  101. UserTextureDownloadService textureService;
  102. lock (m_userTextureServices)
  103. {
  104. if (m_userTextureServices.TryGetValue(agentId, out textureService))
  105. {
  106. textureService.Close();
  107. //m_log.DebugFormat("[TEXTURE MODULE]: Removing UserTextureServices from {0}", m_scene.RegionInfo.RegionName);
  108. m_userTextureServices.Remove(agentId);
  109. }
  110. }
  111. }
  112. public void NewClient(IClientAPI client)
  113. {
  114. UserTextureDownloadService textureService;
  115. lock (m_userTextureServices)
  116. {
  117. if (m_userTextureServices.TryGetValue(client.AgentId, out textureService))
  118. {
  119. textureService.Close();
  120. //m_log.DebugFormat("[TEXTURE MODULE]: Removing outdated UserTextureServices from {0}", m_scene.RegionInfo.RegionName);
  121. m_userTextureServices.Remove(client.AgentId);
  122. }
  123. m_userTextureServices.Add(client.AgentId, new UserTextureDownloadService(client, m_scene, m_queueSenders));
  124. }
  125. client.OnRequestTexture += TextureRequest;
  126. }
  127. /// I'm commenting this out, and replacing it with the implementation below, which
  128. /// may return a null value. This is necessary for avoiding race conditions
  129. /// recreating UserTextureServices for clients that have just been closed.
  130. /// That behavior of always returning a UserTextureServices was causing the
  131. /// A-B-A problem (mantis #2855).
  132. ///
  133. ///// <summary>
  134. ///// Does this user have a registered texture download service?
  135. ///// </summary>
  136. ///// <param name="userID"></param>
  137. ///// <param name="textureService"></param>
  138. ///// <returns>Always returns true, since a service is created if one does not already exist</returns>
  139. //private bool TryGetUserTextureService(
  140. // IClientAPI client, out UserTextureDownloadService textureService)
  141. //{
  142. // lock (m_userTextureServices)
  143. // {
  144. // if (m_userTextureServices.TryGetValue(client.AgentId, out textureService))
  145. // {
  146. // //m_log.DebugFormat("[TEXTURE MODULE]: Found existing UserTextureServices in ", m_scene.RegionInfo.RegionName);
  147. // return true;
  148. // }
  149. // m_log.DebugFormat("[TEXTURE MODULE]: Creating new UserTextureServices in ", m_scene.RegionInfo.RegionName);
  150. // textureService = new UserTextureDownloadService(client, m_scene, m_queueSenders);
  151. // m_userTextureServices.Add(client.AgentId, textureService);
  152. // return true;
  153. // }
  154. //}
  155. /// <summary>
  156. /// Does this user have a registered texture download service?
  157. /// </summary>
  158. /// <param name="userID"></param>
  159. /// <param name="textureService"></param>
  160. /// <returns>A UserTextureDownloadService or null in the output parameter, and true or false accordingly.</returns>
  161. private bool TryGetUserTextureService(IClientAPI client, out UserTextureDownloadService textureService)
  162. {
  163. lock (m_userTextureServices)
  164. {
  165. if (m_userTextureServices.TryGetValue(client.AgentId, out textureService))
  166. {
  167. //m_log.DebugFormat("[TEXTURE MODULE]: Found existing UserTextureServices in ", m_scene.RegionInfo.RegionName);
  168. return true;
  169. }
  170. textureService = null;
  171. return false;
  172. }
  173. }
  174. /// <summary>
  175. /// Start the process of requesting a given texture.
  176. /// </summary>
  177. /// <param name="sender"> </param>
  178. /// <param name="e"></param>
  179. public void TextureRequest(Object sender, TextureRequestArgs e)
  180. {
  181. IClientAPI client = (IClientAPI)sender;
  182. if (e.Priority == 1016001f) // Preview
  183. {
  184. if (client.Scene is Scene)
  185. {
  186. Scene scene = (Scene)client.Scene;
  187. CachedUserInfo profile = scene.CommsManager.UserProfileCacheService.GetUserDetails(client.AgentId);
  188. if (profile == null) // Deny unknown user
  189. return;
  190. if (profile.RootFolder == null) // Deny no inventory
  191. return;
  192. if (profile.UserProfile.GodLevel < 200 && profile.RootFolder.FindAsset(e.RequestedAssetID) == null) // Deny if not owned
  193. return;
  194. m_log.Debug("Texture preview");
  195. }
  196. }
  197. UserTextureDownloadService textureService;
  198. if (TryGetUserTextureService(client, out textureService))
  199. {
  200. textureService.HandleTextureRequest(e);
  201. }
  202. }
  203. /// <summary>
  204. /// Entry point for the thread dedicated to processing the texture queue.
  205. /// </summary>
  206. public void ProcessTextureSenders()
  207. {
  208. ITextureSender sender = null;
  209. try
  210. {
  211. while (true)
  212. {
  213. sender = m_queueSenders.Dequeue();
  214. if (sender.Cancel)
  215. {
  216. TextureSent(sender);
  217. sender.Cancel = false;
  218. }
  219. else
  220. {
  221. bool finished = sender.SendTexturePacket();
  222. if (finished)
  223. {
  224. TextureSent(sender);
  225. }
  226. else
  227. {
  228. m_queueSenders.Enqueue(sender);
  229. }
  230. }
  231. // Make sure that any sender we currently have can get garbage collected
  232. sender = null;
  233. //m_log.InfoFormat("[TEXTURE] Texture sender queue size: {0}", m_queueSenders.Count());
  234. }
  235. }
  236. catch (Exception e)
  237. {
  238. // TODO: Let users in the sim and those entering it and possibly an external watchdog know what has happened
  239. m_log.ErrorFormat(
  240. "[TEXTURE]: Texture send thread terminating with exception. PLEASE REBOOT YOUR SIM - TEXTURES WILL NOT BE AVAILABLE UNTIL YOU DO. Exception is {0}",
  241. e);
  242. }
  243. }
  244. /// <summary>
  245. /// Called when the texture has finished sending.
  246. /// </summary>
  247. /// <param name="sender"></param>
  248. private void TextureSent(ITextureSender sender)
  249. {
  250. sender.Sending = false;
  251. //m_log.DebugFormat("[TEXTURE]: Removing download stat for {0}", sender.assetID);
  252. m_scene.AddPendingDownloads(-1);
  253. }
  254. }
  255. }