AssetServicesConnector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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.Services.Interfaces;
  36. using OpenMetaverse;
  37. namespace OpenSim.Services.Connectors
  38. {
  39. public class AssetServicesConnector : BaseServiceConnector, IAssetService
  40. {
  41. private static readonly ILog m_log =
  42. LogManager.GetLogger(
  43. MethodBase.GetCurrentMethod().DeclaringType);
  44. private string m_ServerURI = String.Empty;
  45. private IImprovedAssetCache m_Cache = null;
  46. private int m_maxAssetRequestConcurrency = 30;
  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 int MaxAssetRequestConcurrency
  52. {
  53. get { return m_maxAssetRequestConcurrency; }
  54. set { m_maxAssetRequestConcurrency = value; }
  55. }
  56. public AssetServicesConnector()
  57. {
  58. }
  59. public AssetServicesConnector(string serverURI)
  60. {
  61. m_ServerURI = serverURI.TrimEnd('/');
  62. }
  63. public AssetServicesConnector(IConfigSource source)
  64. : base(source, "AssetService")
  65. {
  66. Initialise(source);
  67. }
  68. public virtual void Initialise(IConfigSource source)
  69. {
  70. IConfig netconfig = source.Configs["Network"];
  71. if (netconfig != null)
  72. m_maxAssetRequestConcurrency = netconfig.GetInt("MaxRequestConcurrency",m_maxAssetRequestConcurrency);
  73. IConfig assetConfig = source.Configs["AssetService"];
  74. if (assetConfig == null)
  75. {
  76. m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
  77. throw new Exception("Asset connector init error");
  78. }
  79. string serviceURI = assetConfig.GetString("AssetServerURI",
  80. String.Empty);
  81. if (serviceURI == String.Empty)
  82. {
  83. m_log.Error("[ASSET CONNECTOR]: No Server URI named in section AssetService");
  84. throw new Exception("Asset connector init error");
  85. }
  86. m_ServerURI = serviceURI;
  87. }
  88. protected void SetCache(IImprovedAssetCache cache)
  89. {
  90. m_Cache = cache;
  91. }
  92. public AssetBase Get(string id)
  93. {
  94. // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Synchronous get request for {0}", id);
  95. string uri = m_ServerURI + "/assets/" + id;
  96. AssetBase asset = null;
  97. if (m_Cache != null)
  98. asset = m_Cache.Get(id);
  99. if (asset == null)
  100. {
  101. // XXX: Commented out for now since this has either never been properly operational or not for some time
  102. // as m_maxAssetRequestConcurrency was being passed as the timeout, not a concurrency limiting option.
  103. // Wasn't noticed before because timeout wasn't actually used.
  104. // Not attempting concurrency setting for now as this omission was discovered in release candidate
  105. // phase for OpenSimulator 0.8. Need to revisit afterwards.
  106. // asset
  107. // = SynchronousRestObjectRequester.MakeRequest<int, AssetBase>(
  108. // "GET", uri, 0, m_maxAssetRequestConcurrency);
  109. asset = SynchronousRestObjectRequester.MakeRequest<int, AssetBase>("GET", uri, 0, m_Auth);
  110. if (m_Cache != null)
  111. m_Cache.Cache(asset);
  112. }
  113. return asset;
  114. }
  115. public AssetBase GetCached(string id)
  116. {
  117. // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Cache request for {0}", id);
  118. if (m_Cache != null)
  119. return m_Cache.Get(id);
  120. return null;
  121. }
  122. public AssetMetadata GetMetadata(string id)
  123. {
  124. if (m_Cache != null)
  125. {
  126. AssetBase fullAsset = m_Cache.Get(id);
  127. if (fullAsset != null)
  128. return fullAsset.Metadata;
  129. }
  130. string uri = m_ServerURI + "/assets/" + id + "/metadata";
  131. AssetMetadata asset = SynchronousRestObjectRequester.MakeRequest<int, AssetMetadata>("GET", uri, 0, m_Auth);
  132. return asset;
  133. }
  134. public byte[] GetData(string id)
  135. {
  136. if (m_Cache != null)
  137. {
  138. AssetBase fullAsset = m_Cache.Get(id);
  139. if (fullAsset != null)
  140. return fullAsset.Data;
  141. }
  142. using (RestClient rc = new RestClient(m_ServerURI))
  143. {
  144. rc.AddResourcePath("assets");
  145. rc.AddResourcePath(id);
  146. rc.AddResourcePath("data");
  147. rc.RequestMethod = "GET";
  148. Stream s = rc.Request(m_Auth);
  149. if (s == null)
  150. return null;
  151. if (s.Length > 0)
  152. {
  153. byte[] ret = new byte[s.Length];
  154. s.Read(ret, 0, (int)s.Length);
  155. return ret;
  156. }
  157. return null;
  158. }
  159. }
  160. public bool Get(string id, Object sender, AssetRetrieved handler)
  161. {
  162. // m_log.DebugFormat("[ASSET SERVICE CONNECTOR]: Potentially asynchronous get request for {0}", id);
  163. string uri = m_ServerURI + "/assets/" + id;
  164. AssetBase asset = null;
  165. if (m_Cache != null)
  166. asset = m_Cache.Get(id);
  167. if (asset == null)
  168. {
  169. lock (m_AssetHandlers)
  170. {
  171. AssetRetrievedEx handlerEx = new AssetRetrievedEx(delegate(AssetBase _asset) { handler(id, sender, _asset); });
  172. AssetRetrievedEx handlers;
  173. if (m_AssetHandlers.TryGetValue(id, out handlers))
  174. {
  175. // Someone else is already loading this asset. It will notify our handler when done.
  176. handlers += handlerEx;
  177. return true;
  178. }
  179. // Load the asset ourselves
  180. handlers += handlerEx;
  181. m_AssetHandlers.Add(id, handlers);
  182. }
  183. bool success = false;
  184. try
  185. {
  186. AsynchronousRestObjectRequester.MakeRequest<int, AssetBase>("GET", uri, 0,
  187. delegate(AssetBase a)
  188. {
  189. if (a != null && m_Cache != null)
  190. m_Cache.Cache(a);
  191. AssetRetrievedEx handlers;
  192. lock (m_AssetHandlers)
  193. {
  194. handlers = m_AssetHandlers[id];
  195. m_AssetHandlers.Remove(id);
  196. }
  197. handlers.Invoke(a);
  198. }, m_maxAssetRequestConcurrency, m_Auth);
  199. success = true;
  200. }
  201. finally
  202. {
  203. if (!success)
  204. {
  205. lock (m_AssetHandlers)
  206. {
  207. m_AssetHandlers.Remove(id);
  208. }
  209. }
  210. }
  211. }
  212. else
  213. {
  214. handler(id, sender, asset);
  215. }
  216. return true;
  217. }
  218. public virtual bool[] AssetsExist(string[] ids)
  219. {
  220. string uri = m_ServerURI + "/get_assets_exist";
  221. bool[] exist = null;
  222. try
  223. {
  224. exist = SynchronousRestObjectRequester.MakeRequest<string[], bool[]>("POST", uri, ids, m_Auth);
  225. }
  226. catch (Exception)
  227. {
  228. // This is most likely to happen because the server doesn't support this function,
  229. // so just silently return "doesn't exist" for all the assets.
  230. }
  231. if (exist == null)
  232. exist = new bool[ids.Length];
  233. return exist;
  234. }
  235. public string Store(AssetBase asset)
  236. {
  237. if (asset.Local)
  238. {
  239. if (m_Cache != null)
  240. m_Cache.Cache(asset);
  241. return asset.ID;
  242. }
  243. string uri = m_ServerURI + "/assets/";
  244. string newID;
  245. try
  246. {
  247. newID = SynchronousRestObjectRequester.MakeRequest<AssetBase, string>("POST", uri, asset, m_Auth);
  248. }
  249. catch (Exception e)
  250. {
  251. m_log.Warn(string.Format("[ASSET CONNECTOR]: Unable to send asset {0} to asset server. Reason: {1} ", asset.ID, e.Message), e);
  252. return string.Empty;
  253. }
  254. // TEMPORARY: SRAS returns 'null' when it's asked to store existing assets
  255. if (newID == null)
  256. {
  257. m_log.DebugFormat("[ASSET CONNECTOR]: Storing of asset {0} returned null; assuming the asset already exists", asset.ID);
  258. return asset.ID;
  259. }
  260. if (string.IsNullOrEmpty(newID))
  261. return string.Empty;
  262. asset.ID = newID;
  263. if (m_Cache != null)
  264. m_Cache.Cache(asset);
  265. return newID;
  266. }
  267. public bool UpdateContent(string id, byte[] data)
  268. {
  269. AssetBase asset = null;
  270. if (m_Cache != null)
  271. asset = m_Cache.Get(id);
  272. if (asset == null)
  273. {
  274. AssetMetadata metadata = GetMetadata(id);
  275. if (metadata == null)
  276. return false;
  277. asset = new AssetBase(metadata.FullID, metadata.Name, metadata.Type, UUID.Zero.ToString());
  278. asset.Metadata = metadata;
  279. }
  280. asset.Data = data;
  281. string uri = m_ServerURI + "/assets/" + id;
  282. if (SynchronousRestObjectRequester.MakeRequest<AssetBase, bool>("POST", uri, asset, m_Auth))
  283. {
  284. if (m_Cache != null)
  285. m_Cache.Cache(asset);
  286. return true;
  287. }
  288. return false;
  289. }
  290. public bool Delete(string id)
  291. {
  292. string uri = m_ServerURI + "/assets/" + id;
  293. if (SynchronousRestObjectRequester.MakeRequest<int, bool>("DELETE", uri, 0, m_Auth))
  294. {
  295. if (m_Cache != null)
  296. m_Cache.Expire(id);
  297. return true;
  298. }
  299. return false;
  300. }
  301. }
  302. }