UserTextureDownloadService.cs 12 KB

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