HGAssetMapper.cs 11 KB

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