AssetServerBase.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 = new BlockingQueue<AssetRequest>();
  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 virtual void Start()
  97. {
  98. m_log.Debug("[ASSET SERVER]: Starting asset server");
  99. m_localAssetServerThread = new Thread(RunRequests);
  100. m_localAssetServerThread.Name = "LocalAssetServerThread";
  101. m_localAssetServerThread.IsBackground = true;
  102. m_localAssetServerThread.Start();
  103. ThreadTracker.Add(m_localAssetServerThread);
  104. }
  105. public virtual void Stop()
  106. {
  107. m_localAssetServerThread.Abort();
  108. }
  109. public abstract void StoreAsset(AssetBase asset);
  110. /// <summary>
  111. /// This method must be implemented by a subclass to retrieve the asset named in the
  112. /// AssetRequest. If the asset is not found, null should be returned.
  113. /// </summary>
  114. /// <param name="req"></param>
  115. /// <returns></returns>
  116. /// <exception cref="System.Exception">
  117. /// Thrown if the request failed for some other reason than that the
  118. /// asset cannot be found.
  119. /// </exception>
  120. protected abstract AssetBase GetAsset(AssetRequest req);
  121. /// <summary>
  122. /// Does the asset server have any waiting requests?
  123. /// </summary>
  124. ///
  125. /// This does include any request that is currently being handled. This information is not reliable where
  126. /// another thread may be processing requests.
  127. ///
  128. /// <returns>
  129. /// True if there are waiting requests. False if there are no waiting requests.
  130. /// </returns>
  131. public virtual bool HasWaitingRequests()
  132. {
  133. return m_assetRequests.Count() != 0;
  134. }
  135. /// <summary>
  136. /// Process an asset request. This method will call GetAsset(AssetRequest req)
  137. /// on the subclass.
  138. /// </summary>
  139. public virtual void ProcessNextRequest()
  140. {
  141. AssetRequest req = m_assetRequests.Dequeue();
  142. AssetBase asset;
  143. try
  144. {
  145. asset = GetAsset(req);
  146. }
  147. catch (Exception e)
  148. {
  149. m_log.ErrorFormat("[ASSET]: Asset request for {0} threw exception {1} - Stack Trace: {2}", req.AssetID, e, e.StackTrace);
  150. if (StatsManager.SimExtraStats != null)
  151. StatsManager.SimExtraStats.AddAssetServiceRequestFailure();
  152. m_receiver.AssetNotFound(req.AssetID, req.IsTexture);
  153. return;
  154. }
  155. if (asset != null)
  156. {
  157. //m_log.DebugFormat("[ASSET]: Asset {0} received from asset server", req.AssetID);
  158. m_receiver.AssetReceived(asset, req.IsTexture);
  159. }
  160. else
  161. {
  162. //m_log.WarnFormat("[ASSET]: Asset {0} not found by asset server", req.AssetID);
  163. m_receiver.AssetNotFound(req.AssetID, req.IsTexture);
  164. }
  165. }
  166. public virtual void LoadDefaultAssets(string pAssetSetsXml)
  167. {
  168. m_log.Info("[ASSET SERVER]: Setting up asset database");
  169. assetLoader.ForEachDefaultXmlAsset(pAssetSetsXml, StoreAsset);
  170. }
  171. private void RunRequests()
  172. {
  173. while (true) // Since it's a 'blocking queue'
  174. {
  175. try
  176. {
  177. ProcessNextRequest();
  178. }
  179. catch (Exception e)
  180. {
  181. m_log.Error("[ASSET SERVER]: " + e.ToString());
  182. }
  183. }
  184. }
  185. /// <summary>
  186. /// The receiver will be called back with asset data once it comes in.
  187. /// </summary>
  188. /// <param name="receiver"></param>
  189. public void SetReceiver(IAssetReceiver receiver)
  190. {
  191. m_receiver = receiver;
  192. }
  193. public void RequestAsset(UUID assetID, bool isTexture)
  194. {
  195. AssetRequest req = new AssetRequest();
  196. req.AssetID = assetID;
  197. req.IsTexture = isTexture;
  198. m_assetRequests.Enqueue(req);
  199. //m_log.DebugFormat("[ASSET SERVER]: Added {0} to request queue", assetID);
  200. }
  201. public virtual void UpdateAsset(AssetBase asset)
  202. {
  203. m_assetProvider.UpdateAsset(asset);
  204. }
  205. public void SetServerInfo(string ServerUrl, string ServerKey)
  206. {
  207. }
  208. }
  209. }