UserTextureDownloadService.cs 12 KB

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