HGAssetBroker.cs 12 KB

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