1
0

HGAssetBroker.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 IImprovedAssetCache 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<IImprovedAssetCache>();
  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. asset = m_Cache.Get(id);
  174. if (asset != null)
  175. return asset;
  176. }
  177. if (IsHG(id))
  178. {
  179. asset = m_HGService.Get(id);
  180. if (asset != null)
  181. {
  182. // Now store it locally, if allowed
  183. if (m_AssetPerms.AllowedImport(asset.Type))
  184. m_GridService.Store(asset);
  185. else
  186. return null;
  187. }
  188. }
  189. else
  190. asset = m_GridService.Get(id);
  191. if (m_Cache != null)
  192. m_Cache.Cache(asset);
  193. return asset;
  194. }
  195. public AssetBase GetCached(string id)
  196. {
  197. if (m_Cache != null)
  198. return m_Cache.Get(id);
  199. return null;
  200. }
  201. public AssetMetadata GetMetadata(string id)
  202. {
  203. AssetBase asset = null;
  204. if (m_Cache != null)
  205. {
  206. if (m_Cache != null)
  207. m_Cache.Get(id);
  208. if (asset != null)
  209. return asset.Metadata;
  210. }
  211. AssetMetadata metadata;
  212. if (IsHG(id))
  213. metadata = m_HGService.GetMetadata(id);
  214. else
  215. metadata = m_GridService.GetMetadata(id);
  216. return metadata;
  217. }
  218. public byte[] GetData(string id)
  219. {
  220. AssetBase asset = null;
  221. if (m_Cache != null)
  222. {
  223. if (m_Cache != null)
  224. m_Cache.Get(id);
  225. if (asset != null)
  226. return asset.Data;
  227. }
  228. if (IsHG(id))
  229. return m_HGService.GetData(id);
  230. else
  231. return m_GridService.GetData(id);
  232. }
  233. public bool Get(string id, Object sender, AssetRetrieved handler)
  234. {
  235. AssetBase asset = null;
  236. if (m_Cache != null)
  237. asset = m_Cache.Get(id);
  238. if (asset != null)
  239. {
  240. Util.FireAndForget(delegate { handler(id, sender, asset); }, null, "HGAssetBroker.GotFromCache");
  241. return true;
  242. }
  243. if (IsHG(id))
  244. {
  245. return m_HGService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  246. {
  247. if (m_Cache != null)
  248. m_Cache.Cache(a);
  249. handler(assetID, s, a);
  250. });
  251. }
  252. else
  253. {
  254. return m_GridService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  255. {
  256. if (m_Cache != null)
  257. m_Cache.Cache(a);
  258. handler(assetID, s, a);
  259. });
  260. }
  261. }
  262. public virtual bool[] AssetsExist(string[] ids)
  263. {
  264. int numHG = 0;
  265. foreach (string id in ids)
  266. {
  267. if (IsHG(id))
  268. ++numHG;
  269. }
  270. if (numHG == 0)
  271. return m_GridService.AssetsExist(ids);
  272. else if (numHG == ids.Length)
  273. return m_HGService.AssetsExist(ids);
  274. else
  275. throw new Exception("[HG ASSET CONNECTOR]: AssetsExist: all the assets must be either local or foreign");
  276. }
  277. public string Store(AssetBase asset)
  278. {
  279. bool isHG = IsHG(asset.ID);
  280. if ((m_Cache != null) && !isHG)
  281. // Don't store it in the cache if the asset is to
  282. // be sent to the other grid, because this is already
  283. // a copy of the local asset.
  284. m_Cache.Cache(asset);
  285. if (asset.Local)
  286. {
  287. if (m_Cache != null)
  288. m_Cache.Cache(asset);
  289. return asset.ID;
  290. }
  291. string id;
  292. if (IsHG(asset.ID))
  293. {
  294. if (m_AssetPerms.AllowedExport(asset.Type))
  295. id = m_HGService.Store(asset);
  296. else
  297. return String.Empty;
  298. }
  299. else
  300. id = m_GridService.Store(asset);
  301. if (String.IsNullOrEmpty(id))
  302. return string.Empty;
  303. asset.ID = id;
  304. if (m_Cache != null)
  305. m_Cache.Cache(asset);
  306. return id;
  307. }
  308. public bool UpdateContent(string id, byte[] data)
  309. {
  310. AssetBase asset = null;
  311. if (m_Cache != null)
  312. asset = m_Cache.Get(id);
  313. if (asset != null)
  314. {
  315. asset.Data = data;
  316. m_Cache.Cache(asset);
  317. }
  318. if (IsHG(id))
  319. return m_HGService.UpdateContent(id, data);
  320. else
  321. return m_GridService.UpdateContent(id, data);
  322. }
  323. public bool Delete(string id)
  324. {
  325. if (m_Cache != null)
  326. m_Cache.Expire(id);
  327. bool result = false;
  328. if (IsHG(id))
  329. result = m_HGService.Delete(id);
  330. else
  331. result = m_GridService.Delete(id);
  332. if (result && m_Cache != null)
  333. m_Cache.Expire(id);
  334. return result;
  335. }
  336. }
  337. }