AssetServerBase.cs 6.0 KB

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