CachedUserInfo.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. namespace OpenSim.Framework.Communications.Cache
  31. {
  32. public class CachedUserInfo
  33. {
  34. private static readonly log4net.ILog m_log
  35. = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
  36. private readonly CommunicationsManager m_parentCommsManager;
  37. // FIXME: These need to be hidden behind accessors
  38. public InventoryFolderImpl RootFolder = null;
  39. public UserProfileData UserProfile = null;
  40. /// <summary>
  41. /// Stores received folders for which we have not yet received the parents.
  42. /// </summary></param>
  43. private IDictionary<LLUUID, IList<InventoryFolderImpl>> pendingCategorizationFolders
  44. = new Dictionary<LLUUID, IList<InventoryFolderImpl>>();
  45. public CachedUserInfo(CommunicationsManager commsManager)
  46. {
  47. m_parentCommsManager = commsManager;
  48. }
  49. /// <summary>
  50. /// Store a folder pending categorization when its parent is received.
  51. /// </summary>
  52. /// <param name="folder"></param>
  53. private void AddPendingFolder(InventoryFolderImpl folder)
  54. {
  55. LLUUID parentFolderId = folder.parentID;
  56. if (pendingCategorizationFolders.ContainsKey(parentFolderId))
  57. {
  58. pendingCategorizationFolders[parentFolderId].Add(folder);
  59. }
  60. else
  61. {
  62. IList<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  63. folders.Add(folder);
  64. pendingCategorizationFolders[parentFolderId] = folders;
  65. }
  66. }
  67. /// <summary>
  68. /// Add any pending folders which are children of parent
  69. /// </summary>
  70. /// <param name="parentId">
  71. /// A <see cref="LLUUID"/>
  72. /// </param>
  73. private void ResolvePendingFolders(InventoryFolderImpl parent)
  74. {
  75. if (pendingCategorizationFolders.ContainsKey(parent.folderID))
  76. {
  77. foreach (InventoryFolderImpl folder in pendingCategorizationFolders[parent.folderID])
  78. {
  79. // m_log.DebugFormat(
  80. // "[INVENTORY CACHE]: Resolving pending received folder {0} {1} into {2} {3}",
  81. // folder.name, folder.folderID, parent.name, parent.folderID);
  82. if (!parent.SubFolders.ContainsKey(folder.folderID))
  83. {
  84. parent.SubFolders.Add(folder.folderID, folder);
  85. }
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// Callback invoked when a folder is received from an async request to the inventory service.
  91. /// </summary>
  92. /// <param name="userID"></param>
  93. /// <param name="folderInfo"></param>
  94. public void FolderReceive(LLUUID userID, InventoryFolderImpl folderInfo)
  95. {
  96. // FIXME: Exceptions thrown upwards never appear on the console. Could fix further up if these
  97. // are simply being swallowed
  98. try
  99. {
  100. // m_log.DebugFormat(
  101. // "[INVENTORY CACHE]: Received folder {0} {1} for user {2}",
  102. // folderInfo.name, folderInfo.folderID, userID);
  103. if (userID == UserProfile.UUID)
  104. {
  105. if (RootFolder == null)
  106. {
  107. if (folderInfo.parentID == LLUUID.Zero)
  108. {
  109. RootFolder = folderInfo;
  110. }
  111. }
  112. else if (RootFolder.folderID == folderInfo.parentID)
  113. {
  114. if (!RootFolder.SubFolders.ContainsKey(folderInfo.folderID))
  115. {
  116. RootFolder.SubFolders.Add(folderInfo.folderID, folderInfo);
  117. }
  118. else
  119. {
  120. AddPendingFolder(folderInfo);
  121. }
  122. }
  123. else
  124. {
  125. InventoryFolderImpl folder = RootFolder.HasSubFolder(folderInfo.parentID);
  126. if (folder != null)
  127. {
  128. if (!folder.SubFolders.ContainsKey(folderInfo.folderID))
  129. {
  130. folder.SubFolders.Add(folderInfo.folderID, folderInfo);
  131. }
  132. }
  133. else
  134. {
  135. AddPendingFolder(folderInfo);
  136. }
  137. }
  138. ResolvePendingFolders(folderInfo);
  139. }
  140. }
  141. catch (Exception e)
  142. {
  143. m_log.ErrorFormat("[INVENTORY CACHE] {0}", e);
  144. }
  145. }
  146. /// <summary>
  147. /// Callback invoked when an item is received from an async request to the inventory service.
  148. ///
  149. /// FIXME: We're assuming here that items are always received after all the folders have been
  150. /// received.
  151. /// </summary>
  152. /// <param name="userID"></param>
  153. /// <param name="folderInfo"></param>
  154. public void ItemReceive(LLUUID userID, InventoryItemBase itemInfo)
  155. {
  156. if ((userID == UserProfile.UUID) && (RootFolder != null))
  157. {
  158. if (itemInfo.parentFolderID == RootFolder.folderID)
  159. {
  160. if (!RootFolder.Items.ContainsKey(itemInfo.inventoryID))
  161. {
  162. RootFolder.Items.Add(itemInfo.inventoryID, itemInfo);
  163. }
  164. }
  165. else
  166. {
  167. InventoryFolderImpl folder = RootFolder.HasSubFolder(itemInfo.parentFolderID);
  168. if (folder != null)
  169. {
  170. if (!folder.Items.ContainsKey(itemInfo.inventoryID))
  171. {
  172. folder.Items.Add(itemInfo.inventoryID, itemInfo);
  173. }
  174. }
  175. }
  176. }
  177. }
  178. public void AddItem(LLUUID userID, InventoryItemBase itemInfo)
  179. {
  180. if ((userID == UserProfile.UUID) && (RootFolder != null))
  181. {
  182. ItemReceive(userID, itemInfo);
  183. m_parentCommsManager.InventoryService.AddNewInventoryItem(userID, itemInfo);
  184. }
  185. }
  186. public void UpdateItem(LLUUID userID, InventoryItemBase itemInfo)
  187. {
  188. if ((userID == UserProfile.UUID) && (RootFolder != null))
  189. {
  190. m_parentCommsManager.InventoryService.AddNewInventoryItem(userID, itemInfo);
  191. }
  192. }
  193. public bool DeleteItem(LLUUID userID, InventoryItemBase item)
  194. {
  195. bool result = false;
  196. if ((userID == UserProfile.UUID) && (RootFolder != null))
  197. {
  198. result = RootFolder.DeleteItem(item.inventoryID);
  199. if (result)
  200. {
  201. m_parentCommsManager.InventoryService.DeleteInventoryItem(userID, item);
  202. }
  203. }
  204. return result;
  205. }
  206. }
  207. }