HGAssetMapper.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Threading;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Communications.Cache;
  35. using OpenSim.Framework.Communications.Clients;
  36. using OpenSim.Region.Framework.Scenes.Serialization;
  37. using OpenSim.Region.Framework.Interfaces;
  38. using OpenSim.Services.Interfaces;
  39. //using HyperGrid.Framework;
  40. //using OpenSim.Region.Communications.Hypergrid;
  41. namespace OpenSim.Region.Framework.Scenes.Hypergrid
  42. {
  43. public class HGAssetMapper
  44. {
  45. #region Fields
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. // This maps between inventory server urls and inventory server clients
  48. // private Dictionary<string, InventoryClient> m_inventoryServers = new Dictionary<string, InventoryClient>();
  49. private Scene m_scene;
  50. private IHyperAssetService m_hyper;
  51. IHyperAssetService HyperlinkAssets
  52. {
  53. get
  54. {
  55. if (m_hyper == null)
  56. m_hyper = m_scene.RequestModuleInterface<IHyperAssetService>();
  57. return m_hyper;
  58. }
  59. }
  60. #endregion
  61. #region Constructor
  62. public HGAssetMapper(Scene scene)
  63. {
  64. m_scene = scene;
  65. }
  66. #endregion
  67. #region Internal functions
  68. // private string UserAssetURL(UUID userID)
  69. // {
  70. // CachedUserInfo uinfo = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(userID);
  71. // if (uinfo != null)
  72. // return (uinfo.UserProfile.UserAssetURI == "") ? null : uinfo.UserProfile.UserAssetURI;
  73. // return null;
  74. // }
  75. // private string UserInventoryURL(UUID userID)
  76. // {
  77. // CachedUserInfo uinfo = m_scene.CommsManager.UserProfileCacheService.GetUserDetails(userID);
  78. // if (uinfo != null)
  79. // return (uinfo.UserProfile.UserInventoryURI == "") ? null : uinfo.UserProfile.UserInventoryURI;
  80. // return null;
  81. // }
  82. public AssetBase FetchAsset(string url, UUID assetID)
  83. {
  84. AssetBase asset = m_scene.AssetService.Get(url + "/" + assetID.ToString());
  85. if (asset != null)
  86. {
  87. m_log.DebugFormat("[HGScene]: Copied asset {0} from {1} to local asset server. ", asset.ID, url);
  88. return asset;
  89. }
  90. return null;
  91. }
  92. public bool PostAsset(string url, AssetBase asset)
  93. {
  94. if (asset != null)
  95. {
  96. // See long comment in AssetCache.AddAsset
  97. if (!asset.Temporary || asset.Local)
  98. {
  99. // We need to copy the asset into a new asset, because
  100. // we need to set its ID to be URL+UUID, so that the
  101. // HGAssetService dispatches it to the remote grid.
  102. // It's not pretty, but the best that can be done while
  103. // not having a global naming infrastructure
  104. AssetBase asset1 = new AssetBase(asset.FullID, asset.Name, asset.Type);
  105. Copy(asset, asset1);
  106. try
  107. {
  108. asset1.ID = url + "/" + asset.ID;
  109. }
  110. catch
  111. {
  112. m_log.Warn("[HGScene]: Oops.");
  113. }
  114. m_scene.AssetService.Store(asset1);
  115. m_log.DebugFormat("[HGScene]: Posted copy of asset {0} from local asset server to {1}", asset1.ID, url);
  116. }
  117. return true;
  118. }
  119. else
  120. m_log.Warn("[HGScene]: Tried to post asset to remote server, but asset not in local cache.");
  121. return false;
  122. }
  123. private void Copy(AssetBase from, AssetBase to)
  124. {
  125. to.Data = from.Data;
  126. to.Description = from.Description;
  127. to.FullID = from.FullID;
  128. to.ID = from.ID;
  129. to.Local = from.Local;
  130. to.Name = from.Name;
  131. to.Temporary = from.Temporary;
  132. to.Type = from.Type;
  133. }
  134. // TODO: unused
  135. // private void Dump(Dictionary<UUID, bool> lst)
  136. // {
  137. // m_log.Debug("XXX -------- UUID DUMP ------- XXX");
  138. // foreach (KeyValuePair<UUID, bool> kvp in lst)
  139. // m_log.Debug(" >> " + kvp.Key + " (texture? " + kvp.Value + ")");
  140. // m_log.Debug("XXX -------- UUID DUMP ------- XXX");
  141. // }
  142. #endregion
  143. #region Public interface
  144. public void Get(UUID assetID, UUID ownerID)
  145. {
  146. // Get the item from the remote asset server onto the local AssetCache
  147. // and place an entry in m_assetMap
  148. string userAssetURL = HyperlinkAssets.GetUserAssetServer(ownerID);
  149. if ((userAssetURL != string.Empty) && (userAssetURL != HyperlinkAssets.GetSimAssetServer()))
  150. {
  151. m_log.Debug("[HGScene]: Fetching object " + assetID + " from asset server " + userAssetURL);
  152. AssetBase asset = FetchAsset(userAssetURL, assetID);
  153. if (asset != null)
  154. {
  155. // OK, now fetch the inside.
  156. Dictionary<UUID, int> ids = new Dictionary<UUID, int>();
  157. HGUuidGatherer uuidGatherer = new HGUuidGatherer(this, m_scene.AssetService, userAssetURL);
  158. uuidGatherer.GatherAssetUuids(asset.FullID, (AssetType)asset.Type, ids);
  159. foreach (UUID uuid in ids.Keys)
  160. FetchAsset(userAssetURL, uuid);
  161. m_log.DebugFormat("[HGScene]: Successfully fetched asset {0} from asset server {1}", asset.ID, userAssetURL);
  162. }
  163. else
  164. m_log.Warn("[HGScene]: Could not fetch asset from remote asset server " + userAssetURL);
  165. }
  166. else
  167. m_log.Debug("[HGScene]: user's asset server is the local region's asset server");
  168. }
  169. //public InventoryItemBase Get(InventoryItemBase item, UUID rootFolder, CachedUserInfo userInfo)
  170. //{
  171. // InventoryClient invCli = null;
  172. // string inventoryURL = UserInventoryURL(item.Owner);
  173. // if (!m_inventoryServers.TryGetValue(inventoryURL, out invCli))
  174. // {
  175. // m_log.Debug("[HGScene]: Starting new InventorytClient for " + inventoryURL);
  176. // invCli = new InventoryClient(inventoryURL);
  177. // m_inventoryServers.Add(inventoryURL, invCli);
  178. // }
  179. // item = invCli.GetInventoryItem(item);
  180. // if (item != null)
  181. // {
  182. // // Change the folder, stick it in root folder, all items flattened out here in this region cache
  183. // item.Folder = rootFolder;
  184. // //userInfo.AddItem(item); don't use this, it calls back to the inventory server
  185. // lock (userInfo.RootFolder.Items)
  186. // {
  187. // userInfo.RootFolder.Items[item.ID] = item;
  188. // }
  189. // }
  190. // return item;
  191. //}
  192. public void Post(UUID assetID, UUID ownerID)
  193. {
  194. // Post the item from the local AssetCache onto the remote asset server
  195. // and place an entry in m_assetMap
  196. string userAssetURL = HyperlinkAssets.GetUserAssetServer(ownerID);
  197. if ((userAssetURL != string.Empty) && (userAssetURL != HyperlinkAssets.GetSimAssetServer()))
  198. {
  199. m_log.Debug("[HGScene]: Posting object " + assetID + " to asset server " + userAssetURL);
  200. AssetBase asset = m_scene.AssetService.Get(assetID.ToString());
  201. if (asset != null)
  202. {
  203. Dictionary<UUID, int> ids = new Dictionary<UUID, int>();
  204. HGUuidGatherer uuidGatherer = new HGUuidGatherer(this, m_scene.AssetService, string.Empty);
  205. uuidGatherer.GatherAssetUuids(asset.FullID, (AssetType)asset.Type, ids);
  206. foreach (UUID uuid in ids.Keys)
  207. {
  208. asset = m_scene.AssetService.Get(uuid.ToString());
  209. if (asset == null)
  210. m_log.DebugFormat("[HGScene]: Could not find asset {0}", uuid);
  211. else
  212. PostAsset(userAssetURL, asset);
  213. }
  214. // maybe all pieces got there...
  215. m_log.DebugFormat("[HGScene]: Successfully posted item {0} to asset server {1}", assetID, userAssetURL);
  216. }
  217. else
  218. m_log.DebugFormat("[HGScene]: Something wrong with asset {0}, it could not be found", assetID);
  219. }
  220. else
  221. m_log.Debug("[HGScene]: user's asset server is local region's asset server");
  222. }
  223. #endregion
  224. }
  225. }