AssetServerBase.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 log4net;
  31. using OpenMetaverse;
  32. using OpenSim.Data;
  33. using OpenSim.Framework.AssetLoader.Filesystem;
  34. using OpenSim.Framework.Statistics;
  35. namespace OpenSim.Framework.Communications.Cache
  36. {
  37. public abstract class AssetServerBase : IAssetServer
  38. {
  39. private static readonly ILog m_log
  40. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. protected IAssetReceiver m_receiver;
  42. protected BlockingQueue<AssetRequest> m_assetRequests;
  43. protected Thread m_localAssetServerThread;
  44. protected IAssetDataPlugin m_assetProvider;
  45. #region IPlugin
  46. /// <summary>
  47. /// The methods and properties in this region are needed to implement
  48. /// the IPlugin interface and its local extensions.
  49. /// These can all be overridden as appropriate by a derived class.
  50. /// These methods are only applicable when a class is loaded by the
  51. /// IPlugin mechanism.
  52. ///
  53. /// Note that in the case of AssetServerBase, all initialization is
  54. /// performed by the default constructor, so nothing additional is
  55. /// required here. A derived class may wish to do more.
  56. /// </summary>
  57. public virtual string Name
  58. {
  59. // get { return "OpenSim.Framework.Communications.Cache.AssetServerBase"; }
  60. get { return "AssetServerBase"; }
  61. }
  62. public virtual string Version
  63. {
  64. get { return "1.0"; }
  65. }
  66. public virtual void Initialise()
  67. {
  68. m_log.Debug("[ASSET SERVER]: IPlugin null initialization");
  69. }
  70. public virtual void Initialise(ConfigSettings settings)
  71. {
  72. m_log.Debug("[ASSET SERVER]: IPlugin null configured initialization(1)");
  73. m_log.InfoFormat("[ASSET SERVER]: Initializing client [{0}/{1}", Name, Version);
  74. }
  75. public virtual void Initialise(ConfigSettings settings, string p_url)
  76. {
  77. m_log.Debug("[ASSET SERVER]: IPlugin null configured initialization(2)");
  78. m_log.InfoFormat("[ASSET SERVER]: Initializing client [{0}/{1}", Name, Version);
  79. }
  80. public virtual void Initialise(ConfigSettings settings, string p_url, string p_dir, bool p_t)
  81. {
  82. m_log.Debug("[ASSET SERVER]: IPlugin null configured initialization(3)");
  83. m_log.InfoFormat("[ASSET SERVER]: Initializing client [{0}/{1}", Name, Version);
  84. }
  85. public virtual void Dispose()
  86. {
  87. m_log.Debug("[ASSET SERVER]: dispose");
  88. }
  89. #endregion
  90. public IAssetDataPlugin AssetProviderPlugin
  91. {
  92. get { return m_assetProvider; }
  93. }
  94. // Temporarily hardcoded - should be a plugin
  95. protected IAssetLoader assetLoader = new AssetLoaderFileSystem();
  96. public abstract void StoreAsset(AssetBase asset);
  97. /// <summary>
  98. /// This method must be implemented by a subclass to retrieve the asset named in the
  99. /// AssetRequest. If the asset is not found, null should be returned.
  100. /// </summary>
  101. /// <param name="req"></param>
  102. /// <returns></returns>
  103. /// <exception cref="System.Exception">
  104. /// Thrown if the request failed for some other reason than that the
  105. /// asset cannot be found.
  106. /// </exception>
  107. protected abstract AssetBase GetAsset(AssetRequest req);
  108. /// <summary>
  109. /// Process an asset request. This method will call GetAsset(AssetRequest req)
  110. /// on the subclass.
  111. /// </summary>
  112. /// <param name="req"></param>
  113. protected virtual void ProcessRequest(AssetRequest req)
  114. {
  115. AssetBase asset;
  116. try
  117. {
  118. asset = GetAsset(req);
  119. }
  120. catch (Exception e)
  121. {
  122. m_log.ErrorFormat("[ASSET]: Asset request for {0} threw exception {1}", req.AssetID, e);
  123. if (StatsManager.SimExtraStats != null)
  124. StatsManager.SimExtraStats.AddAssetServiceRequestFailure();
  125. m_receiver.AssetNotFound(req.AssetID, req.IsTexture);
  126. return;
  127. }
  128. if (asset != null)
  129. {
  130. //m_log.DebugFormat("[ASSET]: Asset {0} received from asset server", req.AssetID);
  131. m_receiver.AssetReceived(asset, req.IsTexture);
  132. }
  133. else
  134. {
  135. //m_log.WarnFormat("[ASSET]: Asset {0} not found by asset server", req.AssetID);
  136. m_receiver.AssetNotFound(req.AssetID, req.IsTexture);
  137. }
  138. }
  139. public virtual void LoadDefaultAssets(string pAssetSetsXml)
  140. {
  141. m_log.Info("[ASSET SERVER]: Setting up asset database");
  142. assetLoader.ForEachDefaultXmlAsset(pAssetSetsXml, StoreAsset);
  143. }
  144. public AssetServerBase()
  145. {
  146. m_log.Info("[ASSET SERVER]: Starting asset storage system");
  147. m_assetRequests = new BlockingQueue<AssetRequest>();
  148. m_localAssetServerThread = new Thread(RunRequests);
  149. m_localAssetServerThread.Name = "LocalAssetServerThread";
  150. m_localAssetServerThread.IsBackground = true;
  151. m_localAssetServerThread.Start();
  152. ThreadTracker.Add(m_localAssetServerThread);
  153. }
  154. private void RunRequests()
  155. {
  156. while (true) // Since it's a 'blocking queue'
  157. {
  158. try
  159. {
  160. AssetRequest req = m_assetRequests.Dequeue();
  161. ProcessRequest(req);
  162. }
  163. catch (Exception e)
  164. {
  165. m_log.Error("[ASSET SERVER]: " + e.ToString());
  166. }
  167. }
  168. }
  169. /// <summary>
  170. /// The receiver will be called back with asset data once it comes in.
  171. /// </summary>
  172. /// <param name="receiver"></param>
  173. public void SetReceiver(IAssetReceiver receiver)
  174. {
  175. m_receiver = receiver;
  176. }
  177. public void RequestAsset(UUID assetID, bool isTexture)
  178. {
  179. AssetRequest req = new AssetRequest();
  180. req.AssetID = assetID;
  181. req.IsTexture = isTexture;
  182. m_assetRequests.Enqueue(req);
  183. #if DEBUG
  184. //m_log.InfoFormat("[ASSET SERVER]: Added {0} to request queue", assetID);
  185. #endif
  186. }
  187. public virtual void UpdateAsset(AssetBase asset)
  188. {
  189. m_assetProvider.UpdateAsset(asset);
  190. }
  191. public virtual void Close()
  192. {
  193. m_localAssetServerThread.Abort();
  194. }
  195. public void SetServerInfo(string ServerUrl, string ServerKey)
  196. {
  197. }
  198. }
  199. }