HGAssetMapper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.IO;
  30. using System.Reflection;
  31. using System.Threading;
  32. using System.Xml;
  33. using log4net;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Serialization.External;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Region.Framework.Scenes.Serialization;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Services.Interfaces;
  41. //using HyperGrid.Framework;
  42. //using OpenSim.Region.Communications.Hypergrid;
  43. namespace OpenSim.Region.CoreModules.Framework.InventoryAccess
  44. {
  45. public class HGAssetMapper
  46. {
  47. #region Fields
  48. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  49. // This maps between inventory server urls and inventory server clients
  50. // private Dictionary<string, InventoryClient> m_inventoryServers = new Dictionary<string, InventoryClient>();
  51. private Scene m_scene;
  52. private string m_HomeURI;
  53. #endregion
  54. #region Constructor
  55. public HGAssetMapper(Scene scene, string homeURL)
  56. {
  57. m_scene = scene;
  58. m_HomeURI = homeURL;
  59. }
  60. #endregion
  61. #region Internal functions
  62. private AssetMetadata FetchMetadata(string url, UUID assetID)
  63. {
  64. if (string.IsNullOrEmpty(url))
  65. return null;
  66. if (!url.EndsWith("/") && !url.EndsWith("="))
  67. url = url + "/";
  68. AssetMetadata meta = m_scene.AssetService.GetMetadata(url + assetID.ToString());
  69. if (meta != null)
  70. m_log.DebugFormat("[HG ASSET MAPPER]: Fetched metadata for asset {0} of type {1} from {2} ", assetID, meta.Type, url);
  71. else
  72. m_log.DebugFormat("[HG ASSET MAPPER]: Unable to fetched metadata for asset {0} from {1} ", assetID, url);
  73. return meta;
  74. }
  75. private AssetBase FetchAsset(string url, UUID assetID)
  76. {
  77. // Test if it's already here
  78. AssetBase asset = m_scene.AssetService.Get(assetID.ToString());
  79. if (asset == null)
  80. {
  81. if (string.IsNullOrEmpty(url))
  82. return null;
  83. if (!url.EndsWith("/") && !url.EndsWith("="))
  84. url = url + "/";
  85. asset = m_scene.AssetService.Get(url + assetID.ToString());
  86. //if (asset != null)
  87. // m_log.DebugFormat("[HG ASSET MAPPER]: Fetched asset {0} of type {1} from {2} ", assetID, asset.Metadata.Type, url);
  88. //else
  89. // m_log.DebugFormat("[HG ASSET MAPPER]: Unable to fetch asset {0} from {1} ", assetID, url);
  90. }
  91. return asset;
  92. }
  93. public bool PostAsset(string url, AssetBase asset)
  94. {
  95. if (string.IsNullOrEmpty(url))
  96. return false;
  97. if (!url.EndsWith("/") && !url.EndsWith("="))
  98. url = url + "/";
  99. if (asset == null)
  100. {
  101. m_log.Warn("[HG ASSET MAPPER]: Tried to post asset to remote server, but asset not in local cache.");
  102. return false;
  103. }
  104. // See long comment in AssetCache.AddAsset
  105. if (asset.Temporary || asset.Local)
  106. return true;
  107. // We need to copy the asset into a new asset, because
  108. // we need to set its ID to be URL+UUID, so that the
  109. // HGAssetService dispatches it to the remote grid.
  110. // It's not pretty, but the best that can be done while
  111. // not having a global naming infrastructure
  112. AssetBase asset1 = new AssetBase(asset.FullID, asset.Name, asset.Type, asset.Metadata.CreatorID);
  113. Copy(asset, asset1);
  114. asset1.ID = url + asset.ID;
  115. AdjustIdentifiers(asset1.Metadata);
  116. if (asset1.Metadata.Type == (sbyte)AssetType.Object)
  117. asset1.Data = AdjustIdentifiers(asset.Data);
  118. else
  119. asset1.Data = asset.Data;
  120. string id = m_scene.AssetService.Store(asset1);
  121. if (String.IsNullOrEmpty(id))
  122. {
  123. m_log.DebugFormat("[HG ASSET MAPPER]: Asset server {0} did not accept {1}", url, asset.ID);
  124. return false;
  125. }
  126. else {
  127. m_log.DebugFormat("[HG ASSET MAPPER]: Posted copy of asset {0} from local asset server to {1}", asset1.ID, url);
  128. return true;
  129. }
  130. }
  131. private void Copy(AssetBase from, AssetBase to)
  132. {
  133. //to.Data = from.Data; // don't copy this, it's copied elsewhere
  134. to.Description = from.Description;
  135. to.FullID = from.FullID;
  136. to.ID = from.ID;
  137. to.Local = from.Local;
  138. to.Name = from.Name;
  139. to.Temporary = from.Temporary;
  140. to.Type = from.Type;
  141. }
  142. private void AdjustIdentifiers(AssetMetadata meta)
  143. {
  144. if (!string.IsNullOrEmpty(meta.CreatorID))
  145. {
  146. UUID uuid = UUID.Zero;
  147. UUID.TryParse(meta.CreatorID, out uuid);
  148. UserAccount creator = m_scene.UserAccountService.GetUserAccount(m_scene.RegionInfo.ScopeID, uuid);
  149. if (creator != null)
  150. meta.CreatorID = m_HomeURI + ";" + creator.FirstName + " " + creator.LastName;
  151. }
  152. }
  153. protected byte[] AdjustIdentifiers(byte[] data)
  154. {
  155. string xml = Utils.BytesToString(data);
  156. return Utils.StringToBytes(RewriteSOP(xml));
  157. }
  158. protected string RewriteSOP(string xmlData)
  159. {
  160. // Console.WriteLine("Input XML [{0}]", xmlData);
  161. return ExternalRepresentationUtils.RewriteSOP(xmlData, m_scene.Name, m_HomeURI, m_scene.UserAccountService, m_scene.RegionInfo.ScopeID);
  162. }
  163. // TODO: unused
  164. // private void Dump(Dictionary<UUID, bool> lst)
  165. // {
  166. // m_log.Debug("XXX -------- UUID DUMP ------- XXX");
  167. // foreach (KeyValuePair<UUID, bool> kvp in lst)
  168. // m_log.Debug(" >> " + kvp.Key + " (texture? " + kvp.Value + ")");
  169. // m_log.Debug("XXX -------- UUID DUMP ------- XXX");
  170. // }
  171. #endregion
  172. #region Public interface
  173. public void Get(UUID assetID, UUID ownerID, string userAssetURL)
  174. {
  175. // Get the item from the remote asset server onto the local AssetService
  176. AssetMetadata meta = FetchMetadata(userAssetURL, assetID);
  177. if (meta == null)
  178. return;
  179. // The act of gathering UUIDs downloads some assets from the remote server
  180. // but not all...
  181. HGUuidGatherer uuidGatherer = new HGUuidGatherer(m_scene.AssetService, userAssetURL);
  182. uuidGatherer.AddForInspection(assetID);
  183. uuidGatherer.GatherAll();
  184. m_log.DebugFormat("[HG ASSET MAPPER]: Preparing to get {0} assets", uuidGatherer.GatheredUuids.Count);
  185. bool success = true;
  186. foreach (UUID uuid in uuidGatherer.GatheredUuids.Keys)
  187. if (FetchAsset(userAssetURL, uuid) == null)
  188. success = false;
  189. // maybe all pieces got here...
  190. if (!success)
  191. m_log.DebugFormat("[HG ASSET MAPPER]: Problems getting item {0} from asset server {1}", assetID, userAssetURL);
  192. else
  193. m_log.DebugFormat("[HG ASSET MAPPER]: Successfully got item {0} from asset server {1}", assetID, userAssetURL);
  194. }
  195. public void Post(UUID assetID, UUID ownerID, string userAssetURL)
  196. {
  197. m_log.DebugFormat("[HG ASSET MAPPER]: Starting to send asset {0} with children to asset server {1}", assetID, userAssetURL);
  198. // Find all the embedded assets
  199. AssetBase asset = m_scene.AssetService.Get(assetID.ToString());
  200. if (asset == null)
  201. {
  202. m_log.DebugFormat("[HG ASSET MAPPER]: Something wrong with asset {0}, it could not be found", assetID);
  203. return;
  204. }
  205. HGUuidGatherer uuidGatherer = new HGUuidGatherer(m_scene.AssetService, string.Empty);
  206. uuidGatherer.AddForInspection(asset.FullID);
  207. uuidGatherer.GatherAll();
  208. // Check which assets already exist in the destination server
  209. string url = userAssetURL;
  210. if (!url.EndsWith("/") && !url.EndsWith("="))
  211. url = url + "/";
  212. string[] remoteAssetIDs = new string[uuidGatherer.GatheredUuids.Count];
  213. int i = 0;
  214. foreach (UUID id in uuidGatherer.GatheredUuids.Keys)
  215. remoteAssetIDs[i++] = url + id.ToString();
  216. bool[] exist = m_scene.AssetService.AssetsExist(remoteAssetIDs);
  217. var existSet = new HashSet<string>();
  218. i = 0;
  219. foreach (UUID id in uuidGatherer.GatheredUuids.Keys)
  220. {
  221. if (exist[i])
  222. existSet.Add(id.ToString());
  223. ++i;
  224. }
  225. // Send only those assets which don't already exist in the destination server
  226. bool success = true;
  227. foreach (UUID uuid in uuidGatherer.GatheredUuids.Keys)
  228. {
  229. if (!existSet.Contains(uuid.ToString()))
  230. {
  231. asset = m_scene.AssetService.Get(uuid.ToString());
  232. if (asset == null)
  233. {
  234. m_log.DebugFormat("[HG ASSET MAPPER]: Could not find asset {0}", uuid);
  235. }
  236. else
  237. {
  238. try
  239. {
  240. success &= PostAsset(userAssetURL, asset);
  241. }
  242. catch (Exception e)
  243. {
  244. m_log.Error(
  245. string.Format(
  246. "[HG ASSET MAPPER]: Failed to post asset {0} (type {1}, length {2}) referenced from {3} to {4} with exception ",
  247. asset.ID, asset.Type, asset.Data.Length, assetID, userAssetURL),
  248. e);
  249. // For debugging purposes for now we will continue to throw the exception up the stack as was already happening. However, after
  250. // debugging we may want to simply report the failure if we can tell this is due to a failure
  251. // with a particular asset and not a destination network failure where all asset posts will fail (and
  252. // generate large amounts of log spam).
  253. throw e;
  254. }
  255. }
  256. }
  257. else
  258. {
  259. m_log.DebugFormat(
  260. "[HG ASSET MAPPER]: Didn't post asset {0} referenced from {1} because it already exists in asset server {2}",
  261. uuid, assetID, userAssetURL);
  262. }
  263. }
  264. if (!success)
  265. m_log.DebugFormat("[HG ASSET MAPPER]: Problems sending asset {0} with children to asset server {1}", assetID, userAssetURL);
  266. else
  267. m_log.DebugFormat("[HG ASSET MAPPER]: Successfully sent asset {0} with children to asset server {1}", assetID, userAssetURL);
  268. }
  269. #endregion
  270. }
  271. }