RegionAssetConnectorModule.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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 Mono.Addins;
  29. using Nini.Config;
  30. using System;
  31. using System.Collections.Concurrent;
  32. using System.Reflection;
  33. using System.Collections.Generic;
  34. using System.Threading;
  35. using System.Timers;
  36. using OpenMetaverse;
  37. using OpenSim.Framework;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Framework.Monitoring;
  40. using OpenSim.Region.Framework.Scenes;
  41. using OpenSim.Server.Base;
  42. using OpenSim.Services.Interfaces;
  43. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
  44. {
  45. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "RegionAssetConnector")]
  46. public class RegionAssetConnector : ISharedRegionModule, IAssetService
  47. {
  48. private static readonly ILog m_log = LogManager.GetLogger( MethodBase.GetCurrentMethod().DeclaringType);
  49. private delegate void AssetRetrievedEx(AssetBase asset);
  50. private bool m_Enabled = false;
  51. private Scene m_aScene;
  52. private IAssetCache m_Cache;
  53. private IAssetService m_localConnector;
  54. private IAssetService m_HGConnector;
  55. private AssetPermissions m_AssetPerms;
  56. //const int MAXSENDRETRIESLEN = 30;
  57. //private List<AssetBase>[] m_sendRetries;
  58. //private List<string>[] m_sendCachedRetries;
  59. //private System.Timers.Timer m_retryTimer;
  60. //private int m_retryCounter;
  61. //private bool m_inRetries;
  62. private Dictionary<string, List<AssetRetrievedEx>> m_AssetHandlers = new Dictionary<string, List<AssetRetrievedEx>>();
  63. private ObjectJobEngine m_requestQueue;
  64. public Type ReplaceableInterface
  65. {
  66. get { return null; }
  67. }
  68. public string Name
  69. {
  70. get { return "RegionAssetConnector"; }
  71. }
  72. public RegionAssetConnector() {}
  73. public RegionAssetConnector(IConfigSource config)
  74. {
  75. Initialise(config);
  76. }
  77. public void Initialise(IConfigSource source)
  78. {
  79. IConfig moduleConfig = source.Configs["Modules"];
  80. if (moduleConfig != null)
  81. {
  82. string name = moduleConfig.GetString("AssetServices", "");
  83. if (name == Name)
  84. {
  85. IConfig assetConfig = source.Configs["AssetService"];
  86. if (assetConfig == null)
  87. {
  88. m_log.Error("[REGIONASSETCONNECTOR]: AssetService missing from configuration files");
  89. throw new Exception("Region asset connector init error");
  90. }
  91. string localGridConnector = assetConfig.GetString("LocalGridAssetService", string.Empty);
  92. if(string.IsNullOrEmpty(localGridConnector))
  93. {
  94. m_log.Error("[REGIONASSETCONNECTOR]: LocalGridAssetService missing from configuration files");
  95. throw new Exception("Region asset connector init error");
  96. }
  97. object[] args = new object[] { source };
  98. m_localConnector = ServerUtils.LoadPlugin<IAssetService>(localGridConnector, args);
  99. if (m_localConnector == null)
  100. {
  101. m_log.Error("[REGIONASSETCONNECTOR]: Fail to load local asset service " + localGridConnector);
  102. throw new Exception("Region asset connector init error");
  103. }
  104. string HGConnector = assetConfig.GetString("HypergridAssetService", string.Empty);
  105. if(!string.IsNullOrEmpty(HGConnector))
  106. {
  107. m_HGConnector = ServerUtils.LoadPlugin<IAssetService>(HGConnector, args);
  108. if (m_HGConnector == null)
  109. {
  110. m_log.Error("[REGIONASSETCONNECTOR]: Fail to load HG asset service " + HGConnector);
  111. throw new Exception("Region asset connector init error");
  112. }
  113. IConfig hgConfig = source.Configs["HGAssetService"];
  114. if (hgConfig != null)
  115. m_AssetPerms = new AssetPermissions(hgConfig);
  116. }
  117. m_requestQueue = new ObjectJobEngine(AssetRequestProcessor, "GetAssetsWorkers", 2000, 2);
  118. m_Enabled = true;
  119. m_log.Info("[REGIONASSETCONNECTOR]: enabled");
  120. }
  121. }
  122. }
  123. public void PostInitialise()
  124. {
  125. }
  126. public void Close()
  127. {
  128. if (!m_Enabled)
  129. return;
  130. m_requestQueue.Dispose();
  131. m_requestQueue = null;
  132. }
  133. public void AddRegion(Scene scene)
  134. {
  135. if (!m_Enabled)
  136. return;
  137. m_aScene = scene;
  138. m_aScene.RegisterModuleInterface<IAssetService>(this);
  139. }
  140. public void RemoveRegion(Scene scene)
  141. {
  142. }
  143. public void RegionLoaded(Scene scene)
  144. {
  145. if (!m_Enabled)
  146. return;
  147. if (m_Cache == null)
  148. {
  149. m_Cache = scene.RequestModuleInterface<IAssetCache>();
  150. if (!(m_Cache is ISharedRegionModule))
  151. m_Cache = null;
  152. }
  153. if(m_HGConnector == null)
  154. {
  155. if (m_Cache != null)
  156. m_log.InfoFormat("[REGIONASSETCONNECTOR]: active with cache for region {0}", scene.RegionInfo.RegionName);
  157. else
  158. m_log.InfoFormat("[REGIONASSETCONNECTOR]: active without cache for region {0}", scene.RegionInfo.RegionName);
  159. }
  160. else
  161. {
  162. if (m_Cache != null)
  163. m_log.InfoFormat("[REGIONASSETCONNECTOR]: active with HG and cache for region {0}", scene.RegionInfo.RegionName);
  164. else
  165. m_log.InfoFormat("[REGIONASSETCONNECTOR]: active with HG and without cache for region {0}", scene.RegionInfo.RegionName);
  166. }
  167. }
  168. private bool IsHG(string id)
  169. {
  170. return id.Length > 0 && (id[0] == 'h' || id[0] == 'H');
  171. }
  172. public AssetBase GetCached(string id)
  173. {
  174. AssetBase asset = null;
  175. if (m_Cache != null)
  176. m_Cache.Get(id, out asset);
  177. return asset;
  178. }
  179. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  180. private AssetBase GetFromLocal(string id)
  181. {
  182. return m_localConnector.Get(id);
  183. }
  184. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  185. private AssetBase GetFromForeign(string id, string ForeignAssetService)
  186. {
  187. if (m_HGConnector == null || string.IsNullOrEmpty(ForeignAssetService))
  188. return null;
  189. return m_HGConnector.Get(id , ForeignAssetService, true);
  190. }
  191. public AssetBase GetForeign(string id)
  192. {
  193. int type = Util.ParseForeignAssetID(id, out string uri, out string uuidstr);
  194. if (type < 0)
  195. return null;
  196. AssetBase asset = null;
  197. if (m_Cache != null)
  198. {
  199. asset = m_Cache.GetCached(uuidstr);
  200. if(asset != null)
  201. return asset;
  202. }
  203. asset = GetFromLocal(uuidstr);
  204. if (asset != null || type == 0)
  205. return asset;
  206. return GetFromForeign(uuidstr, uri);
  207. }
  208. public AssetBase Get(string id)
  209. {
  210. //m_log.DebugFormat("[HG ASSET CONNECTOR]: Get {0}", id);
  211. AssetBase asset = null;
  212. if (IsHG(id))
  213. {
  214. asset = GetForeign(id);
  215. if (asset != null)
  216. {
  217. // Now store it locally, if allowed
  218. if (m_AssetPerms != null && !m_AssetPerms.AllowedImport(asset.Type))
  219. return null;
  220. Store(asset);
  221. }
  222. }
  223. else
  224. {
  225. if (m_Cache != null)
  226. {
  227. if(!m_Cache.Get(id, out asset))
  228. return null;
  229. if (asset != null)
  230. return asset;
  231. }
  232. asset = GetFromLocal(id);
  233. if(m_Cache != null)
  234. {
  235. if(asset == null)
  236. m_Cache.CacheNegative(id);
  237. else
  238. m_Cache.Cache(asset);
  239. }
  240. }
  241. return asset;
  242. }
  243. public AssetBase Get(string id, string ForeignAssetService, bool StoreOnLocalGrid)
  244. {
  245. // assumes id and ForeignAssetService are valid and resolved
  246. AssetBase asset = null;
  247. if (m_Cache != null)
  248. {
  249. asset = m_Cache.GetCached(id);
  250. if (asset != null)
  251. return asset;
  252. }
  253. asset = GetFromLocal(id);
  254. if (asset == null)
  255. {
  256. asset = GetFromForeign(id, ForeignAssetService);
  257. if (asset != null)
  258. {
  259. if (m_AssetPerms != null && !m_AssetPerms.AllowedImport(asset.Type))
  260. {
  261. if (m_Cache != null)
  262. m_Cache.CacheNegative(id);
  263. return null;
  264. }
  265. if(StoreOnLocalGrid)
  266. Store(asset);
  267. else if (m_Cache != null)
  268. m_Cache.Cache(asset);
  269. }
  270. else if (m_Cache != null)
  271. m_Cache.CacheNegative(id);
  272. }
  273. else if (m_Cache != null)
  274. m_Cache.Cache(asset);
  275. return asset;
  276. }
  277. public AssetMetadata GetMetadata(string id)
  278. {
  279. AssetBase asset = Get(id);
  280. if (asset != null)
  281. return asset.Metadata;
  282. return null;
  283. }
  284. public byte[] GetData(string id)
  285. {
  286. AssetBase asset = Get(id);
  287. if (asset != null)
  288. return asset.Data;
  289. return null;
  290. }
  291. public virtual bool Get(string id, object sender, AssetRetrieved callBack)
  292. {
  293. AssetBase asset = null;
  294. if (m_Cache != null)
  295. {
  296. if (!m_Cache.Get(id, out asset))
  297. return false;
  298. }
  299. if (asset == null)
  300. {
  301. lock (m_AssetHandlers)
  302. {
  303. AssetRetrievedEx handlerEx = new AssetRetrievedEx(delegate (AssetBase _asset) { callBack(id, sender, _asset); });
  304. List<AssetRetrievedEx> handlers;
  305. if (m_AssetHandlers.TryGetValue(id, out handlers))
  306. {
  307. // Someone else is already loading this asset. It will notify our handler when done.
  308. handlers.Add(handlerEx);
  309. return true;
  310. }
  311. handlers = new List<AssetRetrievedEx>();
  312. handlers.Add(handlerEx);
  313. m_AssetHandlers.Add(id, handlers);
  314. m_requestQueue.Enqueue(id);
  315. }
  316. }
  317. else
  318. {
  319. if (asset != null && (asset.Data == null || asset.Data.Length == 0))
  320. asset = null;
  321. callBack(id, sender, asset);
  322. }
  323. return true;
  324. }
  325. private void AssetRequestProcessor(object o)
  326. {
  327. string id = o as string;
  328. if(id == null)
  329. return;
  330. try
  331. {
  332. AssetBase a = Get(id);
  333. List<AssetRetrievedEx> handlers;
  334. lock (m_AssetHandlers)
  335. {
  336. handlers = m_AssetHandlers[id];
  337. m_AssetHandlers.Remove(id);
  338. }
  339. if (handlers != null)
  340. {
  341. Util.FireAndForget(x =>
  342. {
  343. foreach (AssetRetrievedEx h in handlers)
  344. {
  345. try
  346. {
  347. h.Invoke(a);
  348. }
  349. catch { }
  350. }
  351. handlers.Clear();
  352. });
  353. }
  354. }
  355. catch { }
  356. }
  357. public bool[] AssetsExist(string[] ids)
  358. {
  359. int numHG = 0;
  360. foreach (string id in ids)
  361. {
  362. if (IsHG(id))
  363. ++numHG;
  364. }
  365. if(numHG == 0)
  366. return m_localConnector.AssetsExist(ids);
  367. else if (m_HGConnector != null)
  368. return m_HGConnector.AssetsExist(ids);
  369. return null;
  370. }
  371. private readonly string stringUUIDZero = UUID.Zero.ToString();
  372. public string Store(AssetBase asset)
  373. {
  374. string id;
  375. if (IsHG(asset.ID))
  376. {
  377. if (asset.Local || asset.Temporary)
  378. return null;
  379. id = StoreForeign(asset);
  380. if (m_Cache != null)
  381. {
  382. if (!string.IsNullOrEmpty(id) && !id.Equals(stringUUIDZero))
  383. m_Cache.Cache(asset);
  384. }
  385. return id;
  386. }
  387. if (m_Cache != null)
  388. {
  389. m_Cache.Cache(asset);
  390. if (asset.Local || asset.Temporary)
  391. return asset.ID;
  392. }
  393. id = StoreLocal(asset);
  394. if (string.IsNullOrEmpty(id))
  395. return string.Empty;
  396. return id;
  397. }
  398. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  399. private string StoreForeign(AssetBase asset)
  400. {
  401. if (m_HGConnector == null)
  402. return string.Empty;
  403. if (m_AssetPerms != null && !m_AssetPerms.AllowedExport(asset.Type))
  404. return string.Empty;
  405. return m_HGConnector.Store(asset);
  406. }
  407. [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
  408. private string StoreLocal(AssetBase asset)
  409. {
  410. return m_localConnector.Store(asset);
  411. }
  412. public bool UpdateContent(string id, byte[] data)
  413. {
  414. if (IsHG(id))
  415. return false;
  416. return m_localConnector.UpdateContent(id, data);
  417. }
  418. public bool Delete(string id)
  419. {
  420. if (IsHG(id))
  421. return false;
  422. return m_localConnector.Delete(id);
  423. }
  424. }
  425. }