TextureDownloadModule.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 OpenSimulator 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. using OpenSim.Services.Interfaces;
  40. namespace OpenSim.Region.CoreModules.Agent.TextureDownload
  41. {
  42. public class TextureDownloadModule : IRegionModule
  43. {
  44. private static readonly ILog m_log
  45. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. /// <summary>
  47. /// There is one queue for all textures waiting to be sent, regardless of the requesting user.
  48. /// </summary>
  49. private readonly BlockingQueue m_queueSenders
  50. = new BlockingQueue();
  51. /// <summary>
  52. /// Each user has their own texture download service.
  53. /// </summary>
  54. private readonly Dictionary<UUID, UserTextureDownloadService> m_userTextureServices =
  55. new Dictionary<UUID, UserTextureDownloadService>();
  56. private Scene m_scene;
  57. private List<Scene> m_scenes = new List<Scene>();
  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. IInventoryService invService = scene.InventoryService;
  193. if (invService.GetRootFolder(client.AgentId) == null) // Deny no inventory
  194. return;
  195. // Diva 2009-08-13: this test doesn't make any sense to many devs
  196. //if (profile.UserProfile.GodLevel < 200 && profile.RootFolder.FindAsset(e.RequestedAssetID) == null) // Deny if not owned
  197. //{
  198. // m_log.WarnFormat("[TEXTURE]: user {0} doesn't have permissions to texture {1}");
  199. // return;
  200. //}
  201. m_log.Debug("Texture preview");
  202. }
  203. }
  204. UserTextureDownloadService textureService;
  205. if (TryGetUserTextureService(client, out textureService))
  206. {
  207. textureService.HandleTextureRequest(e);
  208. }
  209. }
  210. /// <summary>
  211. /// Entry point for the thread dedicated to processing the texture queue.
  212. /// </summary>
  213. public void ProcessTextureSenders()
  214. {
  215. ITextureSender sender = null;
  216. try
  217. {
  218. while (true)
  219. {
  220. sender = m_queueSenders.Dequeue();
  221. if (sender.Cancel)
  222. {
  223. TextureSent(sender);
  224. sender.Cancel = false;
  225. }
  226. else
  227. {
  228. bool finished = sender.SendTexturePacket();
  229. if (finished)
  230. {
  231. TextureSent(sender);
  232. }
  233. else
  234. {
  235. m_queueSenders.Enqueue(sender);
  236. }
  237. }
  238. // Make sure that any sender we currently have can get garbage collected
  239. sender = null;
  240. //m_log.InfoFormat("[TEXTURE] Texture sender queue size: {0}", m_queueSenders.Count());
  241. }
  242. }
  243. catch (Exception e)
  244. {
  245. // TODO: Let users in the sim and those entering it and possibly an external watchdog know what has happened
  246. m_log.ErrorFormat(
  247. "[TEXTURE]: Texture send thread terminating with exception. PLEASE REBOOT YOUR SIM - TEXTURES WILL NOT BE AVAILABLE UNTIL YOU DO. Exception is {0}",
  248. e);
  249. }
  250. }
  251. /// <summary>
  252. /// Called when the texture has finished sending.
  253. /// </summary>
  254. /// <param name="sender"></param>
  255. private void TextureSent(ITextureSender sender)
  256. {
  257. sender.Sending = false;
  258. //m_log.DebugFormat("[TEXTURE]: Removing download stat for {0}", sender.assetID);
  259. m_scene.StatsReporter.AddPendingDownloads(-1);
  260. }
  261. }
  262. }