CachedUserInfo.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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 OpenSimulator 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 log4net;
  31. using OpenMetaverse;
  32. using OpenSim.Services.Interfaces;
  33. namespace OpenSim.Framework.Communications.Cache
  34. {
  35. internal delegate void AddItemDelegate(InventoryItemBase itemInfo);
  36. internal delegate void UpdateItemDelegate(InventoryItemBase itemInfo);
  37. internal delegate void DeleteItemDelegate(UUID itemID);
  38. internal delegate void QueryItemDelegate(UUID itemID);
  39. internal delegate void QueryFolderDelegate(UUID folderID);
  40. internal delegate void CreateFolderDelegate(string folderName, UUID folderID, ushort folderType, UUID parentID);
  41. internal delegate void MoveFolderDelegate(UUID folderID, UUID parentID);
  42. internal delegate void PurgeFolderDelegate(UUID folderID);
  43. internal delegate void UpdateFolderDelegate(string name, UUID folderID, ushort type, UUID parentID);
  44. internal delegate void SendInventoryDescendentsDelegate(
  45. IClientAPI client, UUID folderID, bool fetchFolders, bool fetchItems);
  46. public delegate void OnItemReceivedDelegate(UUID itemID);
  47. public delegate void OnInventoryReceivedDelegate(UUID userID);
  48. /// <summary>
  49. /// Stores user profile and inventory data received from backend services for a particular user.
  50. /// </summary>
  51. public class CachedUserInfo
  52. {
  53. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  54. //// <value>
  55. /// Fired when a particular item has been received from the inventory service
  56. /// </value>
  57. public event OnItemReceivedDelegate OnItemReceived;
  58. /// <value>
  59. /// Fired once the entire inventory has been received for the user
  60. /// </value>
  61. public event OnInventoryReceivedDelegate OnInventoryReceived;
  62. /// <summary>
  63. /// The comms manager holds references to services (user, grid, inventory, etc.)
  64. /// </summary>
  65. private readonly IInventoryService m_InventoryService;
  66. public UserProfileData UserProfile { get { return m_userProfile; } }
  67. private UserProfileData m_userProfile;
  68. /// <summary>
  69. /// Have we received the user's inventory from the inventory service?
  70. /// </summary>
  71. public bool HasReceivedInventory { get { return m_hasReceivedInventory; } }
  72. private bool m_hasReceivedInventory;
  73. /// <summary>
  74. /// Inventory requests waiting for receipt of this user's inventory from the inventory service.
  75. /// </summary>
  76. private readonly IList<IInventoryRequest> m_pendingRequests = new List<IInventoryRequest>();
  77. /// <summary>
  78. /// The root folder of this user's inventory. Returns null if the root folder has not yet been received.
  79. /// </summary>
  80. public InventoryFolderImpl RootFolder { get { return m_rootFolder; } }
  81. private InventoryFolderImpl m_rootFolder;
  82. public UUID SessionID
  83. {
  84. get { return m_session_id; }
  85. set { m_session_id = value; }
  86. }
  87. private UUID m_session_id = UUID.Zero;
  88. /// <summary>
  89. /// Constructor
  90. /// </summary>
  91. /// <param name="commsManager"></param>
  92. /// <param name="userProfile"></param>
  93. public CachedUserInfo(IInventoryService invService, UserProfileData userProfile)
  94. {
  95. m_userProfile = userProfile;
  96. m_InventoryService = invService;
  97. }
  98. /// <summary>
  99. /// This allows a request to be added to be processed once we receive a user's inventory
  100. /// from the inventory service. If we already have the inventory, the request
  101. /// is executed immediately instead.
  102. /// </summary>
  103. /// <param name="parent"></param>
  104. protected void AddRequest(IInventoryRequest request)
  105. {
  106. lock (m_pendingRequests)
  107. {
  108. if (HasReceivedInventory)
  109. {
  110. request.Execute();
  111. }
  112. else
  113. {
  114. m_pendingRequests.Add(request);
  115. }
  116. }
  117. }
  118. /// <summary>
  119. /// Helper function for InventoryReceive() - Store a folder temporarily until we've received entire folder list
  120. /// </summary>
  121. /// <param name="folder"></param>
  122. private void AddFolderToDictionary(InventoryFolderImpl folder, IDictionary<UUID, IList<InventoryFolderImpl>> dictionary)
  123. {
  124. UUID parentFolderId = folder.ParentID;
  125. if (dictionary.ContainsKey(parentFolderId))
  126. {
  127. dictionary[parentFolderId].Add(folder);
  128. }
  129. else
  130. {
  131. IList<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  132. folders.Add(folder);
  133. dictionary[parentFolderId] = folders;
  134. }
  135. }
  136. /// <summary>
  137. /// Recursively, in depth-first order, add all the folders we've received (stored
  138. /// in a dictionary indexed by parent ID) into the tree that describes user folder
  139. /// heirarchy
  140. /// Any folder that is resolved into the tree is also added to resolvedFolderDictionary,
  141. /// indexed by folder ID.
  142. /// </summary>
  143. /// <param name="parentId">
  144. /// A <see cref="UUID"/>
  145. /// </param>
  146. private void ResolveReceivedFolders(InventoryFolderImpl parentFolder,
  147. IDictionary<UUID, IList<InventoryFolderImpl>> receivedFolderDictionary,
  148. IDictionary<UUID, InventoryFolderImpl> resolvedFolderDictionary)
  149. {
  150. if (receivedFolderDictionary.ContainsKey(parentFolder.ID))
  151. {
  152. List<InventoryFolderImpl> resolvedFolders = new List<InventoryFolderImpl>(); // Folders we've resolved with this invocation
  153. foreach (InventoryFolderImpl folder in receivedFolderDictionary[parentFolder.ID])
  154. {
  155. if (parentFolder.ContainsChildFolder(folder.ID))
  156. {
  157. m_log.WarnFormat(
  158. "[INVENTORY CACHE]: Received folder {0} {1} from inventory service which has already been received",
  159. folder.Name, folder.ID);
  160. }
  161. else
  162. {
  163. if (resolvedFolderDictionary.ContainsKey(folder.ID))
  164. {
  165. m_log.WarnFormat(
  166. "[INVENTORY CACHE]: Received folder {0} {1} from inventory service has already been received but with different parent",
  167. folder.Name, folder.ID);
  168. }
  169. else
  170. {
  171. resolvedFolders.Add(folder);
  172. resolvedFolderDictionary[folder.ID] = folder;
  173. parentFolder.AddChildFolder(folder);
  174. }
  175. }
  176. } // foreach (folder in pendingCategorizationFolders[parentFolder.ID])
  177. receivedFolderDictionary.Remove(parentFolder.ID);
  178. foreach (InventoryFolderImpl folder in resolvedFolders)
  179. ResolveReceivedFolders(folder, receivedFolderDictionary, resolvedFolderDictionary);
  180. } // if (receivedFolderDictionary.ContainsKey(parentFolder.ID))
  181. }
  182. /// <summary>
  183. /// Drop all cached inventory.
  184. /// </summary>
  185. public void DropInventory()
  186. {
  187. m_log.Debug("[INVENTORY CACHE]: DropInventory called");
  188. // Make sure there aren't pending requests around when we do this
  189. // FIXME: There is still a race condition where an inventory operation can be requested (since these aren't being locked).
  190. // Will have to extend locking to exclude this very soon.
  191. lock (m_pendingRequests)
  192. {
  193. m_hasReceivedInventory = false;
  194. m_rootFolder = null;
  195. }
  196. }
  197. /// <summary>
  198. /// Fetch inventory for this user.
  199. /// </summary>
  200. /// This has to be executed as a separate step once user information is retreived.
  201. /// This will occur synchronously if the inventory service is in the same process as this class, and
  202. /// asynchronously otherwise.
  203. public void FetchInventory()
  204. {
  205. m_InventoryService.GetUserInventory(UserProfile.ID, InventoryReceive);
  206. }
  207. /// <summary>
  208. /// Callback invoked when the inventory is received from an async request to the inventory service
  209. /// </summary>
  210. /// <param name="userID"></param>
  211. /// <param name="inventoryCollection"></param>
  212. public void InventoryReceive(ICollection<InventoryFolderImpl> folders, ICollection<InventoryItemBase> items)
  213. {
  214. // FIXME: Exceptions thrown upwards never appear on the console. Could fix further up if these
  215. // are simply being swallowed
  216. try
  217. {
  218. // collection of all received folders, indexed by their parent ID
  219. IDictionary<UUID, IList<InventoryFolderImpl>> receivedFolders =
  220. new Dictionary<UUID, IList<InventoryFolderImpl>>();
  221. // collection of all folders that have been placed into the folder heirarchy starting at m_rootFolder
  222. // This dictonary exists so we don't have to do an InventoryFolderImpl.FindFolder(), which is O(n) on the
  223. // number of folders in our inventory.
  224. // Maybe we should make this structure a member so we can skip InventoryFolderImpl.FindFolder() calls later too?
  225. IDictionary<UUID, InventoryFolderImpl> resolvedFolders =
  226. new Dictionary<UUID, InventoryFolderImpl>();
  227. // Take all received folders, find the root folder, and put ther rest into
  228. // the pendingCategorizationFolders collection
  229. foreach (InventoryFolderImpl folder in folders)
  230. AddFolderToDictionary(folder, receivedFolders);
  231. if (!receivedFolders.ContainsKey(UUID.Zero))
  232. throw new Exception("Database did not return a root inventory folder");
  233. else
  234. {
  235. IList<InventoryFolderImpl> rootFolderList = receivedFolders[UUID.Zero];
  236. m_rootFolder = rootFolderList[0];
  237. resolvedFolders[m_rootFolder.ID] = m_rootFolder;
  238. if (rootFolderList.Count > 1)
  239. {
  240. for (int i = 1; i < rootFolderList.Count; i++)
  241. {
  242. m_log.WarnFormat(
  243. "[INVENTORY CACHE]: Discarding extra root folder {0}. Using previously received root folder {1}",
  244. rootFolderList[i].ID, RootFolder.ID);
  245. }
  246. }
  247. receivedFolders.Remove(UUID.Zero);
  248. }
  249. // Now take the pendingCategorizationFolders collection, and turn that into a tree,
  250. // with the root being RootFolder
  251. if (RootFolder != null)
  252. ResolveReceivedFolders(RootFolder, receivedFolders, resolvedFolders);
  253. // Generate a warning for folders that are not part of the heirarchy
  254. foreach (KeyValuePair<UUID, IList<InventoryFolderImpl>> folderList in receivedFolders)
  255. {
  256. foreach (InventoryFolderImpl folder in folderList.Value)
  257. m_log.WarnFormat("[INVENTORY CACHE]: Malformed Database: Unresolved Pending Folder {0}", folder.Name);
  258. }
  259. // Take all ther received items and put them into the folder tree heirarchy
  260. foreach (InventoryItemBase item in items) {
  261. InventoryFolderImpl folder = resolvedFolders.ContainsKey(item.Folder) ? resolvedFolders[item.Folder] : null;
  262. ItemReceive(item, folder);
  263. }
  264. }
  265. catch (Exception e)
  266. {
  267. m_log.ErrorFormat("[INVENTORY CACHE]: Error processing inventory received from inventory service, {0}", e);
  268. }
  269. // Deal with pending requests
  270. lock (m_pendingRequests)
  271. {
  272. // We're going to change inventory status within the lock to avoid a race condition
  273. // where requests are processed after the AddRequest() method has been called.
  274. m_hasReceivedInventory = true;
  275. foreach (IInventoryRequest request in m_pendingRequests)
  276. {
  277. request.Execute();
  278. }
  279. }
  280. if (OnInventoryReceived != null)
  281. OnInventoryReceived(UserProfile.ID);
  282. }
  283. /// <summary>
  284. /// Callback invoked when an item is received from an async request to the inventory service.
  285. ///
  286. /// We're assuming here that items are always received after all the folders
  287. /// received.
  288. /// If folder is null, we will search for it starting from RootFolder (an O(n) operation),
  289. /// otherwise we'll just put it into folder
  290. /// </summary>
  291. /// <param name="folderInfo"></param>
  292. private void ItemReceive(InventoryItemBase itemInfo, InventoryFolderImpl folder)
  293. {
  294. // m_log.DebugFormat(
  295. // "[INVENTORY CACHE]: Received item {0} {1} for user {2}",
  296. // itemInfo.Name, itemInfo.ID, userID);
  297. if (folder == null && RootFolder != null)
  298. folder = RootFolder.FindFolder(itemInfo.Folder);
  299. if (null == folder)
  300. {
  301. m_log.WarnFormat(
  302. "Received item {0} {1} but its folder {2} does not exist",
  303. itemInfo.Name, itemInfo.ID, itemInfo.Folder);
  304. return;
  305. }
  306. lock (folder.Items)
  307. {
  308. folder.Items[itemInfo.ID] = itemInfo;
  309. }
  310. if (OnItemReceived != null)
  311. OnItemReceived(itemInfo.ID);
  312. }
  313. /// <summary>
  314. /// Create a folder in this agent's inventory.
  315. /// </summary>
  316. ///
  317. /// If the inventory service has not yet delievered the inventory
  318. /// for this user then the request will be queued.
  319. ///
  320. /// <param name="parentID"></param>
  321. /// <returns></returns>
  322. public bool CreateFolder(string folderName, UUID folderID, ushort folderType, UUID parentID)
  323. {
  324. // m_log.DebugFormat(
  325. // "[AGENT INVENTORY]: Creating inventory folder {0} {1} for {2} {3}", folderID, folderName, remoteClient.Name, remoteClient.AgentId);
  326. if (m_hasReceivedInventory)
  327. {
  328. InventoryFolderImpl parentFolder = RootFolder.FindFolder(parentID);
  329. if (null == parentFolder)
  330. {
  331. m_log.WarnFormat(
  332. "[AGENT INVENTORY]: Tried to create folder {0} {1} but the parent {2} does not exist",
  333. folderName, folderID, parentID);
  334. return false;
  335. }
  336. InventoryFolderImpl createdFolder = parentFolder.CreateChildFolder(folderID, folderName, folderType);
  337. if (createdFolder != null)
  338. {
  339. InventoryFolderBase createdBaseFolder = new InventoryFolderBase();
  340. createdBaseFolder.Owner = createdFolder.Owner;
  341. createdBaseFolder.ID = createdFolder.ID;
  342. createdBaseFolder.Name = createdFolder.Name;
  343. createdBaseFolder.ParentID = createdFolder.ParentID;
  344. createdBaseFolder.Type = createdFolder.Type;
  345. createdBaseFolder.Version = createdFolder.Version;
  346. m_InventoryService.AddFolder(createdBaseFolder);
  347. return true;
  348. }
  349. else
  350. {
  351. m_log.WarnFormat(
  352. "[AGENT INVENTORY]: Tried to create folder {0} {1} but the folder already exists",
  353. folderName, folderID);
  354. return false;
  355. }
  356. }
  357. else
  358. {
  359. AddRequest(
  360. new InventoryRequest(
  361. Delegate.CreateDelegate(typeof(CreateFolderDelegate), this, "CreateFolder"),
  362. new object[] { folderName, folderID, folderType, parentID }));
  363. return true;
  364. }
  365. }
  366. /// <summary>
  367. /// Handle a client request to update the inventory folder
  368. /// </summary>
  369. ///
  370. /// If the inventory service has not yet delievered the inventory
  371. /// for this user then the request will be queued.
  372. ///
  373. /// FIXME: We call add new inventory folder because in the data layer, we happen to use an SQL REPLACE
  374. /// so this will work to rename an existing folder. Needless to say, to rely on this is very confusing,
  375. /// and needs to be changed.
  376. ///
  377. /// <param name="folderID"></param>
  378. /// <param name="type"></param>
  379. /// <param name="name"></param>
  380. /// <param name="parentID"></param>
  381. public bool UpdateFolder(string name, UUID folderID, ushort type, UUID parentID)
  382. {
  383. // m_log.DebugFormat(
  384. // "[AGENT INVENTORY]: Updating inventory folder {0} {1} for {2} {3}", folderID, name, remoteClient.Name, remoteClient.AgentId);
  385. if (m_hasReceivedInventory)
  386. {
  387. InventoryFolderImpl folder = RootFolder.FindFolder(folderID);
  388. // Delegate movement if updated parent id isn't the same as the existing parentId
  389. if (folder.ParentID != parentID)
  390. MoveFolder(folderID, parentID);
  391. InventoryFolderBase baseFolder = new InventoryFolderBase();
  392. baseFolder.Owner = m_userProfile.ID;
  393. baseFolder.ID = folderID;
  394. baseFolder.Name = name;
  395. baseFolder.ParentID = parentID;
  396. baseFolder.Type = (short)type;
  397. baseFolder.Version = RootFolder.Version;
  398. m_InventoryService.UpdateFolder(baseFolder);
  399. folder.Name = name;
  400. folder.Type = (short)type;
  401. }
  402. else
  403. {
  404. AddRequest(
  405. new InventoryRequest(
  406. Delegate.CreateDelegate(typeof(UpdateFolderDelegate), this, "UpdateFolder"),
  407. new object[] { name, folderID, type, parentID }));
  408. }
  409. return true;
  410. }
  411. /// <summary>
  412. /// Handle an inventory folder move request from the client.
  413. ///
  414. /// If the inventory service has not yet delievered the inventory
  415. /// for this user then the request will be queued.
  416. /// </summary>
  417. ///
  418. /// <param name="folderID"></param>
  419. /// <param name="parentID"></param>
  420. /// <returns>
  421. /// true if the delete was successful, or if it was queued pending folder receipt
  422. /// false if the folder to be deleted did not exist.
  423. /// </returns>
  424. public bool MoveFolder(UUID folderID, UUID parentID)
  425. {
  426. // m_log.DebugFormat(
  427. // "[AGENT INVENTORY]: Moving inventory folder {0} into folder {1} for {2} {3}",
  428. // parentID, remoteClient.Name, remoteClient.Name, remoteClient.AgentId);
  429. if (m_hasReceivedInventory)
  430. {
  431. InventoryFolderBase baseFolder = new InventoryFolderBase();
  432. baseFolder.Owner = m_userProfile.ID;
  433. baseFolder.ID = folderID;
  434. baseFolder.ParentID = parentID;
  435. m_InventoryService.MoveFolder(baseFolder);
  436. InventoryFolderImpl folder = RootFolder.FindFolder(folderID);
  437. InventoryFolderImpl parentFolder = RootFolder.FindFolder(parentID);
  438. if (parentFolder != null && folder != null)
  439. {
  440. InventoryFolderImpl oldParentFolder = RootFolder.FindFolder(folder.ParentID);
  441. if (oldParentFolder != null)
  442. {
  443. oldParentFolder.RemoveChildFolder(folderID);
  444. parentFolder.AddChildFolder(folder);
  445. }
  446. else
  447. {
  448. return false;
  449. }
  450. }
  451. else
  452. {
  453. return false;
  454. }
  455. return true;
  456. }
  457. else
  458. {
  459. AddRequest(
  460. new InventoryRequest(
  461. Delegate.CreateDelegate(typeof(MoveFolderDelegate), this, "MoveFolder"),
  462. new object[] { folderID, parentID }));
  463. return true;
  464. }
  465. }
  466. /// <summary>
  467. /// This method will delete all the items and folders in the given folder.
  468. /// </summary>
  469. /// If the inventory service has not yet delievered the inventory
  470. /// for this user then the request will be queued.
  471. ///
  472. /// <param name="folderID"></param>
  473. public bool PurgeFolder(UUID folderID)
  474. {
  475. // m_log.InfoFormat("[AGENT INVENTORY]: Purging folder {0} for {1} uuid {2}",
  476. // folderID, remoteClient.Name, remoteClient.AgentId);
  477. if (m_hasReceivedInventory)
  478. {
  479. InventoryFolderImpl purgedFolder = RootFolder.FindFolder(folderID);
  480. if (purgedFolder != null)
  481. {
  482. // XXX Nasty - have to create a new object to hold details we already have
  483. InventoryFolderBase purgedBaseFolder = new InventoryFolderBase();
  484. purgedBaseFolder.Owner = purgedFolder.Owner;
  485. purgedBaseFolder.ID = purgedFolder.ID;
  486. purgedBaseFolder.Name = purgedFolder.Name;
  487. purgedBaseFolder.ParentID = purgedFolder.ParentID;
  488. purgedBaseFolder.Type = purgedFolder.Type;
  489. purgedBaseFolder.Version = purgedFolder.Version;
  490. m_InventoryService.PurgeFolder(purgedBaseFolder);
  491. purgedFolder.Purge();
  492. return true;
  493. }
  494. }
  495. else
  496. {
  497. AddRequest(
  498. new InventoryRequest(
  499. Delegate.CreateDelegate(typeof(PurgeFolderDelegate), this, "PurgeFolder"),
  500. new object[] { folderID }));
  501. return true;
  502. }
  503. return false;
  504. }
  505. /// <summary>
  506. /// Add an item to the user's inventory.
  507. /// </summary>
  508. /// If the item has no folder set (i.e. it is UUID.Zero), then it is placed in the most appropriate folder
  509. /// for that type.
  510. /// <param name="itemInfo"></param>
  511. public void AddItem(InventoryItemBase item)
  512. {
  513. if (m_hasReceivedInventory)
  514. {
  515. if (item.Folder == UUID.Zero)
  516. {
  517. InventoryFolderImpl f = FindFolderForType(item.AssetType);
  518. if (f != null)
  519. item.Folder = f.ID;
  520. else
  521. item.Folder = RootFolder.ID;
  522. }
  523. ItemReceive(item, null);
  524. m_InventoryService.AddItem(item);
  525. }
  526. else
  527. {
  528. AddRequest(
  529. new InventoryRequest(
  530. Delegate.CreateDelegate(typeof(AddItemDelegate), this, "AddItem"),
  531. new object[] { item }));
  532. }
  533. }
  534. /// <summary>
  535. /// Update an item in the user's inventory
  536. /// </summary>
  537. /// <param name="userID"></param>
  538. /// <param name="itemInfo"></param>
  539. public void UpdateItem(InventoryItemBase item)
  540. {
  541. if (m_hasReceivedInventory)
  542. {
  543. m_InventoryService.UpdateItem(item);
  544. }
  545. else
  546. {
  547. AddRequest(
  548. new InventoryRequest(
  549. Delegate.CreateDelegate(typeof(UpdateItemDelegate), this, "UpdateItem"),
  550. new object[] { item }));
  551. }
  552. }
  553. /// <summary>
  554. /// Delete an item from the user's inventory
  555. ///
  556. /// If the inventory service has not yet delievered the inventory
  557. /// for this user then the request will be queued.
  558. /// </summary>
  559. /// <param name="itemID"></param>
  560. /// <returns>
  561. /// true on a successful delete or a if the request is queued.
  562. /// Returns false on an immediate failure
  563. /// </returns>
  564. public bool DeleteItem(UUID itemID)
  565. {
  566. if (m_hasReceivedInventory)
  567. {
  568. // XXX For historical reasons (grid comms), we need to retrieve the whole item in order to delete, even though
  569. // really only the item id is required.
  570. InventoryItemBase item = RootFolder.FindItem(itemID);
  571. if (null == item)
  572. {
  573. m_log.WarnFormat("[AGENT INVENTORY]: Tried to delete item {0} which does not exist", itemID);
  574. return false;
  575. }
  576. if (RootFolder.DeleteItem(item.ID))
  577. {
  578. List<UUID> uuids = new List<UUID>();
  579. uuids.Add(itemID);
  580. return m_InventoryService.DeleteItems(this.UserProfile.ID, uuids);
  581. }
  582. }
  583. else
  584. {
  585. AddRequest(
  586. new InventoryRequest(
  587. Delegate.CreateDelegate(typeof(DeleteItemDelegate), this, "DeleteItem"),
  588. new object[] { itemID }));
  589. return true;
  590. }
  591. return false;
  592. }
  593. /// <summary>
  594. /// Send details of the inventory items and/or folders in a given folder to the client.
  595. /// </summary>
  596. /// <param name="client"></param>
  597. /// <param name="folderID"></param>
  598. /// <param name="fetchFolders"></param>
  599. /// <param name="fetchItems"></param>
  600. /// <returns>true if the request was queued or successfully processed, false otherwise</returns>
  601. public bool SendInventoryDecendents(IClientAPI client, UUID folderID, int version, bool fetchFolders, bool fetchItems)
  602. {
  603. if (m_hasReceivedInventory)
  604. {
  605. InventoryFolderImpl folder;
  606. if ((folder = RootFolder.FindFolder(folderID)) != null)
  607. {
  608. // m_log.DebugFormat(
  609. // "[AGENT INVENTORY]: Found folder {0} for client {1}",
  610. // folderID, remoteClient.AgentId);
  611. client.SendInventoryFolderDetails(
  612. client.AgentId, folderID, folder.RequestListOfItems(),
  613. folder.RequestListOfFolders(), version, fetchFolders, fetchItems);
  614. return true;
  615. }
  616. else
  617. {
  618. m_log.WarnFormat(
  619. "[AGENT INVENTORY]: Could not find folder {0} requested by user {1} {2}",
  620. folderID, client.Name, client.AgentId);
  621. return false;
  622. }
  623. }
  624. else
  625. {
  626. AddRequest(
  627. new InventoryRequest(
  628. Delegate.CreateDelegate(typeof(SendInventoryDescendentsDelegate), this, "SendInventoryDecendents", false, false),
  629. new object[] { client, folderID, fetchFolders, fetchItems }));
  630. return true;
  631. }
  632. }
  633. /// <summary>
  634. /// Find an appropriate folder for the given asset type
  635. /// </summary>
  636. /// <param name="type"></param>
  637. /// <returns>null if no appropriate folder exists</returns>
  638. public InventoryFolderImpl FindFolderForType(int type)
  639. {
  640. if (RootFolder == null)
  641. return null;
  642. return RootFolder.FindFolderForType(type);
  643. }
  644. // Load additional items that other regions have put into the database
  645. // The item will be added tot he local cache. Returns true if the item
  646. // was found and can be sent to the client
  647. //
  648. public bool QueryItem(InventoryItemBase item)
  649. {
  650. if (m_hasReceivedInventory)
  651. {
  652. InventoryItemBase invItem = RootFolder.FindItem(item.ID);
  653. if (invItem != null)
  654. {
  655. // Item is in local cache, just update client
  656. //
  657. return true;
  658. }
  659. InventoryItemBase itemInfo = null;
  660. itemInfo = m_InventoryService.GetItem(item);
  661. if (itemInfo != null)
  662. {
  663. InventoryFolderImpl folder = RootFolder.FindFolder(itemInfo.Folder);
  664. ItemReceive(itemInfo, folder);
  665. return true;
  666. }
  667. return false;
  668. }
  669. else
  670. {
  671. AddRequest(
  672. new InventoryRequest(
  673. Delegate.CreateDelegate(typeof(QueryItemDelegate), this, "QueryItem"),
  674. new object[] { item.ID }));
  675. return true;
  676. }
  677. }
  678. public bool QueryFolder(InventoryFolderBase folder)
  679. {
  680. if (m_hasReceivedInventory)
  681. {
  682. InventoryFolderBase invFolder = RootFolder.FindFolder(folder.ID);
  683. if (invFolder != null)
  684. {
  685. // Folder is in local cache, just update client
  686. //
  687. return true;
  688. }
  689. InventoryFolderBase folderInfo = null;
  690. folderInfo = m_InventoryService.GetFolder(folder);
  691. if (folderInfo != null)
  692. {
  693. InventoryFolderImpl createdFolder = RootFolder.CreateChildFolder(folderInfo.ID, folderInfo.Name, (ushort)folderInfo.Type);
  694. createdFolder.Version = folderInfo.Version;
  695. createdFolder.Owner = folderInfo.Owner;
  696. createdFolder.ParentID = folderInfo.ParentID;
  697. return true;
  698. }
  699. return false;
  700. }
  701. else
  702. {
  703. AddRequest(
  704. new InventoryRequest(
  705. Delegate.CreateDelegate(typeof(QueryFolderDelegate), this, "QueryFolder"),
  706. new object[] { folder.ID }));
  707. return true;
  708. }
  709. }
  710. }
  711. /// <summary>
  712. /// Should be implemented by callers which require a callback when the user's inventory is received
  713. /// </summary>
  714. public interface IInventoryRequest
  715. {
  716. /// <summary>
  717. /// This is the method executed once we have received the user's inventory by which the request can be fulfilled.
  718. /// </summary>
  719. void Execute();
  720. }
  721. /// <summary>
  722. /// Generic inventory request
  723. /// </summary>
  724. class InventoryRequest : IInventoryRequest
  725. {
  726. private Delegate m_delegate;
  727. private Object[] m_args;
  728. internal InventoryRequest(Delegate delegat, Object[] args)
  729. {
  730. m_delegate = delegat;
  731. m_args = args;
  732. }
  733. public void Execute()
  734. {
  735. if (m_delegate != null)
  736. m_delegate.DynamicInvoke(m_args);
  737. }
  738. }
  739. }