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