HGAssetBroker.cs 11 KB

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