AssetServicesConnector.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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.Threading;
  30. using System.Collections.Generic;
  31. using System.Collections.Concurrent;
  32. using System.IO;
  33. using System.Reflection;
  34. using System.Timers;
  35. using Nini.Config;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Monitoring;
  38. using OpenSim.Framework.ServiceAuth;
  39. using OpenSim.Services.Interfaces;
  40. using OpenMetaverse;
  41. namespace OpenSim.Services.Connectors
  42. {
  43. public class AssetServicesConnector : BaseServiceConnector, IAssetService
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. public readonly object ConnectorLock = new object();
  47. protected IAssetCache m_Cache = null;
  48. private string m_ServerURI = string.Empty;
  49. private delegate void AssetRetrievedEx(AssetBase asset);
  50. // Keeps track of concurrent requests for the same asset, so that it's only loaded once.
  51. // Maps: Asset ID -> Handlers which will be called when the asset has been loaded
  52. private Dictionary<string, List<AssetRetrievedEx>> m_AssetHandlers = new Dictionary<string, List<AssetRetrievedEx>>();
  53. public AssetServicesConnector()
  54. {
  55. }
  56. public AssetServicesConnector(string serverURI)
  57. {
  58. OSHHTPHost tmp = new OSHHTPHost(serverURI, true);
  59. m_ServerURI = tmp.IsResolvedHost ? tmp.URI : null;
  60. }
  61. public AssetServicesConnector(IConfigSource source)
  62. {
  63. Initialise(source);
  64. }
  65. public virtual void Initialise(IConfigSource source)
  66. {
  67. IConfig netconfig = source.Configs["Network"];
  68. IConfig assetConfig = source.Configs["AssetService"];
  69. if (assetConfig == null)
  70. {
  71. m_log.Error("[ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
  72. throw new Exception("Asset connector init error");
  73. }
  74. m_ServerURI = assetConfig.GetString("AssetServerURI", string.Empty);
  75. if (string.IsNullOrEmpty(m_ServerURI))
  76. {
  77. if(netconfig != null)
  78. m_ServerURI = netconfig.GetString("asset_server_url", string.Empty);
  79. }
  80. if (string.IsNullOrEmpty(m_ServerURI))
  81. {
  82. m_log.Error("[ASSET CONNECTOR]: AssetServerURI not defined in section AssetService");
  83. throw new Exception("Asset connector init error");
  84. }
  85. OSHHTPHost m_GridAssetsURL = new OSHHTPHost(m_ServerURI, true);
  86. if(!m_GridAssetsURL.IsResolvedHost)
  87. {
  88. m_log.Error("[ASSET CONNECTOR]: Could not parse or resolve AssetServerURI");
  89. throw new Exception("Asset connector init error");
  90. }
  91. m_ServerURI = m_GridAssetsURL.URI;
  92. Initialise(source, "AssetService");
  93. }
  94. private int m_maxAssetRequestConcurrency = 8;
  95. public int MaxAssetRequestConcurrency
  96. {
  97. get { return m_maxAssetRequestConcurrency; }
  98. set { m_maxAssetRequestConcurrency = value; }
  99. }
  100. protected void SetCache(IAssetCache cache)
  101. {
  102. m_Cache = cache;
  103. }
  104. public AssetBase GetCached(string id)
  105. {
  106. AssetBase asset = null;
  107. if (m_Cache != null)
  108. {
  109. m_Cache.Get(id, out asset);
  110. }
  111. return asset;
  112. }
  113. public virtual AssetBase Get(string id)
  114. {
  115. AssetBase asset = null;
  116. if (m_Cache != null)
  117. {
  118. if (!m_Cache.Get(id, out asset))
  119. return null;
  120. }
  121. if (asset == null && m_ServerURI != null)
  122. {
  123. string uri = m_ServerURI + "/assets/" + id;
  124. asset = SynchronousRestObjectRequester.MakeRequest<int, AssetBase>("GET", uri, 0, m_Auth);
  125. if (m_Cache != null)
  126. {
  127. if (asset != null)
  128. m_Cache.Cache(asset);
  129. else
  130. m_Cache.CacheNegative(id);
  131. }
  132. }
  133. return asset;
  134. }
  135. public AssetBase Get(string id, string ForeignAssetService)
  136. {
  137. return null;
  138. }
  139. public virtual AssetMetadata GetMetadata(string id)
  140. {
  141. if (m_Cache != null)
  142. {
  143. AssetBase fullAsset;
  144. if (!m_Cache.Get(id, out fullAsset))
  145. return null;
  146. if (fullAsset != null)
  147. return fullAsset.Metadata;
  148. }
  149. if (m_ServerURI == null)
  150. return null;
  151. string uri = m_ServerURI + "/assets/" + id + "/metadata";
  152. return SynchronousRestObjectRequester.MakeRequest<int, AssetMetadata>("GET", uri, 0, m_Auth);
  153. }
  154. public virtual byte[] GetData(string id)
  155. {
  156. if (m_Cache != null)
  157. {
  158. if (!m_Cache.Get(id, out AssetBase fullAsset))
  159. return null;
  160. if (fullAsset != null)
  161. return fullAsset.Data;
  162. }
  163. if (m_ServerURI == null)
  164. return null;
  165. using (RestClient rc = new RestClient(m_ServerURI))
  166. {
  167. rc.AddResourcePath("assets/" + id + "/Data");
  168. rc.RequestMethod = "GET";
  169. using (MemoryStream s = rc.Request(m_Auth))
  170. {
  171. if (s == null || s.Length == 0)
  172. return null;
  173. return s.ToArray();
  174. }
  175. }
  176. }
  177. public virtual bool Get(string id, object sender, AssetRetrieved handler)
  178. {
  179. AssetBase asset = null;
  180. if (m_Cache != null)
  181. {
  182. if (!m_Cache.Get(id, out asset))
  183. return false;
  184. }
  185. if (asset == null && m_ServerURI != null)
  186. {
  187. string uri = m_ServerURI + "/assets/" + id;
  188. lock (m_AssetHandlers)
  189. {
  190. AssetRetrievedEx handlerEx = new AssetRetrievedEx(delegate (AssetBase _asset) { handler(id, sender, _asset); });
  191. List<AssetRetrievedEx> handlers;
  192. if (m_AssetHandlers.TryGetValue(id, out handlers))
  193. {
  194. // Someone else is already loading this asset. It will notify our handler when done.
  195. handlers.Add(handlerEx);
  196. return true;
  197. }
  198. handlers = new List<AssetRetrievedEx>();
  199. handlers.Add(handlerEx);
  200. m_AssetHandlers.Add(id, handlers);
  201. QueuedAssetRequest request = new QueuedAssetRequest();
  202. request.id = id;
  203. request.uri = uri;
  204. Util.FireAndForget(x =>
  205. {
  206. AssetRequestProcessor(request);
  207. });
  208. }
  209. }
  210. else
  211. {
  212. if (asset != null && (asset.Data == null || asset.Data.Length == 0))
  213. asset = null;
  214. handler(id, sender, asset);
  215. }
  216. return true;
  217. }
  218. private class QueuedAssetRequest
  219. {
  220. public string uri;
  221. public string id;
  222. }
  223. private void AssetRequestProcessor(QueuedAssetRequest r)
  224. {
  225. string id = r.id;
  226. try
  227. {
  228. AssetBase a = SynchronousRestObjectRequester.MakeRequest<int, AssetBase>("GET", r.uri, 0, 30000, m_Auth);
  229. if (a != null && m_Cache != null)
  230. m_Cache.Cache(a);
  231. List<AssetRetrievedEx> handlers;
  232. lock (m_AssetHandlers)
  233. {
  234. handlers = m_AssetHandlers[id];
  235. m_AssetHandlers.Remove(id);
  236. }
  237. if(handlers != null)
  238. {
  239. foreach (AssetRetrievedEx h in handlers)
  240. {
  241. try { h.Invoke(a); }
  242. catch { }
  243. }
  244. handlers.Clear();
  245. }
  246. }
  247. catch { }
  248. }
  249. public virtual bool[] AssetsExist(string[] ids)
  250. {
  251. if (m_ServerURI == null)
  252. return null;
  253. string uri = m_ServerURI + "/get_assets_exist";
  254. bool[] exist = null;
  255. try
  256. {
  257. exist = SynchronousRestObjectRequester.MakeRequest<string[], bool[]>("POST", uri, ids, m_Auth);
  258. }
  259. catch (Exception)
  260. {
  261. // This is most likely to happen because the server doesn't support this function,
  262. // so just silently return "doesn't exist" for all the assets.
  263. }
  264. if (exist == null)
  265. exist = new bool[ids.Length];
  266. return exist;
  267. }
  268. string stringUUIDZero = UUID.Zero.ToString();
  269. public virtual string Store(AssetBase asset)
  270. {
  271. // Have to assign the asset ID here. This isn't likely to
  272. // trigger since current callers don't pass emtpy IDs
  273. // We need the asset ID to route the request to the proper
  274. // cluster member, so we can't have the server assign one.
  275. if (asset.ID == string.Empty || asset.ID == stringUUIDZero)
  276. {
  277. if (asset.FullID == UUID.Zero)
  278. {
  279. asset.FullID = UUID.Random();
  280. }
  281. m_log.WarnFormat("[Assets] Zero ID: {0}",asset.Name);
  282. asset.ID = asset.FullID.ToString();
  283. }
  284. if (asset.FullID == UUID.Zero)
  285. {
  286. UUID uuid = UUID.Zero;
  287. if (UUID.TryParse(asset.ID, out uuid))
  288. {
  289. asset.FullID = uuid;
  290. }
  291. if(asset.FullID == UUID.Zero)
  292. {
  293. m_log.WarnFormat("[Assets] Zero IDs: {0}",asset.Name);
  294. asset.FullID = UUID.Random();
  295. asset.ID = asset.FullID.ToString();
  296. }
  297. }
  298. if (m_Cache != null)
  299. m_Cache.Cache(asset);
  300. if (asset.Temporary || asset.Local)
  301. {
  302. return asset.ID;
  303. }
  304. if (m_ServerURI == null)
  305. return null;
  306. string uri = m_ServerURI + "/assets/";
  307. string newID = null;
  308. try
  309. {
  310. newID = SynchronousRestObjectRequester.MakeRequest<AssetBase, string>("POST", uri, asset, 10000, m_Auth);
  311. }
  312. catch
  313. {
  314. newID = null;
  315. }
  316. if (string.IsNullOrEmpty(newID) || newID == stringUUIDZero)
  317. {
  318. return string.Empty;
  319. }
  320. else
  321. {
  322. if (newID != asset.ID)
  323. {
  324. // Placing this here, so that this work with old asset servers that don't send any reply back
  325. // SynchronousRestObjectRequester returns somethins that is not an empty string
  326. asset.ID = newID;
  327. if (m_Cache != null)
  328. m_Cache.Cache(asset);
  329. }
  330. }
  331. return asset.ID;
  332. }
  333. public virtual bool UpdateContent(string id, byte[] data)
  334. {
  335. if (m_ServerURI == null)
  336. return false;
  337. AssetBase asset = null;
  338. m_Cache?.Get(id, out asset);
  339. if (asset == null)
  340. {
  341. AssetMetadata metadata = GetMetadata(id);
  342. if (metadata == null)
  343. return false;
  344. asset = new AssetBase(metadata.FullID, metadata.Name, metadata.Type, UUID.Zero.ToString());
  345. asset.Metadata = metadata;
  346. }
  347. asset.Data = data;
  348. string uri = m_ServerURI + "/assets/" + id;
  349. if (SynchronousRestObjectRequester.MakeRequest<AssetBase, bool>("POST", uri, asset, m_Auth))
  350. {
  351. m_Cache?.Cache(asset, true);
  352. return true;
  353. }
  354. return false;
  355. }
  356. public virtual bool Delete(string id)
  357. {
  358. if (m_ServerURI == null)
  359. return false;
  360. string uri = m_ServerURI + "/assets/" + id;
  361. if (SynchronousRestObjectRequester.MakeRequest<int, bool>("DELETE", uri, 0, m_Auth))
  362. {
  363. if (m_Cache != null)
  364. m_Cache.Expire(id);
  365. return true;
  366. }
  367. return false;
  368. }
  369. }
  370. }