AssetsRequest.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 System.Reflection;
  30. using System.Threading;
  31. using OpenMetaverse;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications.Cache;
  35. using OpenSim.Region.Environment.Interfaces;
  36. using OpenSim.Region.Environment.Scenes;
  37. namespace OpenSim.Region.Environment.Modules.World.Archiver
  38. {
  39. /// <summary>
  40. /// Encapsulate the asynchronous requests for the assets required for an archive operation
  41. /// </summary>
  42. class AssetsRequest
  43. {
  44. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  45. /// <summary>
  46. /// uuids to request
  47. /// </summary>
  48. protected ICollection<UUID> m_uuids;
  49. /// <summary>
  50. /// Callback used when all the assets requested have been received.
  51. /// </summary>
  52. protected AssetsRequestCallback m_assetsRequestCallback;
  53. /// <summary>
  54. /// Assets retrieved in this request
  55. /// </summary>
  56. protected Dictionary<UUID, AssetBase> m_assets = new Dictionary<UUID, AssetBase>();
  57. /// <summary>
  58. /// Maintain a list of assets that could not be found. This will be passed back to the requester.
  59. /// </summary>
  60. protected List<UUID> m_notFoundAssetUuids = new List<UUID>();
  61. /// <summary>
  62. /// Record the number of asset replies required so we know when we've finished
  63. /// </summary>
  64. private int m_repliesRequired;
  65. /// <summary>
  66. /// Asset cache used to request the assets
  67. /// </summary>
  68. protected AssetCache m_assetCache;
  69. protected internal AssetsRequest(ICollection<UUID> uuids, AssetCache assetCache, AssetsRequestCallback assetsRequestCallback)
  70. {
  71. m_uuids = uuids;
  72. m_assetsRequestCallback = assetsRequestCallback;
  73. m_assetCache = assetCache;
  74. m_repliesRequired = uuids.Count;
  75. }
  76. protected internal void Execute()
  77. {
  78. // We can stop here if there are no assets to fetch
  79. if (m_repliesRequired == 0)
  80. m_assetsRequestCallback(m_assets, m_notFoundAssetUuids);
  81. foreach (UUID uuid in m_uuids)
  82. {
  83. m_assetCache.GetAsset(uuid, AssetRequestCallback, true);
  84. }
  85. }
  86. /// <summary>
  87. /// Called back by the asset cache when it has the asset
  88. /// </summary>
  89. /// <param name="assetID"></param>
  90. /// <param name="asset"></param>
  91. public void AssetRequestCallback(UUID assetID, AssetBase asset)
  92. {
  93. if (asset != null)
  94. m_assets[assetID] = asset;
  95. else
  96. m_notFoundAssetUuids.Add(assetID);
  97. //m_log.DebugFormat(
  98. // "[ARCHIVER]: Received {0} assets and notification of {1} missing assets", m_assets.Count, m_notFoundAssetUuids.Count);
  99. if (m_assets.Count + m_notFoundAssetUuids.Count == m_repliesRequired)
  100. {
  101. // We want to stop using the asset cache thread asap as we now need to do the actual work of producing the archive
  102. Thread newThread = new Thread(PerformAssetsRequestCallback);
  103. newThread.Name = "OpenSimulator archiving thread post assets receipt";
  104. newThread.Start();
  105. }
  106. }
  107. /// <summary>
  108. /// Perform the callback on the original requester of the assets
  109. /// </summary>
  110. protected void PerformAssetsRequestCallback()
  111. {
  112. try
  113. {
  114. m_assetsRequestCallback(m_assets, m_notFoundAssetUuids);
  115. }
  116. catch (Exception e)
  117. {
  118. m_log.ErrorFormat(
  119. "[ARCHIVER]: Terminating archive creation since asset requster callback failed with {0}", e);
  120. }
  121. }
  122. }
  123. }