AssetTransactionModule.cs 12 KB

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