AssetServiceConnector.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 OpenSimulator 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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using Nini.Config;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Communications;
  36. using OpenSim.Services.Interfaces;
  37. using OpenMetaverse;
  38. namespace OpenSim.Services.Connectors
  39. {
  40. public class AssetServicesConnector : IAssetService
  41. {
  42. private static readonly ILog m_log =
  43. LogManager.GetLogger(
  44. MethodBase.GetCurrentMethod().DeclaringType);
  45. private string m_ServerURI = String.Empty;
  46. private IImprovedAssetCache m_Cache = null;
  47. private delegate void AssetRetrievedEx(AssetBase asset);
  48. // Keeps track of concurrent requests for the same asset, so that it's only loaded once.
  49. // Maps: Asset ID -> Handlers which will be called when the asset has been loaded
  50. private Dictionary<string, AssetRetrievedEx> m_AssetHandlers = new Dictionary<string, AssetRetrievedEx>();
  51. public AssetServicesConnector()
  52. {
  53. }
  54. public AssetServicesConnector(string serverURI)
  55. {
  56. m_ServerURI = serverURI.TrimEnd('/');
  57. }
  58. public AssetServicesConnector(IConfigSource source)
  59. {
  60. Initialise(source);
  61. }
  62. public virtual void Initialise(IConfigSource source)
  63. {
  64. IConfig assetConfig = source.Configs["AssetService"];
  65. if (assetConfig == null)
  66. {
  67. m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
  68. throw new Exception("Asset connector init error");
  69. }
  70. string serviceURI = assetConfig.GetString("AssetServerURI",
  71. String.Empty);
  72. if (serviceURI == String.Empty)
  73. {
  74. m_log.Error("[ASSET CONNECTOR]: No Server URI named in section AssetService");
  75. throw new Exception("Asset connector init error");
  76. }
  77. m_ServerURI = serviceURI;
  78. }
  79. protected void SetCache(IImprovedAssetCache cache)
  80. {
  81. m_Cache = cache;
  82. }
  83. public AssetBase Get(string id)
  84. {
  85. // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Synchronous get request for {0}", id);
  86. string uri = m_ServerURI + "/assets/" + id;
  87. AssetBase asset = null;
  88. if (m_Cache != null)
  89. asset = m_Cache.Get(id);
  90. if (asset == null)
  91. {
  92. asset = SynchronousRestObjectRequester.
  93. MakeRequest<int, AssetBase>("GET", uri, 0);
  94. if (m_Cache != null)
  95. m_Cache.Cache(asset);
  96. }
  97. return asset;
  98. }
  99. public AssetBase GetCached(string id)
  100. {
  101. // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Cache request for {0}", id);
  102. if (m_Cache != null)
  103. return m_Cache.Get(id);
  104. return null;
  105. }
  106. public AssetMetadata GetMetadata(string id)
  107. {
  108. if (m_Cache != null)
  109. {
  110. AssetBase fullAsset = m_Cache.Get(id);
  111. if (fullAsset != null)
  112. return fullAsset.Metadata;
  113. }
  114. string uri = m_ServerURI + "/assets/" + id + "/metadata";
  115. AssetMetadata asset = SynchronousRestObjectRequester.
  116. MakeRequest<int, AssetMetadata>("GET", uri, 0);
  117. return asset;
  118. }
  119. public byte[] GetData(string id)
  120. {
  121. if (m_Cache != null)
  122. {
  123. AssetBase fullAsset = m_Cache.Get(id);
  124. if (fullAsset != null)
  125. return fullAsset.Data;
  126. }
  127. RestClient rc = new RestClient(m_ServerURI);
  128. rc.AddResourcePath("assets");
  129. rc.AddResourcePath(id);
  130. rc.AddResourcePath("data");
  131. rc.RequestMethod = "GET";
  132. Stream s = rc.Request();
  133. if (s == null)
  134. return null;
  135. if (s.Length > 0)
  136. {
  137. byte[] ret = new byte[s.Length];
  138. s.Read(ret, 0, (int)s.Length);
  139. return ret;
  140. }
  141. return null;
  142. }
  143. public bool Get(string id, Object sender, AssetRetrieved handler)
  144. {
  145. // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Potentially asynchronous get request for {0}", id);
  146. string uri = m_ServerURI + "/assets/" + id;
  147. AssetBase asset = null;
  148. if (m_Cache != null)
  149. asset = m_Cache.Get(id);
  150. if (asset == null)
  151. {
  152. lock (m_AssetHandlers)
  153. {
  154. AssetRetrievedEx handlerEx = new AssetRetrievedEx(delegate(AssetBase _asset) { handler(id, sender, _asset); });
  155. AssetRetrievedEx handlers;
  156. if (m_AssetHandlers.TryGetValue(id, out handlers))
  157. {
  158. // Someone else is already loading this asset. It will notify our handler when done.
  159. handlers += handlerEx;
  160. return true;
  161. }
  162. // Load the asset ourselves
  163. handlers += handlerEx;
  164. m_AssetHandlers.Add(id, handlers);
  165. }
  166. bool success = false;
  167. try
  168. {
  169. AsynchronousRestObjectRequester.MakeRequest<int, AssetBase>("GET", uri, 0,
  170. delegate(AssetBase a)
  171. {
  172. if (m_Cache != null)
  173. m_Cache.Cache(a);
  174. AssetRetrievedEx handlers;
  175. lock (m_AssetHandlers)
  176. {
  177. handlers = m_AssetHandlers[id];
  178. m_AssetHandlers.Remove(id);
  179. }
  180. handlers.Invoke(a);
  181. });
  182. success = true;
  183. }
  184. finally
  185. {
  186. if (!success)
  187. {
  188. lock (m_AssetHandlers)
  189. {
  190. m_AssetHandlers.Remove(id);
  191. }
  192. }
  193. }
  194. }
  195. else
  196. {
  197. handler(id, sender, asset);
  198. }
  199. return true;
  200. }
  201. public string Store(AssetBase asset)
  202. {
  203. if (asset.Temporary || asset.Local)
  204. {
  205. if (m_Cache != null)
  206. m_Cache.Cache(asset);
  207. return asset.ID;
  208. }
  209. string uri = m_ServerURI + "/assets/";
  210. string newID = string.Empty;
  211. try
  212. {
  213. newID = SynchronousRestObjectRequester.
  214. MakeRequest<AssetBase, string>("POST", uri, asset);
  215. }
  216. catch (Exception e)
  217. {
  218. m_log.WarnFormat("[ASSET CONNECTOR]: Unable to send asset {0} to asset server. Reason: {1}", asset.ID, e.Message);
  219. }
  220. if (newID != String.Empty)
  221. {
  222. // Placing this here, so that this work with old asset servers that don't send any reply back
  223. // SynchronousRestObjectRequester returns somethins that is not an empty string
  224. if (newID != null)
  225. asset.ID = newID;
  226. if (m_Cache != null)
  227. m_Cache.Cache(asset);
  228. }
  229. return newID;
  230. }
  231. public bool UpdateContent(string id, byte[] data)
  232. {
  233. AssetBase asset = null;
  234. if (m_Cache != null)
  235. asset = m_Cache.Get(id);
  236. if (asset == null)
  237. {
  238. AssetMetadata metadata = GetMetadata(id);
  239. if (metadata == null)
  240. return false;
  241. asset = new AssetBase(metadata.FullID, metadata.Name, metadata.Type, UUID.Zero.ToString());
  242. asset.Metadata = metadata;
  243. }
  244. asset.Data = data;
  245. string uri = m_ServerURI + "/assets/" + id;
  246. if (SynchronousRestObjectRequester.
  247. MakeRequest<AssetBase, bool>("POST", uri, asset))
  248. {
  249. if (m_Cache != null)
  250. m_Cache.Cache(asset);
  251. return true;
  252. }
  253. return false;
  254. }
  255. public bool Delete(string id)
  256. {
  257. string uri = m_ServerURI + "/assets/" + id;
  258. if (SynchronousRestObjectRequester.
  259. MakeRequest<int, bool>("DELETE", uri, 0))
  260. {
  261. if (m_Cache != null)
  262. m_Cache.Expire(id);
  263. return true;
  264. }
  265. return false;
  266. }
  267. }
  268. }