UserTextureDownloadService.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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.Collections.Generic;
  28. using System.Reflection;
  29. using libsecondlife;
  30. using log4net;
  31. using OpenSim.Framework;
  32. using OpenSim.Framework.Communications.Limit;
  33. using OpenSim.Region.Environment.Interfaces;
  34. using OpenSim.Region.Environment.Scenes;
  35. namespace OpenSim.Region.Environment.Modules.Agent.TextureDownload
  36. {
  37. /// <summary>
  38. /// This module sets up texture senders in response to client texture requests, and places them on a
  39. /// processing queue once those senders have the appropriate data (i.e. a texture retrieved from the
  40. /// asset cache).
  41. /// </summary>
  42. public class UserTextureDownloadService
  43. {
  44. private static readonly ILog m_log
  45. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. /// <summary>
  47. /// We will allow the client to request the same texture n times before dropping further requests
  48. ///
  49. /// This number includes repeated requests for the same texture at different resolutions (which we don't
  50. /// currently handle properly as far as I know). However, this situation should be handled in a more
  51. /// sophisticated way.
  52. /// </summary>
  53. private static readonly int MAX_ALLOWED_TEXTURE_REQUESTS = 5;
  54. /// <summary>
  55. /// XXX Also going to limit requests for found textures.
  56. /// </summary>
  57. private readonly IRequestLimitStrategy<LLUUID> foundTextureLimitStrategy
  58. = new RepeatLimitStrategy<LLUUID>(MAX_ALLOWED_TEXTURE_REQUESTS);
  59. private readonly IClientAPI m_client;
  60. private readonly Scene m_scene;
  61. /// <summary>
  62. /// Texture Senders are placed in this queue once they have received their texture from the asset
  63. /// cache. Another module actually invokes the send.
  64. /// </summary>
  65. private readonly BlockingQueue<ITextureSender> m_sharedSendersQueue;
  66. /// <summary>
  67. /// Holds texture senders before they have received the appropriate texture from the asset cache.
  68. /// </summary>
  69. private readonly Dictionary<LLUUID, TextureSender.TextureSender> m_textureSenders = new Dictionary<LLUUID, TextureSender.TextureSender>();
  70. /// <summary>
  71. /// We're going to limit requests for the same missing texture.
  72. /// XXX This is really a temporary solution to deal with the situation where a client continually requests
  73. /// the same missing textures
  74. /// </summary>
  75. private readonly IRequestLimitStrategy<LLUUID> missingTextureLimitStrategy
  76. = new RepeatLimitStrategy<LLUUID>(MAX_ALLOWED_TEXTURE_REQUESTS);
  77. public UserTextureDownloadService(
  78. IClientAPI client, Scene scene, BlockingQueue<ITextureSender> sharedQueue)
  79. {
  80. m_client = client;
  81. m_scene = scene;
  82. m_sharedSendersQueue = sharedQueue;
  83. }
  84. /// <summary>
  85. /// Handle a texture request. This involves creating a texture sender and placing it on the
  86. /// previously passed in shared queue.
  87. /// </summary>
  88. /// <param name="e"></param>
  89. public void HandleTextureRequest(TextureRequestArgs e)
  90. {
  91. TextureSender.TextureSender textureSender;
  92. //TODO: should be working out the data size/ number of packets to be sent for each discard level
  93. if ((e.DiscardLevel >= 0) || (e.Priority != 0))
  94. {
  95. lock (m_textureSenders)
  96. {
  97. if (m_textureSenders.TryGetValue(e.RequestedAssetID, out textureSender))
  98. {
  99. // If we've received new non UUID information for this request and it hasn't dispatched
  100. // yet, then update the request accordingly.
  101. textureSender.UpdateRequest(e.DiscardLevel, e.PacketNumber);
  102. }
  103. else
  104. {
  105. if (!foundTextureLimitStrategy.AllowRequest(e.RequestedAssetID))
  106. {
  107. // m_log.DebugFormat(
  108. // "[USER TEXTURE DOWNLOAD SERVICE]: Refusing request for {0} from client {1}",
  109. // e.RequestedAssetID, m_client.AgentId);
  110. return;
  111. }
  112. else if (!missingTextureLimitStrategy.AllowRequest(e.RequestedAssetID))
  113. {
  114. if (missingTextureLimitStrategy.IsFirstRefusal(e.RequestedAssetID))
  115. {
  116. // Commenting out this message for now as it causes too much noise with other
  117. // debug messages.
  118. // TODO: possibly record this as a statistic in the future
  119. //
  120. // m_log.DebugFormat(
  121. // "[USER TEXTURE DOWNLOAD SERVICE]: Dropping requests for notified missing texture {0} for client {1} since we have received more than {2} requests",
  122. // e.RequestedAssetID, m_client.AgentId, MAX_ALLOWED_TEXTURE_REQUESTS);
  123. }
  124. return;
  125. }
  126. m_scene.AddPendingDownloads(1);
  127. TextureSender.TextureSender requestHandler = new TextureSender.TextureSender(m_client, e.DiscardLevel, e.PacketNumber);
  128. m_textureSenders.Add(e.RequestedAssetID, requestHandler);
  129. m_scene.AssetCache.GetAsset(e.RequestedAssetID, TextureCallback, true);
  130. }
  131. }
  132. }
  133. else
  134. {
  135. lock (m_textureSenders)
  136. {
  137. if (m_textureSenders.TryGetValue(e.RequestedAssetID, out textureSender))
  138. {
  139. textureSender.Cancel = true;
  140. }
  141. }
  142. }
  143. }
  144. /// <summary>
  145. /// The callback for the asset cache when a texture has been retrieved. This method queues the
  146. /// texture sender for processing.
  147. /// </summary>
  148. /// <param name="textureID"></param>
  149. /// <param name="texture"></param>
  150. public void TextureCallback(LLUUID textureID, AssetBase texture)
  151. {
  152. //m_log.DebugFormat("[USER TEXTURE DOWNLOAD SERVICE]: Calling TextureCallback with {0}, texture == null is {1}", textureID, (texture == null ? true : false));
  153. lock (m_textureSenders)
  154. {
  155. TextureSender.TextureSender textureSender;
  156. if (m_textureSenders.TryGetValue(textureID, out textureSender))
  157. {
  158. // XXX It may be perfectly valid for a texture to have no data... but if we pass
  159. // this on to the TextureSender it will blow up, so just discard for now.
  160. // Needs investigation.
  161. if (texture == null || texture.Data == null)
  162. {
  163. if (!missingTextureLimitStrategy.IsMonitoringRequests(textureID))
  164. {
  165. missingTextureLimitStrategy.MonitorRequests(textureID);
  166. m_log.DebugFormat(
  167. "[USER TEXTURE DOWNLOAD SERVICE]: Queueing first TextureNotFoundSender for {0}, client {1}",
  168. textureID, m_client.AgentId);
  169. }
  170. ITextureSender textureNotFoundSender = new TextureNotFoundSender(m_client, textureID);
  171. EnqueueTextureSender(textureNotFoundSender);
  172. }
  173. else
  174. {
  175. if (!textureSender.ImageLoaded)
  176. {
  177. textureSender.TextureReceived(texture);
  178. EnqueueTextureSender(textureSender);
  179. foundTextureLimitStrategy.MonitorRequests(textureID);
  180. }
  181. }
  182. //m_log.InfoFormat("[TEXTURE SENDER] Removing texture sender with uuid {0}", textureID);
  183. m_textureSenders.Remove(textureID);
  184. //m_log.InfoFormat("[TEXTURE SENDER] Current texture senders in dictionary: {0}", m_textureSenders.Count);
  185. }
  186. else
  187. {
  188. m_log.WarnFormat(
  189. "Got a texture uuid {0} with no sender object to handle it, this shouldn't happen",
  190. textureID);
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// Place a ready texture sender on the processing queue.
  196. /// </summary>
  197. /// <param name="textureSender"></param>
  198. private void EnqueueTextureSender(ITextureSender textureSender)
  199. {
  200. textureSender.Cancel = false;
  201. textureSender.Sending = true;
  202. if (!m_sharedSendersQueue.Contains(textureSender))
  203. {
  204. m_sharedSendersQueue.Enqueue(textureSender);
  205. }
  206. }
  207. /// <summary>
  208. /// Close this module.
  209. /// </summary>
  210. internal void Close()
  211. {
  212. lock (m_textureSenders)
  213. {
  214. foreach (TextureSender.TextureSender textureSender in m_textureSenders.Values)
  215. {
  216. textureSender.Cancel = true;
  217. }
  218. m_textureSenders.Clear();
  219. }
  220. }
  221. }
  222. }