AssetTransactionModule.cs 12 KB

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