TextureDownloadModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.Reflection;
  30. using System.Threading;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Framework.Communications.Cache;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. using BlockingQueue = OpenSim.Framework.BlockingQueue<OpenSim.Region.Framework.Interfaces.ITextureSender>;
  39. namespace OpenSim.Region.CoreModules.Agent.TextureDownload
  40. {
  41. public class TextureDownloadModule : IRegionModule
  42. {
  43. private static readonly ILog m_log
  44. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. /// <summary>
  46. /// There is one queue for all textures waiting to be sent, regardless of the requesting user.
  47. /// </summary>
  48. private readonly BlockingQueue m_queueSenders
  49. = new BlockingQueue();
  50. /// <summary>
  51. /// Each user has their own texture download service.
  52. /// </summary>
  53. private readonly Dictionary<UUID, UserTextureDownloadService> m_userTextureServices =
  54. new Dictionary<UUID, UserTextureDownloadService>();
  55. private Scene m_scene;
  56. private List<Scene> m_scenes = new List<Scene>();
  57. private Thread m_thread;
  58. public TextureDownloadModule()
  59. {
  60. }
  61. #region IRegionModule Members
  62. public void Initialise(Scene scene, IConfigSource config)
  63. {
  64. if (m_scene == null)
  65. {
  66. //m_log.Debug("Creating Texture download module");
  67. m_scene = scene;
  68. m_thread = new Thread(new ThreadStart(ProcessTextureSenders));
  69. m_thread.Name = "ProcessTextureSenderThread";
  70. m_thread.IsBackground = true;
  71. m_thread.Start();
  72. ThreadTracker.Add(m_thread);
  73. }
  74. if (!m_scenes.Contains(scene))
  75. {
  76. m_scenes.Add(scene);
  77. m_scene = scene;
  78. m_scene.EventManager.OnNewClient += NewClient;
  79. m_scene.EventManager.OnRemovePresence += EventManager_OnRemovePresence;
  80. }
  81. }
  82. public void PostInitialise()
  83. {
  84. }
  85. public void Close()
  86. {
  87. }
  88. public string Name
  89. {
  90. get { return "TextureDownloadModule"; }
  91. }
  92. public bool IsSharedModule
  93. {
  94. get { return false; }
  95. }
  96. #endregion
  97. /// <summary>
  98. /// Cleanup the texture service related objects for the removed presence.
  99. /// </summary>
  100. /// <param name="agentId"> </param>
  101. private void EventManager_OnRemovePresence(UUID agentId)
  102. {
  103. UserTextureDownloadService textureService;
  104. lock (m_userTextureServices)
  105. {
  106. if (m_userTextureServices.TryGetValue(agentId, out textureService))
  107. {
  108. textureService.Close();
  109. //m_log.DebugFormat("[TEXTURE MODULE]: Removing UserTextureServices from {0}", m_scene.RegionInfo.RegionName);
  110. m_userTextureServices.Remove(agentId);
  111. }
  112. }
  113. }
  114. public void NewClient(IClientAPI client)
  115. {
  116. UserTextureDownloadService textureService;
  117. lock (m_userTextureServices)
  118. {
  119. if (m_userTextureServices.TryGetValue(client.AgentId, out textureService))
  120. {
  121. textureService.Close();
  122. //m_log.DebugFormat("[TEXTURE MODULE]: Removing outdated UserTextureServices from {0}", m_scene.RegionInfo.RegionName);
  123. m_userTextureServices.Remove(client.AgentId);
  124. }
  125. m_userTextureServices.Add(client.AgentId, new UserTextureDownloadService(client, m_scene, m_queueSenders));
  126. }
  127. client.OnRequestTexture += TextureRequest;
  128. }
  129. /// I'm commenting this out, and replacing it with the implementation below, which
  130. /// may return a null value. This is necessary for avoiding race conditions
  131. /// recreating UserTextureServices for clients that have just been closed.
  132. /// That behavior of always returning a UserTextureServices was causing the
  133. /// A-B-A problem (mantis #2855).
  134. ///
  135. ///// <summary>
  136. ///// Does this user have a registered texture download service?
  137. ///// </summary>
  138. ///// <param name="userID"></param>
  139. ///// <param name="textureService"></param>
  140. ///// <returns>Always returns true, since a service is created if one does not already exist</returns>
  141. //private bool TryGetUserTextureService(
  142. // IClientAPI client, out UserTextureDownloadService textureService)
  143. //{
  144. // lock (m_userTextureServices)
  145. // {
  146. // if (m_userTextureServices.TryGetValue(client.AgentId, out textureService))
  147. // {
  148. // //m_log.DebugFormat("[TEXTURE MODULE]: Found existing UserTextureServices in ", m_scene.RegionInfo.RegionName);
  149. // return true;
  150. // }
  151. // m_log.DebugFormat("[TEXTURE MODULE]: Creating new UserTextureServices in ", m_scene.RegionInfo.RegionName);
  152. // textureService = new UserTextureDownloadService(client, m_scene, m_queueSenders);
  153. // m_userTextureServices.Add(client.AgentId, textureService);
  154. // return true;
  155. // }
  156. //}
  157. /// <summary>
  158. /// Does this user have a registered texture download service?
  159. /// </summary>
  160. /// <param name="userID"></param>
  161. /// <param name="textureService"></param>
  162. /// <returns>A UserTextureDownloadService or null in the output parameter, and true or false accordingly.</returns>
  163. private bool TryGetUserTextureService(IClientAPI client, out UserTextureDownloadService textureService)
  164. {
  165. lock (m_userTextureServices)
  166. {
  167. if (m_userTextureServices.TryGetValue(client.AgentId, out textureService))
  168. {
  169. //m_log.DebugFormat("[TEXTURE MODULE]: Found existing UserTextureServices in ", m_scene.RegionInfo.RegionName);
  170. return true;
  171. }
  172. textureService = null;
  173. return false;
  174. }
  175. }
  176. /// <summary>
  177. /// Start the process of requesting a given texture.
  178. /// </summary>
  179. /// <param name="sender"> </param>
  180. /// <param name="e"></param>
  181. public void TextureRequest(Object sender, TextureRequestArgs e)
  182. {
  183. IClientAPI client = (IClientAPI)sender;
  184. if (e.Priority == 1016001f) // Preview
  185. {
  186. if (client.Scene is Scene)
  187. {
  188. Scene scene = (Scene)client.Scene;
  189. CachedUserInfo profile = scene.CommsManager.UserProfileCacheService.GetUserDetails(client.AgentId);
  190. if (profile == null) // Deny unknown user
  191. return;
  192. if (profile.RootFolder == null) // Deny no inventory
  193. return;
  194. if (profile.UserProfile.GodLevel < 200 && profile.RootFolder.FindAsset(e.RequestedAssetID) == null) // Deny if not owned
  195. return;
  196. m_log.Debug("Texture preview");
  197. }
  198. }
  199. UserTextureDownloadService textureService;
  200. if (TryGetUserTextureService(client, out textureService))
  201. {
  202. textureService.HandleTextureRequest(e);
  203. }
  204. }
  205. /// <summary>
  206. /// Entry point for the thread dedicated to processing the texture queue.
  207. /// </summary>
  208. public void ProcessTextureSenders()
  209. {
  210. ITextureSender sender = null;
  211. try
  212. {
  213. while (true)
  214. {
  215. sender = m_queueSenders.Dequeue();
  216. if (sender.Cancel)
  217. {
  218. TextureSent(sender);
  219. sender.Cancel = false;
  220. }
  221. else
  222. {
  223. bool finished = sender.SendTexturePacket();
  224. if (finished)
  225. {
  226. TextureSent(sender);
  227. }
  228. else
  229. {
  230. m_queueSenders.Enqueue(sender);
  231. }
  232. }
  233. // Make sure that any sender we currently have can get garbage collected
  234. sender = null;
  235. //m_log.InfoFormat("[TEXTURE] Texture sender queue size: {0}", m_queueSenders.Count());
  236. }
  237. }
  238. catch (Exception e)
  239. {
  240. // TODO: Let users in the sim and those entering it and possibly an external watchdog know what has happened
  241. m_log.ErrorFormat(
  242. "[TEXTURE]: Texture send thread terminating with exception. PLEASE REBOOT YOUR SIM - TEXTURES WILL NOT BE AVAILABLE UNTIL YOU DO. Exception is {0}",
  243. e);
  244. }
  245. }
  246. /// <summary>
  247. /// Called when the texture has finished sending.
  248. /// </summary>
  249. /// <param name="sender"></param>
  250. private void TextureSent(ITextureSender sender)
  251. {
  252. sender.Sending = false;
  253. //m_log.DebugFormat("[TEXTURE]: Removing download stat for {0}", sender.assetID);
  254. m_scene.AddPendingDownloads(-1);
  255. }
  256. }
  257. }