AssetTransactionModule.cs 11 KB

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