AssetsRequest.cs 5.3 KB

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