HGAssetBroker.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 Nini.Config;
  29. using System;
  30. using System.Collections.Generic;
  31. using System.Reflection;
  32. using OpenSim.Framework;
  33. using OpenSim.Server.Base;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. using OpenSim.Services.Interfaces;
  37. using OpenMetaverse;
  38. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Asset
  39. {
  40. public class HGAssetBroker :
  41. ISharedRegionModule, IAssetService
  42. {
  43. private static readonly ILog m_log =
  44. LogManager.GetLogger(
  45. MethodBase.GetCurrentMethod().DeclaringType);
  46. private IImprovedAssetCache m_Cache = null;
  47. private IAssetService m_GridService;
  48. private IAssetService m_HGService;
  49. private bool m_Enabled = false;
  50. public string Name
  51. {
  52. get { return "HGAssetBroker"; }
  53. }
  54. public void Initialise(IConfigSource source)
  55. {
  56. IConfig moduleConfig = source.Configs["Modules"];
  57. if (moduleConfig != null)
  58. {
  59. string name = moduleConfig.GetString("AssetServices", "");
  60. if (name == Name)
  61. {
  62. IConfig assetConfig = source.Configs["AssetService"];
  63. if (assetConfig == null)
  64. {
  65. m_log.Error("[HG ASSET CONNECTOR]: AssetService missing from OpenSim.ini");
  66. return;
  67. }
  68. string localDll = assetConfig.GetString("LocalGridAssetService",
  69. String.Empty);
  70. string HGDll = assetConfig.GetString("HypergridAssetService",
  71. String.Empty);
  72. if (localDll == String.Empty)
  73. {
  74. m_log.Error("[HG ASSET CONNECTOR]: No LocalGridAssetService named in section AssetService");
  75. return;
  76. }
  77. if (HGDll == String.Empty)
  78. {
  79. m_log.Error("[HG ASSET CONNECTOR]: No HypergridAssetService named in section AssetService");
  80. return;
  81. }
  82. Object[] args = new Object[] { source };
  83. m_GridService =
  84. ServerUtils.LoadPlugin<IAssetService>(localDll,
  85. args);
  86. m_HGService =
  87. ServerUtils.LoadPlugin<IAssetService>(HGDll,
  88. args);
  89. if (m_GridService == null)
  90. {
  91. m_log.Error("[HG ASSET CONNECTOR]: Can't load local asset service");
  92. return;
  93. }
  94. if (m_HGService == null)
  95. {
  96. m_log.Error("[HG ASSET CONNECTOR]: Can't load hypergrid asset service");
  97. return;
  98. }
  99. m_Enabled = true;
  100. m_log.Info("[HG ASSET CONNECTOR]: HG asset broker enabled");
  101. }
  102. }
  103. }
  104. public void PostInitialise()
  105. {
  106. }
  107. public void Close()
  108. {
  109. }
  110. public void AddRegion(Scene scene)
  111. {
  112. if (!m_Enabled)
  113. return;
  114. scene.RegisterModuleInterface<IAssetService>(this);
  115. }
  116. public void RemoveRegion(Scene scene)
  117. {
  118. }
  119. public void RegionLoaded(Scene scene)
  120. {
  121. if (!m_Enabled)
  122. return;
  123. if (m_Cache == null)
  124. {
  125. m_Cache = scene.RequestModuleInterface<IImprovedAssetCache>();
  126. if (!(m_Cache is ISharedRegionModule))
  127. m_Cache = null;
  128. }
  129. m_log.InfoFormat("[HG ASSET CONNECTOR]: Enabled hypergrid asset broker for region {0}", scene.RegionInfo.RegionName);
  130. if (m_Cache != null)
  131. {
  132. m_log.InfoFormat("[HG ASSET CONNECTOR]: Enabled asset caching for region {0}", scene.RegionInfo.RegionName);
  133. }
  134. }
  135. private bool IsHG(string id)
  136. {
  137. Uri assetUri;
  138. if (Uri.TryCreate(id, UriKind.Absolute, out assetUri) &&
  139. assetUri.Scheme == Uri.UriSchemeHttp)
  140. return true;
  141. return false;
  142. }
  143. public AssetBase Get(string id)
  144. {
  145. //m_log.DebugFormat("[HG ASSET CONNECTOR]: Get {0}", id);
  146. AssetBase asset = null;
  147. if (m_Cache != null)
  148. {
  149. asset = m_Cache.Get(id);
  150. if (asset != null)
  151. return asset;
  152. }
  153. if (IsHG(id))
  154. {
  155. asset = m_HGService.Get(id);
  156. if (asset != null)
  157. {
  158. // Now store it locally
  159. // For now, let me just do it for textures and scripts
  160. if (((AssetType)asset.Type == AssetType.Texture) ||
  161. ((AssetType)asset.Type == AssetType.LSLBytecode) ||
  162. ((AssetType)asset.Type == AssetType.LSLText))
  163. {
  164. m_GridService.Store(asset);
  165. }
  166. }
  167. }
  168. else
  169. asset = m_GridService.Get(id);
  170. if (asset != null)
  171. {
  172. if (m_Cache != null)
  173. m_Cache.Cache(asset);
  174. }
  175. return asset;
  176. }
  177. public AssetMetadata GetMetadata(string id)
  178. {
  179. AssetBase asset = null;
  180. if (m_Cache != null)
  181. {
  182. if (m_Cache != null)
  183. m_Cache.Get(id);
  184. if (asset != null)
  185. return asset.Metadata;
  186. }
  187. AssetMetadata metadata;
  188. if (IsHG(id))
  189. metadata = m_HGService.GetMetadata(id);
  190. else
  191. metadata = m_GridService.GetMetadata(id);
  192. return metadata;
  193. }
  194. public byte[] GetData(string id)
  195. {
  196. AssetBase asset = null;
  197. if (m_Cache != null)
  198. {
  199. if (m_Cache != null)
  200. m_Cache.Get(id);
  201. if (asset != null)
  202. return asset.Data;
  203. }
  204. if (IsHG(id))
  205. asset = m_HGService.Get(id);
  206. else
  207. asset = m_GridService.Get(id);
  208. if (asset != null)
  209. {
  210. if (m_Cache != null)
  211. m_Cache.Cache(asset);
  212. return asset.Data;
  213. }
  214. return null;
  215. }
  216. public bool Get(string id, Object sender, AssetRetrieved handler)
  217. {
  218. AssetBase asset = null;
  219. if (m_Cache != null)
  220. asset = m_Cache.Get(id);
  221. if (asset != null)
  222. {
  223. handler.BeginInvoke(id, sender, asset, null, null);
  224. return true;
  225. }
  226. if (IsHG(id))
  227. {
  228. return m_HGService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  229. {
  230. if (a != null && m_Cache != null)
  231. m_Cache.Cache(a);
  232. handler(assetID, s, a);
  233. });
  234. }
  235. else
  236. {
  237. return m_GridService.Get(id, sender, delegate (string assetID, Object s, AssetBase a)
  238. {
  239. if (a != null && m_Cache != null)
  240. m_Cache.Cache(a);
  241. handler(assetID, s, a);
  242. });
  243. }
  244. }
  245. public string Store(AssetBase asset)
  246. {
  247. bool isHG = IsHG(asset.ID);
  248. if ((m_Cache != null) && !isHG)
  249. // Don't store it in the cache if the asset is to
  250. // be sent to the other grid, because this is already
  251. // a copy of the local asset.
  252. m_Cache.Cache(asset);
  253. if (asset.Temporary || asset.Local)
  254. return asset.ID;
  255. if (IsHG(asset.ID))
  256. return m_HGService.Store(asset);
  257. else
  258. return m_GridService.Store(asset);
  259. }
  260. public bool UpdateContent(string id, byte[] data)
  261. {
  262. AssetBase asset = null;
  263. if (m_Cache != null)
  264. asset = m_Cache.Get(id);
  265. if (asset != null)
  266. {
  267. asset.Data = data;
  268. m_Cache.Cache(asset);
  269. }
  270. if (IsHG(id))
  271. return m_HGService.UpdateContent(id, data);
  272. else
  273. return m_GridService.UpdateContent(id, data);
  274. }
  275. public bool Delete(string id)
  276. {
  277. if (m_Cache != null)
  278. m_Cache.Expire(id);
  279. if (IsHG(id))
  280. return m_HGService.Delete(id);
  281. else
  282. return m_GridService.Delete(id);
  283. }
  284. }
  285. }