UserTextureDownloadService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 = 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.StatsReporter.AddPendingDownloads(1);
  134. //TextureSender.TextureSender requestHandler = new TextureSender.TextureSender(m_client, e.DiscardLevel, e.PacketNumber);
  135. //m_textureSenders.Add(e.RequestedAssetID, null);
  136. m_scene.AssetService.Get(e.RequestedAssetID.ToString(), this, TextureReceived);
  137. }
  138. protected void TextureReceived(string id, Object sender, AssetBase asset)
  139. {
  140. if (asset != null)
  141. TextureCallback(asset.FullID, asset);
  142. }
  143. /// <summary>
  144. /// The callback for the asset cache when a texture has been retrieved. This method queues the
  145. /// texture sender for processing.
  146. /// </summary>
  147. /// <param name="textureID"></param>
  148. /// <param name="texture"></param>
  149. public void TextureCallback(UUID textureID, AssetBase texture)
  150. {
  151. //m_log.DebugFormat("[USER TEXTURE DOWNLOAD SERVICE]: Calling TextureCallback with {0}, texture == null is {1}", textureID, (texture == null ? true : false));
  152. // There may still be texture requests pending for a logged out client
  153. if (closed)
  154. return;
  155. /*
  156. lock (m_textureSenders)
  157. {
  158. TextureSender.TextureSender textureSender;
  159. if (m_textureSenders.TryGetValue(textureID, out textureSender))
  160. {
  161. // XXX It may be perfectly valid for a texture to have no data... but if we pass
  162. // this on to the TextureSender it will blow up, so just discard for now.
  163. // Needs investigation.
  164. if (texture == null || texture.Data == null)
  165. {
  166. if (!missingTextureLimitStrategy.IsMonitoringRequests(textureID))
  167. {
  168. missingTextureLimitStrategy.MonitorRequests(textureID);
  169. // m_log.DebugFormat(
  170. // "[TEXTURE]: Queueing first TextureNotFoundSender for {0}, client {1}",
  171. // textureID, m_client.AgentId);
  172. }
  173. ITextureSender textureNotFoundSender = new TextureNotFoundSender(m_client, textureID);
  174. EnqueueTextureSender(textureNotFoundSender);
  175. }
  176. else
  177. {
  178. if (!textureSender.ImageLoaded)
  179. {
  180. textureSender.TextureReceived(texture);
  181. EnqueueTextureSender(textureSender);
  182. foundTextureLimitStrategy.MonitorRequests(textureID);
  183. }
  184. }
  185. //m_log.InfoFormat("[TEXTURE] Removing texture sender with uuid {0}", textureID);
  186. m_textureSenders.Remove(textureID);
  187. //m_log.InfoFormat("[TEXTURE] Current texture senders in dictionary: {0}", m_textureSenders.Count);
  188. }
  189. else
  190. {
  191. m_log.WarnFormat(
  192. "[TEXTURE]: Got a texture uuid {0} with no sender object to handle it, this shouldn't happen",
  193. textureID);
  194. }
  195. }
  196. */
  197. }
  198. /// <summary>
  199. /// Place a ready texture sender on the processing queue.
  200. /// </summary>
  201. /// <param name="textureSender"></param>
  202. // private void EnqueueTextureSender(ITextureSender textureSender)
  203. // {
  204. // textureSender.Cancel = false;
  205. // textureSender.Sending = true;
  206. //
  207. // if (!m_sharedSendersQueue.Contains(textureSender))
  208. // {
  209. // m_sharedSendersQueue.Enqueue(textureSender);
  210. // }
  211. // }
  212. /// <summary>
  213. /// Close this module.
  214. /// </summary>
  215. internal void Close()
  216. {
  217. closed = true;
  218. lock (m_textureSenders)
  219. {
  220. foreach (TextureSender.TextureSender textureSender in m_textureSenders.Values)
  221. {
  222. textureSender.Cancel = true;
  223. }
  224. m_textureSenders.Clear();
  225. }
  226. // XXX: It might be possible to also remove pending texture requests from the asset cache queues,
  227. // though this might also be more trouble than it's worth.
  228. }
  229. }
  230. }