AssetServerBase.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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.Reflection;
  29. using System.Threading;
  30. using OpenMetaverse;
  31. using log4net;
  32. using OpenSim.Framework.AssetLoader.Filesystem;
  33. using OpenSim.Framework.Statistics;
  34. namespace OpenSim.Framework.Communications.Cache
  35. {
  36. public abstract class AssetServerBase : IAssetServer
  37. {
  38. private static readonly ILog m_log
  39. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. protected IAssetReceiver m_receiver;
  41. protected BlockingQueue<AssetRequest> m_assetRequests;
  42. protected Thread m_localAssetServerThread;
  43. protected IAssetProviderPlugin m_assetProvider;
  44. public IAssetProviderPlugin AssetProviderPlugin
  45. {
  46. get { return m_assetProvider; }
  47. }
  48. // Temporarily hardcoded - should be a plugin
  49. protected IAssetLoader assetLoader = new AssetLoaderFileSystem();
  50. public abstract void StoreAsset(AssetBase asset);
  51. /// <summary>
  52. /// This method must be implemented by a subclass to retrieve the asset named in the
  53. /// AssetRequest. If the asset is not found, null should be returned.
  54. /// </summary>
  55. /// <param name="req"></param>
  56. /// <returns></returns>
  57. /// <exception cref="System.Exception">
  58. /// Thrown if the request failed for some other reason than that the
  59. /// asset cannot be found.
  60. /// </exception>
  61. protected abstract AssetBase GetAsset(AssetRequest req);
  62. /// <summary>
  63. /// Process an asset request. This method will call GetAsset(AssetRequest req)
  64. /// on the subclass.
  65. /// </summary>
  66. /// <param name="req"></param>
  67. protected virtual void ProcessRequest(AssetRequest req)
  68. {
  69. AssetBase asset;
  70. try
  71. {
  72. asset = GetAsset(req);
  73. }
  74. catch (Exception e)
  75. {
  76. m_log.ErrorFormat("[ASSET]: Asset request for {0} threw exception {1}", req.AssetID, e);
  77. if (StatsManager.SimExtraStats != null)
  78. StatsManager.SimExtraStats.AddAssetServiceRequestFailure();
  79. m_receiver.AssetNotFound(req.AssetID, req.IsTexture);
  80. return;
  81. }
  82. if (asset != null)
  83. {
  84. //m_log.DebugFormat("[ASSET]: Asset {0} received from asset server", req.AssetID);
  85. m_receiver.AssetReceived(asset, req.IsTexture);
  86. }
  87. else
  88. {
  89. //m_log.WarnFormat("[ASSET]: Asset {0} not found by asset server", req.AssetID);
  90. m_receiver.AssetNotFound(req.AssetID, req.IsTexture);
  91. }
  92. }
  93. public virtual void LoadDefaultAssets(string pAssetSetsXml)
  94. {
  95. m_log.Info("[ASSET SERVER]: Setting up asset database");
  96. assetLoader.ForEachDefaultXmlAsset(pAssetSetsXml, StoreAsset);
  97. }
  98. public AssetServerBase()
  99. {
  100. m_log.Info("[ASSET SERVER]: Starting asset storage system");
  101. m_assetRequests = new BlockingQueue<AssetRequest>();
  102. m_localAssetServerThread = new Thread(RunRequests);
  103. m_localAssetServerThread.Name = "LocalAssetServerThread";
  104. m_localAssetServerThread.IsBackground = true;
  105. m_localAssetServerThread.Start();
  106. ThreadTracker.Add(m_localAssetServerThread);
  107. }
  108. private void RunRequests()
  109. {
  110. while (true) // Since it's a 'blocking queue'
  111. {
  112. try
  113. {
  114. AssetRequest req = m_assetRequests.Dequeue();
  115. ProcessRequest(req);
  116. }
  117. catch (Exception e)
  118. {
  119. m_log.Error("[ASSET SERVER]: " + e.ToString());
  120. }
  121. }
  122. }
  123. /// <summary>
  124. /// The receiver will be called back with asset data once it comes in.
  125. /// </summary>
  126. /// <param name="receiver"></param>
  127. public void SetReceiver(IAssetReceiver receiver)
  128. {
  129. m_receiver = receiver;
  130. }
  131. public void RequestAsset(UUID assetID, bool isTexture)
  132. {
  133. AssetRequest req = new AssetRequest();
  134. req.AssetID = assetID;
  135. req.IsTexture = isTexture;
  136. m_assetRequests.Enqueue(req);
  137. #if DEBUG
  138. //m_log.InfoFormat("[ASSET SERVER]: Added {0} to request queue", assetID);
  139. #endif
  140. }
  141. public virtual void UpdateAsset(AssetBase asset)
  142. {
  143. m_assetProvider.UpdateAsset(asset);
  144. }
  145. public virtual void Close()
  146. {
  147. m_localAssetServerThread.Abort();
  148. }
  149. public void SetServerInfo(string ServerUrl, string ServerKey)
  150. {
  151. }
  152. }
  153. }