1
0

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