AssetTransactionModule.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. ///
  153. /// This is called when clothing or body parts are updated (for
  154. /// instance, with new textures or colours). It may also be called in
  155. /// other situations.
  156. /// </summary>
  157. /// <param name="remoteClient"></param>
  158. /// <param name="transactionID"></param>
  159. /// <param name="item"></param>
  160. public void HandleItemUpdateFromTransaction(IClientAPI remoteClient,
  161. UUID transactionID, InventoryItemBase item)
  162. {
  163. // m_log.DebugFormat(
  164. // "[TRANSACTIONS MANAGER] Called HandleItemUpdateFromTransaction with item {0}",
  165. // item.Name);
  166. AgentAssetTransactions transactions =
  167. GetUserTransactions(remoteClient.AgentId);
  168. transactions.RequestUpdateInventoryItem(remoteClient,
  169. transactionID, item);
  170. }
  171. /// <summary>
  172. /// Update a task inventory item with data that has been received
  173. /// through a transaction.
  174. ///
  175. /// This is currently called when, for instance, a notecard in a prim
  176. /// is saved. The data is sent up through a single AssetUploadRequest.
  177. /// A subsequent UpdateTaskInventory then references the transaction
  178. /// and comes through this method.
  179. /// </summary>
  180. /// <param name="remoteClient"></param>
  181. /// <param name="transactionID"></param>
  182. /// <param name="item"></param>
  183. public void HandleTaskItemUpdateFromTransaction(IClientAPI remoteClient,
  184. SceneObjectPart part, UUID transactionID,
  185. TaskInventoryItem item)
  186. {
  187. // m_log.DebugFormat(
  188. // "[TRANSACTIONS MANAGER] Called HandleTaskItemUpdateFromTransaction with item {0}",
  189. // item.Name);
  190. AgentAssetTransactions transactions =
  191. GetUserTransactions(remoteClient.AgentId);
  192. transactions.RequestUpdateTaskInventoryItem(remoteClient, part,
  193. transactionID, item);
  194. }
  195. /// <summary>
  196. /// Request that a client (agent) begin an asset transfer.
  197. /// </summary>
  198. /// <param name="remoteClient"></param>
  199. /// <param name="assetID"></param>
  200. /// <param name="transaction"></param>
  201. /// <param name="type"></param>
  202. /// <param name="data"></param></param>
  203. /// <param name="tempFile"></param>
  204. public void HandleUDPUploadRequest(IClientAPI remoteClient,
  205. UUID assetID, UUID transaction, sbyte type, byte[] data,
  206. bool storeLocal, bool tempFile)
  207. {
  208. // m_log.Debug("HandleUDPUploadRequest - assetID: " + assetID.ToString() + " transaction: " + transaction.ToString() + " type: " + type.ToString() + " storelocal: " + storeLocal + " tempFile: " + tempFile);
  209. if (((AssetType)type == AssetType.Texture ||
  210. (AssetType)type == AssetType.Sound ||
  211. (AssetType)type == AssetType.TextureTGA ||
  212. (AssetType)type == AssetType.Animation) &&
  213. tempFile == false)
  214. {
  215. Scene scene = (Scene)remoteClient.Scene;
  216. IMoneyModule mm = scene.RequestModuleInterface<IMoneyModule>();
  217. if (mm != null)
  218. {
  219. if (!mm.UploadCovered(remoteClient, mm.UploadCharge))
  220. {
  221. remoteClient.SendAgentAlertMessage("Unable to upload asset. Insufficient funds.", false);
  222. return;
  223. }
  224. }
  225. }
  226. AgentAssetTransactions transactions =
  227. GetUserTransactions(remoteClient.AgentId);
  228. AssetXferUploader uploader =
  229. transactions.RequestXferUploader(transaction);
  230. if (uploader != null)
  231. {
  232. uploader.Initialise(remoteClient, assetID, transaction, type,
  233. data, storeLocal, tempFile);
  234. }
  235. }
  236. /// <summary>
  237. /// Handle asset transfer data packets received in response to the
  238. /// asset upload request in HandleUDPUploadRequest()
  239. /// </summary>
  240. /// <param name="remoteClient"></param>
  241. /// <param name="xferID"></param>
  242. /// <param name="packetID"></param>
  243. /// <param name="data"></param>
  244. public void HandleXfer(IClientAPI remoteClient, ulong xferID,
  245. uint packetID, byte[] data)
  246. {
  247. //m_log.Debug("xferID: " + xferID + " packetID: " + packetID + " data!");
  248. AgentAssetTransactions transactions =
  249. GetUserTransactions(remoteClient.AgentId);
  250. transactions.HandleXfer(xferID, packetID, data);
  251. }
  252. #endregion
  253. }
  254. }