HGAssetBroker.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 void Initialise(IConfigSource source)
  63. {
  64. IConfig moduleConfig = source.Configs["Modules"];
  65. if (moduleConfig != null)
  66. {
  67. string name = moduleConfig.GetString("AssetServices", "");
  68. if (name == Name)
  69. {
  70. IConfig assetConfig = source.Configs["AssetService"];
  71. if (assetConfig == null)
  72. {
  73. m_log.Error("[HG ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
  74. return;
  75. }
  76. string localDll = assetConfig.GetString("LocalGridAssetService",
  77. String.Empty);
  78. string HGDll = assetConfig.GetString("HypergridAssetService",
  79. String.Empty);
  80. if (localDll == String.Empty)
  81. {
  82. m_log.Error("[HG ASSET CONNECTOR]: No LocalGridAssetService named in section AssetService");
  83. return;
  84. }
  85. if (HGDll == String.Empty)
  86. {
  87. m_log.Error("[HG ASSET CONNECTOR]: No HypergridAssetService named in section AssetService");
  88. return;
  89. }
  90. Object[] args = new Object[] { source };
  91. m_GridService =
  92. ServerUtils.LoadPlugin<IAssetService>(localDll,
  93. args);
  94. m_HGService =
  95. ServerUtils.LoadPlugin<IAssetService>(HGDll,
  96. args);
  97. if (m_GridService == null)
  98. {
  99. m_log.Error("[HG ASSET CONNECTOR]: Can't load local asset service");
  100. return;
  101. }
  102. if (m_HGService == null)
  103. {
  104. m_log.Error("[HG ASSET CONNECTOR]: Can't load hypergrid asset service");
  105. return;
  106. }
  107. m_LocalAssetServiceURI = assetConfig.GetString("AssetServerURI", string.Empty);
  108. if (m_LocalAssetServiceURI == string.Empty)
  109. {
  110. IConfig netConfig = source.Configs["Network"];
  111. m_LocalAssetServiceURI = netConfig.GetString("asset_server_url", string.Empty);
  112. }
  113. if (m_LocalAssetServiceURI != string.Empty)
  114. m_LocalAssetServiceURI = m_LocalAssetServiceURI.Trim('/');
  115. IConfig hgConfig = source.Configs["HGAssetService"];
  116. m_AssetPerms = new AssetPermissions(hgConfig); // it's ok if arg is null
  117. m_Enabled = true;
  118. m_log.Info("[HG ASSET CONNECTOR]: HG asset broker enabled");
  119. }
  120. }
  121. }
  122. public void PostInitialise()
  123. {
  124. }
  125. public void Close()
  126. {
  127. }
  128. public void AddRegion(Scene scene)
  129. {
  130. if (!m_Enabled)
  131. return;
  132. m_aScene = scene;
  133. m_aScene.RegisterModuleInterface<IAssetService>(this);
  134. }
  135. public void RemoveRegion(Scene scene)
  136. {
  137. }
  138. public void RegionLoaded(Scene scene)
  139. {
  140. if (!m_Enabled)
  141. return;
  142. if (m_Cache == null)
  143. {
  144. m_Cache = scene.RequestModuleInterface<IImprovedAssetCache>();
  145. if (!(m_Cache is ISharedRegionModule))
  146. m_Cache = null;
  147. }
  148. m_log.InfoFormat("[HG ASSET CONNECTOR]: Enabled hypergrid asset broker for region {0}", scene.RegionInfo.RegionName);
  149. if (m_Cache != null)
  150. {
  151. m_log.InfoFormat("[HG ASSET CONNECTOR]: Enabled asset caching for region {0}", scene.RegionInfo.RegionName);
  152. }
  153. }
  154. private bool IsHG(string id)
  155. {
  156. Uri assetUri;
  157. if (Uri.TryCreate(id, UriKind.Absolute, out assetUri) &&
  158. assetUri.Scheme == Uri.UriSchemeHttp)
  159. return true;
  160. return false;
  161. }
  162. public AssetBase Get(string id)
  163. {
  164. //m_log.DebugFormat("[HG ASSET CONNECTOR]: Get {0}", id);
  165. AssetBase asset = null;
  166. if (m_Cache != null)
  167. {
  168. asset = m_Cache.Get(id);
  169. if (asset != null)
  170. return asset;
  171. }
  172. if (IsHG(id))
  173. {
  174. asset = m_HGService.Get(id);
  175. if (asset != null)
  176. {
  177. // Now store it locally, if allowed
  178. if (m_AssetPerms.AllowedImport(asset.Type))
  179. m_GridService.Store(asset);
  180. else
  181. return null;
  182. }
  183. }
  184. else
  185. asset = m_GridService.Get(id);
  186. if (m_Cache != null)
  187. m_Cache.Cache(asset);
  188. return asset;
  189. }
  190. public AssetBase GetCached(string id)
  191. {
  192. if (m_Cache != null)
  193. return m_Cache.Get(id);
  194. return null;
  195. }
  196. public AssetMetadata GetMetadata(string id)
  197. {
  198. AssetBase asset = null;
  199. if (m_Cache != null)
  200. {
  201. if (m_Cache != null)
  202. m_Cache.Get(id);
  203. if (asset != null)
  204. return asset.Metadata;
  205. }
  206. AssetMetadata metadata;
  207. if (IsHG(id))
  208. metadata = m_HGService.GetMetadata(id);
  209. else
  210. metadata = m_GridService.GetMetadata(id);
  211. return metadata;
  212. }
  213. public byte[] GetData(string id)
  214. {
  215. AssetBase asset = null;
  216. if (m_Cache != null)
  217. {
  218. if (m_Cache != null)
  219. m_Cache.Get(id);
  220. if (asset != null)
  221. return asset.Data;
  222. }
  223. if (IsHG(id))
  224. return m_HGService.GetData(id);
  225. else
  226. return m_GridService.GetData(id);
  227. }
  228. public bool Get(string id, Object sender, AssetRetrieved handler)
  229. {
  230. AssetBase asset = null;
  231. if (m_Cache != null)
  232. asset = m_Cache.Get(id);
  233. if (asset != null)
  234. {
  235. Util.FireAndForget(delegate { handler(id, sender, asset); });
  236. return true;
  237. }
  238. if (IsHG(id))
  239. {
  240. return m_HGService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  241. {
  242. if (m_Cache != null)
  243. m_Cache.Cache(a);
  244. handler(assetID, s, a);
  245. });
  246. }
  247. else
  248. {
  249. return m_GridService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  250. {
  251. if (m_Cache != null)
  252. m_Cache.Cache(a);
  253. handler(assetID, s, a);
  254. });
  255. }
  256. }
  257. public string Store(AssetBase asset)
  258. {
  259. bool isHG = IsHG(asset.ID);
  260. if ((m_Cache != null) && !isHG)
  261. // Don't store it in the cache if the asset is to
  262. // be sent to the other grid, because this is already
  263. // a copy of the local asset.
  264. m_Cache.Cache(asset);
  265. if (asset.Local)
  266. {
  267. if (m_Cache != null)
  268. m_Cache.Cache(asset);
  269. return asset.ID;
  270. }
  271. string id = string.Empty;
  272. if (IsHG(asset.ID))
  273. {
  274. if (m_AssetPerms.AllowedExport(asset.Type))
  275. id = m_HGService.Store(asset);
  276. else
  277. return String.Empty;
  278. }
  279. else
  280. id = m_GridService.Store(asset);
  281. if (id != String.Empty)
  282. {
  283. // Placing this here, so that this work with old asset servers that don't send any reply back
  284. // SynchronousRestObjectRequester returns somethins that is not an empty string
  285. if (id != null)
  286. asset.ID = id;
  287. if (m_Cache != null)
  288. m_Cache.Cache(asset);
  289. }
  290. return id;
  291. }
  292. public bool UpdateContent(string id, byte[] data)
  293. {
  294. AssetBase asset = null;
  295. if (m_Cache != null)
  296. asset = m_Cache.Get(id);
  297. if (asset != null)
  298. {
  299. asset.Data = data;
  300. m_Cache.Cache(asset);
  301. }
  302. if (IsHG(id))
  303. return m_HGService.UpdateContent(id, data);
  304. else
  305. return m_GridService.UpdateContent(id, data);
  306. }
  307. public bool Delete(string id)
  308. {
  309. if (m_Cache != null)
  310. m_Cache.Expire(id);
  311. bool result = false;
  312. if (IsHG(id))
  313. result = m_HGService.Delete(id);
  314. else
  315. result = m_GridService.Delete(id);
  316. if (result && m_Cache != null)
  317. m_Cache.Expire(id);
  318. return result;
  319. }
  320. }
  321. }