AgentAssetTransactionModule.cs 11 KB

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