AgentAssetsTransactions.cs 10 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.Collections.Generic;
  28. using System.Reflection;
  29. using log4net;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Framework.Scenes;
  33. using OpenSim.Services.Interfaces;
  34. namespace OpenSim.Region.CoreModules.Agent.AssetTransaction
  35. {
  36. /// <summary>
  37. /// Manage asset transactions for a single agent.
  38. /// </summary>
  39. public class AgentAssetTransactions
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. // Fields
  43. private bool m_dumpAssetsToFile;
  44. private Scene m_Scene;
  45. private Dictionary<UUID, AssetXferUploader> XferUploaders = new Dictionary<UUID, AssetXferUploader>();
  46. // Methods
  47. public AgentAssetTransactions(UUID agentID, Scene scene,
  48. bool dumpAssetsToFile)
  49. {
  50. m_Scene = scene;
  51. m_dumpAssetsToFile = dumpAssetsToFile;
  52. }
  53. /// <summary>
  54. /// Return a xfer uploader if one does not already exist.
  55. /// </summary>
  56. /// <param name="transactionID"></param>
  57. /// <param name="assetID">
  58. /// We must transfer the new asset ID into the uploader on creation, otherwise
  59. /// we can see race conditions with other threads which can retrieve an item before it is updated with the new
  60. /// asset id.
  61. /// </param>
  62. /// <returns>
  63. /// The xfer uploader requested. Null if one is already in existence.
  64. /// FIXME: This is a bizarre thing to do, and is probably meant to signal an error condition if multiple
  65. /// transfers are made. Needs to be corrected.
  66. /// </returns>
  67. public AssetXferUploader RequestXferUploader(UUID transactionID, UUID assetID)
  68. {
  69. lock (XferUploaders)
  70. {
  71. if (!XferUploaders.ContainsKey(transactionID))
  72. {
  73. AssetXferUploader uploader = new AssetXferUploader(this, m_Scene, assetID, m_dumpAssetsToFile);
  74. // m_log.DebugFormat(
  75. // "[AGENT ASSETS TRANSACTIONS]: Adding asset xfer uploader {0} since it didn't previously exist", transactionID);
  76. XferUploaders.Add(transactionID, uploader);
  77. return uploader;
  78. }
  79. }
  80. m_log.WarnFormat("[AGENT ASSETS TRANSACTIONS]: Ignoring request for asset xfer uploader {0} since it already exists", transactionID);
  81. return null;
  82. }
  83. public void HandleXfer(ulong xferID, uint packetID, byte[] data)
  84. {
  85. AssetXferUploader foundUploader = null;
  86. lock (XferUploaders)
  87. {
  88. foreach (AssetXferUploader uploader in XferUploaders.Values)
  89. {
  90. // m_log.DebugFormat(
  91. // "[AGENT ASSETS TRANSACTIONS]: In HandleXfer, inspect xfer upload with xfer id {0}",
  92. // uploader.XferID);
  93. if (uploader.XferID == xferID)
  94. {
  95. foundUploader = uploader;
  96. break;
  97. }
  98. }
  99. }
  100. if (foundUploader != null)
  101. {
  102. // m_log.DebugFormat(
  103. // "[AGENT ASSETS TRANSACTIONS]: Found xfer uploader for xfer id {0}, packet id {1}, data length {2}",
  104. // xferID, packetID, data.Length);
  105. foundUploader.HandleXferPacket(xferID, packetID, data);
  106. }
  107. else
  108. {
  109. m_log.ErrorFormat(
  110. "[AGENT ASSET TRANSACTIONS]: Could not find uploader for xfer id {0}, packet id {1}, data length {2}",
  111. xferID, packetID, data.Length);
  112. }
  113. }
  114. public bool RemoveXferUploader(UUID transactionID)
  115. {
  116. lock (XferUploaders)
  117. {
  118. bool removed = XferUploaders.Remove(transactionID);
  119. if (!removed)
  120. m_log.WarnFormat(
  121. "[AGENT ASSET TRANSACTIONS]: Received request to remove xfer uploader with transaction ID {0} but none found",
  122. transactionID);
  123. // else
  124. // m_log.DebugFormat(
  125. // "[AGENT ASSET TRANSACTIONS]: Removed xfer uploader with transaction ID {0}", transactionID);
  126. return removed;
  127. }
  128. }
  129. public void RequestCreateInventoryItem(IClientAPI remoteClient,
  130. UUID transactionID, UUID folderID, uint callbackID,
  131. string description, string name, sbyte invType,
  132. sbyte type, byte wearableType, uint nextOwnerMask)
  133. {
  134. AssetXferUploader uploader = null;
  135. lock (XferUploaders)
  136. {
  137. if (XferUploaders.ContainsKey(transactionID))
  138. uploader = XferUploaders[transactionID];
  139. }
  140. if (uploader != null)
  141. uploader.RequestCreateInventoryItem(
  142. remoteClient, transactionID, folderID,
  143. callbackID, description, name, invType, type,
  144. wearableType, nextOwnerMask);
  145. else
  146. m_log.ErrorFormat(
  147. "[AGENT ASSET TRANSACTIONS]: Could not find uploader with transaction ID {0} when handling request to create inventory item {1} from {2}",
  148. transactionID, name, remoteClient.Name);
  149. }
  150. /// <summary>
  151. /// Get an uploaded asset. If the data is successfully retrieved,
  152. /// the transaction will be removed.
  153. /// </summary>
  154. /// <param name="transactionID"></param>
  155. /// <returns>The asset if the upload has completed, null if it has not.</returns>
  156. private AssetBase GetTransactionAsset(UUID transactionID)
  157. {
  158. lock (XferUploaders)
  159. {
  160. if (XferUploaders.ContainsKey(transactionID))
  161. {
  162. AssetXferUploader uploader = XferUploaders[transactionID];
  163. AssetBase asset = uploader.GetAssetData();
  164. RemoveXferUploader(transactionID);
  165. return asset;
  166. }
  167. }
  168. return null;
  169. }
  170. public void RequestUpdateTaskInventoryItem(IClientAPI remoteClient,
  171. SceneObjectPart part, UUID transactionID,
  172. TaskInventoryItem item)
  173. {
  174. AssetXferUploader uploader = null;
  175. lock (XferUploaders)
  176. {
  177. if (XferUploaders.ContainsKey(transactionID))
  178. uploader = XferUploaders[transactionID];
  179. }
  180. if (uploader != null)
  181. {
  182. AssetBase asset = GetTransactionAsset(transactionID);
  183. // Only legacy viewers use this, and they prefer CAPS, which
  184. // we have, so this really never runs.
  185. // Allow it, but only for "safe" types.
  186. if ((InventoryType)item.InvType != InventoryType.Notecard &&
  187. (InventoryType)item.InvType != InventoryType.LSL)
  188. return;
  189. if (asset != null)
  190. {
  191. // m_log.DebugFormat(
  192. // "[AGENT ASSETS TRANSACTIONS]: Updating item {0} in {1} for transaction {2}",
  193. // item.Name, part.Name, transactionID);
  194. asset.FullID = UUID.Random();
  195. asset.Name = item.Name;
  196. asset.Description = item.Description;
  197. asset.Type = (sbyte)item.Type;
  198. item.AssetID = asset.FullID;
  199. m_Scene.AssetService.Store(asset);
  200. }
  201. }
  202. else
  203. {
  204. m_log.ErrorFormat(
  205. "[AGENT ASSET TRANSACTIONS]: Could not find uploader with transaction ID {0} when handling request to update task inventory item {1} in {2}",
  206. transactionID, item.Name, part.Name);
  207. }
  208. }
  209. public void RequestUpdateInventoryItem(IClientAPI remoteClient,
  210. UUID transactionID, InventoryItemBase item)
  211. {
  212. AssetXferUploader uploader = null;
  213. lock (XferUploaders)
  214. {
  215. if (XferUploaders.ContainsKey(transactionID))
  216. uploader = XferUploaders[transactionID];
  217. }
  218. if (uploader != null)
  219. {
  220. uploader.RequestUpdateInventoryItem(remoteClient, transactionID, item);
  221. }
  222. else
  223. {
  224. m_log.ErrorFormat(
  225. "[AGENT ASSET TRANSACTIONS]: Could not find uploader with transaction ID {0} when handling request to update inventory item {1} for {2}",
  226. transactionID, item.Name, remoteClient.Name);
  227. }
  228. }
  229. }
  230. }