AssetTransactionModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. namespace OpenSim.Region.CoreModules.Agent.AssetTransaction
  37. {
  38. public class AssetTransactionModule : IRegionModule, IAgentAssetTransactions
  39. {
  40. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. private readonly Dictionary<UUID, Scene> RegisteredScenes = new Dictionary<UUID, Scene>();
  42. private bool m_dumpAssetsToFile = false;
  43. private Scene m_scene = null;
  44. [Obsolete]
  45. public Scene MyScene
  46. {
  47. get{ return m_scene;}
  48. }
  49. /// <summary>
  50. /// Each agent has its own singleton collection of transactions
  51. /// </summary>
  52. private Dictionary<UUID, AgentAssetTransactions> AgentTransactions =
  53. new Dictionary<UUID, AgentAssetTransactions>();
  54. public AssetTransactionModule()
  55. {
  56. //m_log.Debug("creating AgentAssetTransactionModule");
  57. }
  58. #region IRegionModule Members
  59. public void Initialise(Scene scene, IConfigSource config)
  60. {
  61. if (!RegisteredScenes.ContainsKey(scene.RegionInfo.RegionID))
  62. {
  63. // m_log.Debug("initialising AgentAssetTransactionModule");
  64. RegisteredScenes.Add(scene.RegionInfo.RegionID, scene);
  65. scene.RegisterModuleInterface<IAgentAssetTransactions>(this);
  66. scene.EventManager.OnNewClient += NewClient;
  67. }
  68. // EVIL HACK!
  69. // This needs killing!
  70. //
  71. if (m_scene == null)
  72. m_scene = scene;
  73. }
  74. public void PostInitialise()
  75. {
  76. }
  77. public void Close()
  78. {
  79. }
  80. public string Name
  81. {
  82. get { return "AgentTransactionModule"; }
  83. }
  84. public bool IsSharedModule
  85. {
  86. get { return true; }
  87. }
  88. #endregion
  89. public void NewClient(IClientAPI client)
  90. {
  91. client.OnAssetUploadRequest += HandleUDPUploadRequest;
  92. client.OnXferReceive += HandleXfer;
  93. }
  94. #region AgentAssetTransactions
  95. /// <summary>
  96. /// Get the collection of asset transactions for the given user. If one does not already exist, it
  97. /// is created.
  98. /// </summary>
  99. /// <param name="userID"></param>
  100. /// <returns></returns>
  101. private AgentAssetTransactions GetUserTransactions(UUID userID)
  102. {
  103. lock (AgentTransactions)
  104. {
  105. if (!AgentTransactions.ContainsKey(userID))
  106. {
  107. AgentAssetTransactions transactions = new AgentAssetTransactions(userID, this, m_dumpAssetsToFile);
  108. AgentTransactions.Add(userID, transactions);
  109. }
  110. return AgentTransactions[userID];
  111. }
  112. }
  113. /// <summary>
  114. /// Remove the given agent asset transactions. This should be called when a client is departing
  115. /// from a scene (and hence won't be making any more transactions here).
  116. /// </summary>
  117. /// <param name="userID"></param>
  118. public void RemoveAgentAssetTransactions(UUID userID)
  119. {
  120. // m_log.DebugFormat("Removing agent asset transactions structure for agent {0}", userID);
  121. lock (AgentTransactions)
  122. {
  123. AgentTransactions.Remove(userID);
  124. }
  125. }
  126. /// <summary>
  127. /// Create an inventory item from data that has been received through a transaction.
  128. ///
  129. /// This is called when new clothing or body parts are created. It may also be called in other
  130. /// situations.
  131. /// </summary>
  132. /// <param name="remoteClient"></param>
  133. /// <param name="transactionID"></param>
  134. /// <param name="folderID"></param>
  135. /// <param name="callbackID"></param>
  136. /// <param name="description"></param>
  137. /// <param name="name"></param>
  138. /// <param name="invType"></param>
  139. /// <param name="type"></param>
  140. /// <param name="wearableType"></param>
  141. /// <param name="nextOwnerMask"></param>
  142. public void HandleItemCreationFromTransaction(IClientAPI remoteClient, UUID transactionID, UUID folderID,
  143. uint callbackID, string description, string name, sbyte invType,
  144. sbyte type, byte wearableType, uint nextOwnerMask)
  145. {
  146. // m_log.DebugFormat(
  147. // "[TRANSACTIONS MANAGER] Called HandleItemCreationFromTransaction with item {0}", name);
  148. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  149. transactions.RequestCreateInventoryItem(
  150. remoteClient, transactionID, folderID, callbackID, description,
  151. name, invType, type, wearableType, nextOwnerMask);
  152. }
  153. /// <summary>
  154. /// Update an inventory item with data that has been received through a transaction.
  155. ///
  156. /// This is called when clothing or body parts are updated (for instance, with new textures or
  157. /// colours). It may also be called in other situations.
  158. /// </summary>
  159. /// <param name="remoteClient"></param>
  160. /// <param name="transactionID"></param>
  161. /// <param name="item"></param>
  162. public void HandleItemUpdateFromTransaction(IClientAPI remoteClient, UUID transactionID,
  163. InventoryItemBase item)
  164. {
  165. // m_log.DebugFormat(
  166. // "[TRANSACTIONS MANAGER] Called HandleItemUpdateFromTransaction with item {0}",
  167. // item.Name);
  168. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  169. transactions.RequestUpdateInventoryItem(remoteClient, transactionID, item);
  170. }
  171. /// <summary>
  172. /// Update a task inventory item with data that has been received through a transaction.
  173. ///
  174. /// This is currently called when, for instance, a notecard in a prim is saved. The data is sent
  175. /// up through a single AssetUploadRequest. A subsequent UpdateTaskInventory then references the transaction
  176. /// and comes through this method.
  177. /// </summary>
  178. /// <param name="remoteClient"></param>
  179. /// <param name="transactionID"></param>
  180. /// <param name="item"></param>
  181. public void HandleTaskItemUpdateFromTransaction(
  182. IClientAPI remoteClient, SceneObjectPart part, UUID transactionID, TaskInventoryItem item)
  183. {
  184. // m_log.DebugFormat(
  185. // "[TRANSACTIONS MANAGER] Called HandleTaskItemUpdateFromTransaction with item {0}",
  186. // item.Name);
  187. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  188. transactions.RequestUpdateTaskInventoryItem(remoteClient, part, transactionID, item);
  189. }
  190. /// <summary>
  191. /// Request that a client (agent) begin an asset transfer.
  192. /// </summary>
  193. /// <param name="remoteClient"></param>
  194. /// <param name="assetID"></param>
  195. /// <param name="transaction"></param>
  196. /// <param name="type"></param>
  197. /// <param name="data"></param></param>
  198. /// <param name="tempFile"></param>
  199. public void HandleUDPUploadRequest(IClientAPI remoteClient, UUID assetID, UUID transaction, sbyte type,
  200. byte[] data, bool storeLocal, bool tempFile)
  201. {
  202. // m_log.Debug("HandleUDPUploadRequest - assetID: " + assetID.ToString() + " transaction: " + transaction.ToString() + " type: " + type.ToString() + " storelocal: " + storeLocal + " tempFile: " + tempFile);
  203. if (((AssetType)type == AssetType.Texture ||
  204. (AssetType)type == AssetType.Sound ||
  205. (AssetType)type == AssetType.TextureTGA ||
  206. (AssetType)type == AssetType.Animation) &&
  207. tempFile == false)
  208. {
  209. Scene scene = (Scene)remoteClient.Scene;
  210. IMoneyModule mm = scene.RequestModuleInterface<IMoneyModule>();
  211. if (mm != null)
  212. {
  213. if (!mm.UploadCovered(remoteClient))
  214. {
  215. remoteClient.SendAgentAlertMessage("Unable to upload asset. Insufficient funds.", false);
  216. return;
  217. }
  218. }
  219. }
  220. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  221. AssetXferUploader uploader = transactions.RequestXferUploader(transaction);
  222. if (uploader != null)
  223. {
  224. uploader.Initialise(remoteClient, assetID, transaction, type, data, storeLocal, tempFile);
  225. }
  226. }
  227. /// <summary>
  228. /// Handle asset transfer data packets received in response to the asset upload request in
  229. /// HandleUDPUploadRequest()
  230. /// </summary>
  231. /// <param name="remoteClient"></param>
  232. /// <param name="xferID"></param>
  233. /// <param name="packetID"></param>
  234. /// <param name="data"></param>
  235. public void HandleXfer(IClientAPI remoteClient, ulong xferID, uint packetID, byte[] data)
  236. {
  237. //m_log.Debug("xferID: " + xferID + " packetID: " + packetID + " data!");
  238. AgentAssetTransactions transactions = GetUserTransactions(remoteClient.AgentId);
  239. transactions.HandleXfer(xferID, packetID, data);
  240. }
  241. #endregion
  242. }
  243. }