AssetServerBase.cs 6.2 KB

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