1
0

AssetServicesConnector.cs 13 KB

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