UserTextureDownloadService.cs 11 KB

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