CachedUserInfo.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. namespace OpenSim.Framework.Communications.Cache
  33. {
  34. /// <summary>
  35. /// Stores user profile and inventory data received from backend services for a particular user.
  36. /// </summary>
  37. public class CachedUserInfo
  38. {
  39. private static readonly ILog m_log
  40. = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  41. /// <summary>
  42. /// The comms manager holds references to services (user, grid, inventory, etc.)
  43. /// </summary>
  44. private readonly CommunicationsManager m_commsManager;
  45. public UserProfileData UserProfile { get { return m_userProfile; } }
  46. private UserProfileData m_userProfile;
  47. /// <summary>
  48. /// Has we received the user's inventory from the inventory service?
  49. /// </summary>
  50. private bool m_hasInventory;
  51. /// <summary>
  52. /// Inventory requests waiting for receipt of this user's inventory from the inventory service.
  53. /// </summary>
  54. private readonly IList<IInventoryRequest> m_pendingRequests = new List<IInventoryRequest>();
  55. /// <summary>
  56. /// Has this user info object yet received its inventory information from the invetnroy service?
  57. /// </summary>
  58. public bool HasInventory { get { return m_hasInventory; } }
  59. private InventoryFolderImpl m_rootFolder;
  60. public InventoryFolderImpl RootFolder { get { return m_rootFolder; } }
  61. /// <summary>
  62. /// FIXME: This could be contained within a local variable - it doesn't need to be a field
  63. /// </summary>
  64. private IDictionary<LLUUID, IList<InventoryFolderImpl>> pendingCategorizationFolders
  65. = new Dictionary<LLUUID, IList<InventoryFolderImpl>>();
  66. /// <summary>
  67. /// Constructor
  68. /// </summary>
  69. /// <param name="commsManager"></param>
  70. /// <param name="userProfile"></param>
  71. public CachedUserInfo(CommunicationsManager commsManager, UserProfileData userProfile)
  72. {
  73. m_commsManager = commsManager;
  74. m_userProfile = userProfile;
  75. }
  76. /// <summary>
  77. /// This allows a request to be added to be processed once we receive a user's inventory
  78. /// from the inventory service. If we already have the inventory, the request
  79. /// is executed immediately instead.
  80. /// </summary>
  81. /// <param name="parent"></param>
  82. public void AddRequest(IInventoryRequest request)
  83. {
  84. lock (m_pendingRequests)
  85. {
  86. if (m_hasInventory)
  87. {
  88. request.Execute();
  89. }
  90. else
  91. {
  92. m_pendingRequests.Add(request);
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// Store a folder pending categorization when its parent is received.
  98. /// </summary>
  99. /// <param name="folder"></param>
  100. private void AddPendingFolder(InventoryFolderImpl folder)
  101. {
  102. LLUUID parentFolderId = folder.ParentID;
  103. if (pendingCategorizationFolders.ContainsKey(parentFolderId))
  104. {
  105. pendingCategorizationFolders[parentFolderId].Add(folder);
  106. }
  107. else
  108. {
  109. IList<InventoryFolderImpl> folders = new List<InventoryFolderImpl>();
  110. folders.Add(folder);
  111. pendingCategorizationFolders[parentFolderId] = folders;
  112. }
  113. }
  114. /// <summary>
  115. /// Add any pending folders which are children of parent
  116. /// </summary>
  117. /// <param name="parentId">
  118. /// A <see cref="LLUUID"/>
  119. /// </param>
  120. private void ResolvePendingFolders(InventoryFolderImpl parent)
  121. {
  122. if (pendingCategorizationFolders.ContainsKey(parent.ID))
  123. {
  124. foreach (InventoryFolderImpl folder in pendingCategorizationFolders[parent.ID])
  125. {
  126. // m_log.DebugFormat(
  127. // "[INVENTORY CACHE]: Resolving pending received folder {0} {1} into {2} {3}",
  128. // folder.name, folder.folderID, parent.name, parent.folderID);
  129. lock (parent.SubFolders)
  130. {
  131. if (!parent.SubFolders.ContainsKey(folder.ID))
  132. {
  133. parent.SubFolders.Add(folder.ID, folder);
  134. }
  135. }
  136. }
  137. }
  138. }
  139. /// <summary>
  140. /// Callback invoked when the inventory is received from an async request to the inventory service
  141. /// </summary>
  142. /// <param name="userID"></param>
  143. /// <param name="inventoryCollection"></param>
  144. public void InventoryReceive(LLUUID userID, ICollection<InventoryFolderImpl> folders, ICollection<InventoryItemBase> items)
  145. {
  146. // FIXME: Exceptions thrown upwards never appear on the console. Could fix further up if these
  147. // are simply being swallowed
  148. try
  149. {
  150. foreach (InventoryFolderImpl folder in folders)
  151. {
  152. FolderReceive(userID, folder);
  153. }
  154. foreach (InventoryItemBase item in items)
  155. {
  156. ItemReceive(userID, item);
  157. }
  158. }
  159. catch (Exception e)
  160. {
  161. m_log.ErrorFormat("[INVENTORY CACHE]: Error processing inventory received from inventory service, {0}", e);
  162. }
  163. // Deal with pending requests
  164. lock (m_pendingRequests)
  165. {
  166. // We're going to change inventory status within the lock to avoid a race condition
  167. // where requests are processed after the AddRequest() method has been called.
  168. m_hasInventory = true;
  169. foreach (IInventoryRequest request in m_pendingRequests)
  170. {
  171. request.Execute();
  172. }
  173. }
  174. }
  175. /// <summary>
  176. /// Callback invoked when a folder is received from an async request to the inventory service.
  177. /// </summary>
  178. /// <param name="userID"></param>
  179. /// <param name="folderInfo"></param>
  180. private void FolderReceive(LLUUID userID, InventoryFolderImpl folderInfo)
  181. {
  182. // m_log.DebugFormat(
  183. // "[INVENTORY CACHE]: Received folder {0} {1} for user {2}",
  184. // folderInfo.Name, folderInfo.ID, userID);
  185. if (userID == UserProfile.ID)
  186. {
  187. if (RootFolder == null)
  188. {
  189. if (folderInfo.ParentID == LLUUID.Zero)
  190. {
  191. m_rootFolder = folderInfo;
  192. }
  193. }
  194. else if (RootFolder.ID == folderInfo.ParentID)
  195. {
  196. lock (RootFolder.SubFolders)
  197. {
  198. if (!RootFolder.SubFolders.ContainsKey(folderInfo.ID))
  199. {
  200. RootFolder.SubFolders.Add(folderInfo.ID, folderInfo);
  201. }
  202. else
  203. {
  204. AddPendingFolder(folderInfo);
  205. }
  206. }
  207. }
  208. else
  209. {
  210. InventoryFolderImpl folder = RootFolder.GetDescendentFolder(folderInfo.ParentID);
  211. lock (folder.SubFolders)
  212. {
  213. if (folder != null)
  214. {
  215. if (!folder.SubFolders.ContainsKey(folderInfo.ID))
  216. {
  217. folder.SubFolders.Add(folderInfo.ID, folderInfo);
  218. }
  219. }
  220. else
  221. {
  222. AddPendingFolder(folderInfo);
  223. }
  224. }
  225. }
  226. ResolvePendingFolders(folderInfo);
  227. }
  228. }
  229. /// <summary>
  230. /// Callback invoked when an item is received from an async request to the inventory service.
  231. ///
  232. /// We're assuming here that items are always received after all the folders have been
  233. /// received.
  234. /// </summary>
  235. /// <param name="userID"></param>
  236. /// <param name="folderInfo"></param>
  237. private void ItemReceive(LLUUID userID, InventoryItemBase itemInfo)
  238. {
  239. // m_log.DebugFormat(
  240. // "[INVENTORY CACHE]: Received item {0} {1} for user {2}",
  241. // itemInfo.Name, itemInfo.ID, userID);
  242. if ((userID == UserProfile.ID) && (RootFolder != null))
  243. {
  244. if (itemInfo.Folder == RootFolder.ID)
  245. {
  246. lock (RootFolder.Items)
  247. {
  248. if (!RootFolder.Items.ContainsKey(itemInfo.ID))
  249. {
  250. RootFolder.Items.Add(itemInfo.ID, itemInfo);
  251. }
  252. else
  253. {
  254. RootFolder.Items[itemInfo.ID] = itemInfo;
  255. }
  256. }
  257. }
  258. else
  259. {
  260. InventoryFolderImpl folder = RootFolder.GetDescendentFolder(itemInfo.Folder);
  261. if (folder != null)
  262. {
  263. lock (folder.Items)
  264. {
  265. if (!folder.Items.ContainsKey(itemInfo.ID))
  266. {
  267. folder.Items.Add(itemInfo.ID, itemInfo);
  268. }
  269. else
  270. {
  271. folder.Items[itemInfo.ID] = itemInfo;
  272. }
  273. }
  274. }
  275. }
  276. }
  277. }
  278. /// <summary>
  279. /// Add an item to the user's inventory
  280. /// </summary>
  281. /// <param name="userID"></param>
  282. /// <param name="itemInfo"></param>
  283. public void AddItem(LLUUID userID, InventoryItemBase itemInfo)
  284. {
  285. if ((userID == UserProfile.ID) && HasInventory)
  286. {
  287. ItemReceive(userID, itemInfo);
  288. m_commsManager.InventoryService.AddNewInventoryItem(userID, itemInfo);
  289. }
  290. }
  291. /// <summary>
  292. /// Update an item in the user's inventory
  293. /// </summary>
  294. /// <param name="userID"></param>
  295. /// <param name="itemInfo"></param>
  296. public void UpdateItem(LLUUID userID, InventoryItemBase itemInfo)
  297. {
  298. if ((userID == UserProfile.ID) && HasInventory)
  299. {
  300. m_commsManager.InventoryService.UpdateInventoryItem(userID, itemInfo);
  301. }
  302. }
  303. /// <summary>
  304. /// Delete an item from the user's inventory
  305. /// </summary>
  306. /// <param name="userID"></param>
  307. /// <param name="item"></param>
  308. /// <returns></returns>
  309. public bool DeleteItem(LLUUID userID, InventoryItemBase item)
  310. {
  311. bool result = false;
  312. if ((userID == UserProfile.ID) && HasInventory)
  313. {
  314. result = RootFolder.DeleteItem(item.ID);
  315. if (result)
  316. {
  317. m_commsManager.InventoryService.DeleteInventoryItem(userID, item);
  318. }
  319. }
  320. return result;
  321. }
  322. }
  323. /// <summary>
  324. /// Should be implemented by callers which require a callback when the user's inventory is received
  325. /// </summary>
  326. public interface IInventoryRequest
  327. {
  328. /// <summary>
  329. /// This is the method executed once we have received the user's inventory by which the request can be fulfilled.
  330. /// </summary>
  331. void Execute();
  332. }
  333. /// <summary>
  334. /// Generic inventory request
  335. /// </summary>
  336. public class InventoryRequest : IInventoryRequest
  337. {
  338. private Delegate m_delegat;
  339. private Object[] m_args;
  340. internal InventoryRequest(Delegate delegat, Object[] args)
  341. {
  342. m_delegat = delegat;
  343. m_args = args;
  344. }
  345. public void Execute()
  346. {
  347. m_delegat.DynamicInvoke(m_args);
  348. }
  349. }
  350. }