TextureDownloadModule.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 libsecondlife;
  31. using Nini.Config;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Scenes;
  35. namespace OpenSim.Region.Environment.Modules.Agent.TextureDownload
  36. {
  37. public class TextureDownloadModule : IRegionModule
  38. {
  39. //private static readonly log4net.ILog m_log
  40. // = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  41. /// <summary>
  42. /// There is one queue for all textures waiting to be sent, regardless of the requesting user.
  43. /// </summary>
  44. private readonly BlockingQueue<ITextureSender> m_queueSenders
  45. = new BlockingQueue<ITextureSender>();
  46. /// <summary>
  47. /// Each user has their own texture download service.
  48. /// </summary>
  49. private readonly Dictionary<LLUUID, UserTextureDownloadService> m_userTextureServices =
  50. new Dictionary<LLUUID, UserTextureDownloadService>();
  51. private Scene m_scene;
  52. private List<Scene> m_scenes = new List<Scene>();
  53. private Thread m_thread;
  54. public TextureDownloadModule()
  55. {
  56. }
  57. #region IRegionModule Members
  58. public void Initialise(Scene scene, IConfigSource config)
  59. {
  60. if (m_scene == null)
  61. {
  62. //Console.WriteLine("Creating Texture download module");
  63. m_thread = new Thread(new ThreadStart(ProcessTextureSenders));
  64. m_thread.Name = "ProcessTextureSenderThread";
  65. m_thread.IsBackground = true;
  66. m_thread.Start();
  67. ThreadTracker.Add(m_thread);
  68. }
  69. if (!m_scenes.Contains(scene))
  70. {
  71. m_scenes.Add(scene);
  72. m_scene = scene;
  73. m_scene.EventManager.OnNewClient += NewClient;
  74. m_scene.EventManager.OnRemovePresence += EventManager_OnRemovePresence;
  75. }
  76. }
  77. public void PostInitialise()
  78. {
  79. }
  80. public void Close()
  81. {
  82. }
  83. public string Name
  84. {
  85. get { return "TextureDownloadModule"; }
  86. }
  87. public bool IsSharedModule
  88. {
  89. get { return false; }
  90. }
  91. #endregion
  92. /// <summary>
  93. /// Cleanup the texture service related objects for the removed presence.
  94. /// </summary>
  95. /// <param name="agentId"> </param>
  96. private void EventManager_OnRemovePresence(LLUUID agentId)
  97. {
  98. UserTextureDownloadService textureService;
  99. lock (m_userTextureServices)
  100. {
  101. if (m_userTextureServices.TryGetValue(agentId, out textureService))
  102. {
  103. textureService.Close();
  104. m_userTextureServices.Remove(agentId);
  105. }
  106. }
  107. }
  108. public void NewClient(IClientAPI client)
  109. {
  110. client.OnRequestTexture += TextureRequest;
  111. }
  112. /// <summary>
  113. /// Does this user have a registered texture download service?
  114. /// </summary>
  115. /// <param name="userID"></param>
  116. /// <param name="textureService"></param>
  117. /// <returns>Always returns true, since a service is created if one does not already exist</returns>
  118. private bool TryGetUserTextureService(
  119. IClientAPI client, out UserTextureDownloadService textureService)
  120. {
  121. lock (m_userTextureServices)
  122. {
  123. if (m_userTextureServices.TryGetValue(client.AgentId, out textureService))
  124. {
  125. return true;
  126. }
  127. textureService = new UserTextureDownloadService(client, m_scene, m_queueSenders);
  128. m_userTextureServices.Add(client.AgentId, textureService);
  129. return true;
  130. }
  131. }
  132. /// <summary>
  133. /// Start the process of requesting a given texture.
  134. /// </summary>
  135. /// <param name="sender"> </param>
  136. /// <param name="e"></param>
  137. public void TextureRequest(Object sender, TextureRequestArgs e)
  138. {
  139. IClientAPI client = (IClientAPI) sender;
  140. UserTextureDownloadService textureService;
  141. if (TryGetUserTextureService(client, out textureService))
  142. {
  143. textureService.HandleTextureRequest(e);
  144. }
  145. }
  146. /// <summary>
  147. /// Entry point for the thread dedicated to processing the texture queue.
  148. /// </summary>
  149. public void ProcessTextureSenders()
  150. {
  151. ITextureSender sender = null;
  152. while (true)
  153. {
  154. sender = m_queueSenders.Dequeue();
  155. if (sender.Cancel)
  156. {
  157. TextureSent(sender);
  158. sender.Cancel = false;
  159. }
  160. else
  161. {
  162. bool finished = sender.SendTexturePacket();
  163. if (finished)
  164. {
  165. TextureSent(sender);
  166. }
  167. else
  168. {
  169. m_queueSenders.Enqueue(sender);
  170. }
  171. }
  172. // Make sure that any sender we currently have can get garbage collected
  173. sender = null;
  174. //m_log.InfoFormat("[TEXTURE DOWNLOAD] Texture sender queue size: {0}", m_queueSenders.Count());
  175. }
  176. }
  177. /// <summary>
  178. /// Called when the texture has finished sending.
  179. /// </summary>
  180. /// <param name="sender"></param>
  181. private void TextureSent(ITextureSender sender)
  182. {
  183. sender.Sending = false;
  184. //m_log.DebugFormat("[TEXTURE DOWNLOAD]: Removing download stat for {0}", sender.assetID);
  185. m_scene.AddPendingDownloads(-1);
  186. }
  187. }
  188. }