AgentAssetTransactionModule.cs 11 KB

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