HGAssetBroker.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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.Generic;
  32. using System.Reflection;
  33. using OpenSim.Framework;
  34. using OpenSim.Server.Base;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Services.Interfaces;
  38. using OpenMetaverse;
  39. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
  40. {
  41. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "HGAssetBroker")]
  42. public class HGAssetBroker : ISharedRegionModule, IAssetService
  43. {
  44. private static readonly ILog m_log =
  45. LogManager.GetLogger(
  46. MethodBase.GetCurrentMethod().DeclaringType);
  47. private IAssetCache m_Cache = null;
  48. private IAssetService m_GridService;
  49. private IAssetService m_HGService;
  50. private Scene m_aScene;
  51. private string m_LocalAssetServiceURI;
  52. private bool m_Enabled = false;
  53. private AssetPermissions m_AssetPerms;
  54. public Type ReplaceableInterface
  55. {
  56. get { return null; }
  57. }
  58. public string Name
  59. {
  60. get { return "HGAssetBroker"; }
  61. }
  62. public HGAssetBroker() {}
  63. public HGAssetBroker(IConfigSource config)
  64. {
  65. Initialise(config);
  66. }
  67. public void Initialise(IConfigSource source)
  68. {
  69. IConfig moduleConfig = source.Configs["Modules"];
  70. if (moduleConfig != null)
  71. {
  72. string name = moduleConfig.GetString("AssetServices", "");
  73. if (name == Name)
  74. {
  75. IConfig assetConfig = source.Configs["AssetService"];
  76. if (assetConfig == null)
  77. {
  78. m_log.Error("[HG ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
  79. return;
  80. }
  81. string localDll = assetConfig.GetString("LocalGridAssetService",
  82. String.Empty);
  83. string HGDll = assetConfig.GetString("HypergridAssetService",
  84. String.Empty);
  85. if (localDll == String.Empty)
  86. {
  87. m_log.Error("[HG ASSET CONNECTOR]: No LocalGridAssetService named in section AssetService");
  88. return;
  89. }
  90. if (HGDll == String.Empty)
  91. {
  92. m_log.Error("[HG ASSET CONNECTOR]: No HypergridAssetService named in section AssetService");
  93. return;
  94. }
  95. Object[] args = new Object[] { source };
  96. m_GridService =
  97. ServerUtils.LoadPlugin<IAssetService>(localDll,
  98. args);
  99. m_HGService =
  100. ServerUtils.LoadPlugin<IAssetService>(HGDll,
  101. args);
  102. if (m_GridService == null)
  103. {
  104. m_log.Error("[HG ASSET CONNECTOR]: Can't load local asset service");
  105. return;
  106. }
  107. if (m_HGService == null)
  108. {
  109. m_log.Error("[HG ASSET CONNECTOR]: Can't load hypergrid asset service");
  110. return;
  111. }
  112. m_LocalAssetServiceURI = assetConfig.GetString("AssetServerURI", string.Empty);
  113. if (m_LocalAssetServiceURI == string.Empty)
  114. {
  115. IConfig netConfig = source.Configs["Network"];
  116. m_LocalAssetServiceURI = netConfig.GetString("asset_server_url", string.Empty);
  117. }
  118. if (m_LocalAssetServiceURI != string.Empty)
  119. m_LocalAssetServiceURI = m_LocalAssetServiceURI.Trim('/');
  120. IConfig hgConfig = source.Configs["HGAssetService"];
  121. m_AssetPerms = new AssetPermissions(hgConfig); // it's ok if arg is null
  122. m_Enabled = true;
  123. m_log.Info("[HG ASSET CONNECTOR]: HG asset broker enabled");
  124. }
  125. }
  126. }
  127. public void PostInitialise()
  128. {
  129. }
  130. public void Close()
  131. {
  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. m_log.InfoFormat("[HG ASSET CONNECTOR]: Enabled hypergrid asset broker for region {0}", scene.RegionInfo.RegionName);
  154. if (m_Cache != null)
  155. {
  156. m_log.InfoFormat("[HG ASSET CONNECTOR]: Enabled asset caching for region {0}", scene.RegionInfo.RegionName);
  157. }
  158. }
  159. private bool IsHG(string id)
  160. {
  161. Uri assetUri;
  162. if (Uri.TryCreate(id, UriKind.Absolute, out assetUri) &&
  163. assetUri.Scheme == Uri.UriSchemeHttp)
  164. return true;
  165. return false;
  166. }
  167. public AssetBase Get(string id)
  168. {
  169. //m_log.DebugFormat("[HG ASSET CONNECTOR]: Get {0}", id);
  170. AssetBase asset = null;
  171. if (m_Cache != null)
  172. {
  173. if (!m_Cache.Get(id, out asset))
  174. return null;
  175. if (asset != null)
  176. return asset;
  177. }
  178. if (IsHG(id))
  179. {
  180. asset = m_HGService.Get(id);
  181. if (asset != null)
  182. {
  183. // Now store it locally, if allowed
  184. if (m_AssetPerms.AllowedImport(asset.Type))
  185. m_GridService.Store(asset);
  186. else
  187. return null;
  188. }
  189. }
  190. else
  191. asset = m_GridService.Get(id);
  192. if (m_Cache != null)
  193. m_Cache.Cache(asset);
  194. return asset;
  195. }
  196. public AssetBase GetCached(string id)
  197. {
  198. AssetBase asset = null;
  199. if (m_Cache != null)
  200. m_Cache.Get(id, out asset);
  201. return asset;
  202. }
  203. public AssetMetadata GetMetadata(string id)
  204. {
  205. AssetBase asset = null;
  206. if (m_Cache != null)
  207. {
  208. if (!m_Cache.Get(id, out asset))
  209. return null;
  210. if (asset != null)
  211. return asset.Metadata;
  212. }
  213. AssetMetadata metadata;
  214. if (IsHG(id))
  215. metadata = m_HGService.GetMetadata(id);
  216. else
  217. metadata = m_GridService.GetMetadata(id);
  218. return metadata;
  219. }
  220. public byte[] GetData(string id)
  221. {
  222. AssetBase asset = null;
  223. if (m_Cache != null)
  224. {
  225. if (!m_Cache.Get(id, out asset))
  226. return null;
  227. if (asset != null)
  228. return asset.Data;
  229. }
  230. if (IsHG(id))
  231. return m_HGService.GetData(id);
  232. else
  233. return m_GridService.GetData(id);
  234. }
  235. public bool Get(string id, Object sender, AssetRetrieved handler)
  236. {
  237. AssetBase asset = null;
  238. if (m_Cache != null)
  239. {
  240. if (!m_Cache.Get(id, out asset))
  241. return false;
  242. }
  243. if (asset != null)
  244. {
  245. Util.FireAndForget(delegate { handler(id, sender, asset); }, null, "HGAssetBroker.GotFromCache");
  246. return true;
  247. }
  248. if (IsHG(id))
  249. {
  250. return m_HGService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  251. {
  252. if (m_Cache != null)
  253. m_Cache.Cache(a);
  254. handler(assetID, s, a);
  255. });
  256. }
  257. else
  258. {
  259. return m_GridService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  260. {
  261. if (m_Cache != null)
  262. m_Cache.Cache(a);
  263. handler(assetID, s, a);
  264. });
  265. }
  266. }
  267. public virtual bool[] AssetsExist(string[] ids)
  268. {
  269. int numHG = 0;
  270. foreach (string id in ids)
  271. {
  272. if (IsHG(id))
  273. ++numHG;
  274. }
  275. if (numHG == 0)
  276. return m_GridService.AssetsExist(ids);
  277. else if (numHG == ids.Length)
  278. return m_HGService.AssetsExist(ids);
  279. else
  280. throw new Exception("[HG ASSET CONNECTOR]: AssetsExist: all the assets must be either local or foreign");
  281. }
  282. public string Store(AssetBase asset)
  283. {
  284. if (asset.Local || asset.Temporary)
  285. {
  286. if (m_Cache != null)
  287. m_Cache.Cache(asset);
  288. return asset.ID;
  289. }
  290. bool isHG = IsHG(asset.ID);
  291. if ((m_Cache != null) && !isHG)
  292. // Don't store it in the cache if the asset is to
  293. // be sent to the other grid, because this is already
  294. // a copy of the local asset.
  295. m_Cache.Cache(asset);
  296. string id;
  297. if (isHG)
  298. {
  299. if (m_AssetPerms.AllowedExport(asset.Type))
  300. id = m_HGService.Store(asset);
  301. else
  302. return String.Empty;
  303. }
  304. else
  305. id = m_GridService.Store(asset);
  306. if (String.IsNullOrEmpty(id))
  307. return string.Empty;
  308. if(asset.ID != id)
  309. {
  310. asset.ID = id;
  311. if (m_Cache != null)
  312. m_Cache.Cache(asset);
  313. }
  314. return id;
  315. }
  316. public bool UpdateContent(string id, byte[] data)
  317. {
  318. AssetBase asset = null;
  319. if (m_Cache != null)
  320. m_Cache.Get(id, out asset);
  321. if (asset != null)
  322. {
  323. asset.Data = data;
  324. m_Cache.Cache(asset);
  325. }
  326. if (IsHG(id))
  327. return m_HGService.UpdateContent(id, data);
  328. else
  329. return m_GridService.UpdateContent(id, data);
  330. }
  331. public bool Delete(string id)
  332. {
  333. if (m_Cache != null)
  334. m_Cache.Expire(id);
  335. bool result = false;
  336. if (IsHG(id))
  337. result = m_HGService.Delete(id);
  338. else
  339. result = m_GridService.Delete(id);
  340. if (result && m_Cache != null)
  341. m_Cache.Expire(id);
  342. return result;
  343. }
  344. }
  345. }