AssetsRequest.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 OpenSim.Framework;
  28. using OpenSim.Framework.Communications.Cache;
  29. using OpenSim.Region.Environment.Interfaces;
  30. using OpenSim.Region.Environment.Scenes;
  31. using System.Collections.Generic;
  32. using System.Threading;
  33. using libsecondlife;
  34. namespace OpenSim.Region.Environment.Modules.World.Archiver
  35. {
  36. /// <summary>
  37. /// Encapsulate the asynchronous requests for the assets required for an archive operation
  38. /// </summary>
  39. class AssetsRequest
  40. {
  41. /// <summary>
  42. /// uuids to request
  43. /// </summary>
  44. protected ICollection<LLUUID> m_uuids;
  45. /// <summary>
  46. /// Callback used when all the assets requested have been received.
  47. /// </summary>
  48. protected AssetsRequestCallback m_assetsRequestCallback;
  49. /// <summary>
  50. /// Assets retrieved in this request
  51. /// </summary>
  52. protected Dictionary<LLUUID, AssetBase> m_assets = new Dictionary<LLUUID, AssetBase>();
  53. /// <summary>
  54. /// Record the number of asset replies required so we know when we've finished
  55. /// </summary>
  56. private int m_repliesRequired;
  57. /// <summary>
  58. /// Asset cache used to request the assets
  59. /// </summary>
  60. protected AssetCache m_assetCache;
  61. protected internal AssetsRequest(ICollection<LLUUID> uuids, AssetCache assetCache, AssetsRequestCallback assetsRequestCallback)
  62. {
  63. m_uuids = uuids;
  64. m_assetsRequestCallback = assetsRequestCallback;
  65. m_assetCache = assetCache;
  66. m_repliesRequired = uuids.Count;
  67. }
  68. protected internal void Execute()
  69. {
  70. // We can stop here if there are no assets to fetch
  71. if (m_repliesRequired == 0)
  72. m_assetsRequestCallback(m_assets);
  73. foreach (LLUUID uuid in m_uuids)
  74. {
  75. m_assetCache.GetAsset(uuid, AssetRequestCallback, true);
  76. }
  77. }
  78. /// <summary>
  79. /// Called back by the asset cache when it has the asset
  80. /// </summary>
  81. /// <param name="assetID"></param>
  82. /// <param name="asset"></param>
  83. public void AssetRequestCallback(LLUUID assetID, AssetBase asset)
  84. {
  85. m_assets[assetID] = asset;
  86. if (m_assets.Count == m_repliesRequired)
  87. {
  88. // We want to stop using the asset cache thread asap as we now need to do the actual work of producing the archive
  89. Thread newThread = new Thread(PerformAssetsRequestCallback);
  90. newThread.Name = "OpenSimulator archiving thread post assets receipt";
  91. newThread.Start();
  92. }
  93. }
  94. /// <summary>
  95. /// Perform the callback on the original requester of the assets
  96. /// </summary>
  97. protected void PerformAssetsRequestCallback()
  98. {
  99. m_assetsRequestCallback(m_assets);
  100. }
  101. }
  102. }