AssetTransactionModule.cs 11 KB

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