UserProfileCacheService.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 System.Threading;
  31. using libsecondlife;
  32. using log4net;
  33. namespace OpenSim.Framework.Communications.Cache
  34. {
  35. /// <summary>
  36. /// Holds user profile information and retrieves it from backend services.
  37. /// </summary>
  38. public class UserProfileCacheService
  39. {
  40. private static readonly ILog m_log = 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. /// <summary>
  46. /// Each user has a cached profile.
  47. /// </summary>
  48. private readonly Dictionary<LLUUID, CachedUserInfo> m_userProfiles = new Dictionary<LLUUID, CachedUserInfo>();
  49. public readonly LibraryRootFolder libraryRoot = new LibraryRootFolder();
  50. // Methods
  51. public UserProfileCacheService(CommunicationsManager commsManager)
  52. {
  53. m_commsManager = commsManager;
  54. }
  55. /// <summary>
  56. /// A new user has moved into a region in this instance so retrieve their profile from the user service.
  57. /// </summary>
  58. /// <param name="userID"></param>
  59. public void AddNewUser(LLUUID userID)
  60. {
  61. // Potential fix - Multithreading issue.
  62. lock (m_userProfiles)
  63. {
  64. if (!m_userProfiles.ContainsKey(userID))
  65. {
  66. UserProfileData userProfile = m_commsManager.UserService.GetUserProfile(userID);
  67. CachedUserInfo userInfo = new CachedUserInfo(m_commsManager, userProfile);
  68. if (userInfo.UserProfile != null)
  69. {
  70. // The inventory for the user will be populated when they actually enter the scene
  71. m_userProfiles.Add(userID, userInfo);
  72. }
  73. else
  74. {
  75. m_log.ErrorFormat("[USER CACHE]: User profile for user {0} not found.", userID);
  76. }
  77. }
  78. }
  79. }
  80. /// <summary>
  81. /// Remove this user's profile cache.
  82. /// </summary>
  83. /// <param name="userID"></param>
  84. /// <returns>true if the user was successfully removed, false otherwise</returns>
  85. public bool RemoveUser(LLUUID userID)
  86. {
  87. lock (m_userProfiles)
  88. {
  89. if (m_userProfiles.ContainsKey(userID))
  90. {
  91. m_userProfiles.Remove(userID);
  92. return true;
  93. }
  94. else
  95. {
  96. m_log.ErrorFormat("[USER CACHE]: Tried to remove the profile of user {0}, but this was not in the scene", userID);
  97. }
  98. }
  99. return false;
  100. }
  101. /// <summary>
  102. /// Request the inventory data for the given user. This will occur asynchronously if running on a grid
  103. /// </summary>
  104. /// <param name="userID"></param>
  105. /// <param name="userInfo"></param>
  106. public void RequestInventoryForUser(LLUUID userID)
  107. {
  108. CachedUserInfo userInfo = GetUserDetails(userID);
  109. if (userInfo != null)
  110. {
  111. m_commsManager.InventoryService.RequestInventoryForUser(userID, userInfo.InventoryReceive);
  112. //IInventoryServices invService = userInfo.GetInventoryService();
  113. //if (invService != null)
  114. //{
  115. // invService.RequestInventoryForUser(userID, userInfo.InventoryReceive);
  116. //}
  117. }
  118. else
  119. {
  120. m_log.ErrorFormat("[USER CACHE]: RequestInventoryForUser() - user profile for user {0} not found", userID);
  121. }
  122. }
  123. /// <summary>
  124. /// Get the details of the given user. A caller should try this method first if it isn't sure that
  125. /// a user profile exists for the given user.
  126. /// </summary>
  127. /// <param name="userID"></param>
  128. /// <returns>null if no user details are found</returns>
  129. public CachedUserInfo GetUserDetails(LLUUID userID)
  130. {
  131. if (m_userProfiles.ContainsKey(userID))
  132. return m_userProfiles[userID];
  133. else
  134. return null;
  135. }
  136. /// <summary>
  137. /// Handle an inventory folder creation request from the client.
  138. /// </summary>
  139. /// <param name="remoteClient"></param>
  140. /// <param name="folderID"></param>
  141. /// <param name="folderType"></param>
  142. /// <param name="folderName"></param>
  143. /// <param name="parentID"></param>
  144. public void HandleCreateInventoryFolder(IClientAPI remoteClient, LLUUID folderID, ushort folderType,
  145. string folderName, LLUUID parentID)
  146. {
  147. CachedUserInfo userProfile;
  148. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  149. {
  150. if (!userProfile.CreateFolder(folderName, folderID, folderType, parentID))
  151. {
  152. m_log.ErrorFormat(
  153. "[AGENT INVENTORY]: Failed to create folder for user {0} {1}",
  154. remoteClient.Name, remoteClient.AgentId);
  155. }
  156. }
  157. else
  158. {
  159. m_log.ErrorFormat(
  160. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  161. remoteClient.Name, remoteClient.AgentId);
  162. }
  163. }
  164. /// <summary>
  165. /// Handle a client request to update the inventory folder
  166. ///
  167. /// FIXME: We call add new inventory folder because in the data layer, we happen to use an SQL REPLACE
  168. /// so this will work to rename an existing folder. Needless to say, to rely on this is very confusing,
  169. /// and needs to be changed.
  170. /// </summary>
  171. /// <param name="remoteClient"></param>
  172. /// <param name="folderID"></param>
  173. /// <param name="type"></param>
  174. /// <param name="name"></param>
  175. /// <param name="parentID"></param>
  176. public void HandleUpdateInventoryFolder(IClientAPI remoteClient, LLUUID folderID, ushort type, string name,
  177. LLUUID parentID)
  178. {
  179. // m_log.DebugFormat(
  180. // "[AGENT INVENTORY]: Updating inventory folder {0} {1} for {2} {3}", folderID, name, remoteClient.Name, remoteClient.AgentId);
  181. CachedUserInfo userProfile;
  182. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  183. {
  184. if (!userProfile.UpdateFolder(name, folderID, type, parentID))
  185. {
  186. m_log.ErrorFormat(
  187. "[AGENT INVENTORY]: Failed to update folder for user {0} {1}",
  188. remoteClient.Name, remoteClient.AgentId);
  189. }
  190. }
  191. else
  192. {
  193. m_log.ErrorFormat(
  194. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  195. remoteClient.Name, remoteClient.AgentId);
  196. }
  197. }
  198. /// <summary>
  199. /// Handle an inventory folder move request from the client.
  200. /// </summary>
  201. /// <param name="remoteClient"></param>
  202. /// <param name="folderID"></param>
  203. /// <param name="parentID"></param>
  204. public void HandleMoveInventoryFolder(IClientAPI remoteClient, LLUUID folderID, LLUUID parentID)
  205. {
  206. CachedUserInfo userProfile;
  207. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  208. {
  209. if (!userProfile.MoveFolder(folderID, parentID))
  210. {
  211. m_log.ErrorFormat(
  212. "[AGENT INVENTORY]: Failed to move folder for user {0} {1}",
  213. remoteClient.Name, remoteClient.AgentId);
  214. }
  215. }
  216. else
  217. {
  218. m_log.ErrorFormat(
  219. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  220. remoteClient.Name, remoteClient.AgentId);
  221. }
  222. }
  223. /// <summary>
  224. /// Tell the client about the various child items and folders contained in the requested folder.
  225. /// </summary>
  226. /// <param name="remoteClient"></param>
  227. /// <param name="folderID"></param>
  228. /// <param name="ownerID"></param>
  229. /// <param name="fetchFolders"></param>
  230. /// <param name="fetchItems"></param>
  231. /// <param name="sortOrder"></param>
  232. public void HandleFetchInventoryDescendents(IClientAPI remoteClient, LLUUID folderID, LLUUID ownerID,
  233. bool fetchFolders, bool fetchItems, int sortOrder)
  234. {
  235. // FIXME MAYBE: We're not handling sortOrder!
  236. InventoryFolderImpl fold = null;
  237. if ((fold = libraryRoot.FindFolder(folderID)) != null)
  238. {
  239. remoteClient.SendInventoryFolderDetails(
  240. libraryRoot.Owner, folderID, fold.RequestListOfItems(),
  241. fold.RequestListOfFolders(), fetchFolders, fetchItems);
  242. return;
  243. }
  244. CachedUserInfo userProfile;
  245. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  246. {
  247. userProfile.SendInventoryDecendents(remoteClient, folderID, fetchFolders, fetchItems);
  248. }
  249. else
  250. {
  251. m_log.ErrorFormat(
  252. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  253. remoteClient.Name, remoteClient.AgentId);
  254. }
  255. }
  256. /// <summary>
  257. /// Handle the caps inventory descendents fetch.
  258. ///
  259. /// Since the folder structure is sent to the client on login, I believe we only need to handle items.
  260. /// </summary>
  261. /// <param name="agentID"></param>
  262. /// <param name="folderID"></param>
  263. /// <param name="ownerID"></param>
  264. /// <param name="fetchFolders"></param>
  265. /// <param name="fetchItems"></param>
  266. /// <param name="sortOrder"></param>
  267. /// <returns>null if the inventory look up failed</returns>
  268. public List<InventoryItemBase> HandleFetchInventoryDescendentsCAPS(LLUUID agentID, LLUUID folderID, LLUUID ownerID,
  269. bool fetchFolders, bool fetchItems, int sortOrder)
  270. {
  271. // m_log.DebugFormat(
  272. // "[INVENTORY CACHE]: Fetching folders ({0}), items ({1}) from {2} for agent {3}",
  273. // fetchFolders, fetchItems, folderID, agentID);
  274. // FIXME MAYBE: We're not handling sortOrder!
  275. InventoryFolderImpl fold;
  276. if ((fold = libraryRoot.FindFolder(folderID)) != null)
  277. {
  278. return fold.RequestListOfItems();
  279. }
  280. CachedUserInfo userProfile;
  281. if (m_userProfiles.TryGetValue(agentID, out userProfile))
  282. {
  283. // XXX: When a client crosses into a scene, their entire inventory is fetched
  284. // asynchronously. If the client makes a request before the inventory is received, we need
  285. // to give the inventory a chance to come in.
  286. //
  287. // This is a crude way of dealing with that by retrying the lookup. It's not quite as bad
  288. // in CAPS as doing this with the udp request, since here it won't hold up other packets.
  289. // In fact, here we'll be generous and try for longer.
  290. if (!userProfile.HasInventory)
  291. {
  292. int attempts = 0;
  293. while (attempts++ < 30)
  294. {
  295. m_log.DebugFormat(
  296. "[INVENTORY CACHE]: Poll number {0} for inventory items in folder {1} for user {2}",
  297. attempts, folderID, agentID);
  298. Thread.Sleep(2000);
  299. if (userProfile.HasInventory)
  300. {
  301. break;
  302. }
  303. }
  304. }
  305. if (userProfile.HasInventory)
  306. {
  307. if ((fold = userProfile.RootFolder.FindFolder(folderID)) != null)
  308. {
  309. return fold.RequestListOfItems();
  310. }
  311. else
  312. {
  313. m_log.WarnFormat(
  314. "[AGENT INVENTORY]: Could not find folder {0} requested by user {1}",
  315. folderID, agentID);
  316. return null;
  317. }
  318. }
  319. else
  320. {
  321. m_log.ErrorFormat("[INVENTORY CACHE]: Could not find root folder for user {0}", agentID);
  322. return null;
  323. }
  324. }
  325. else
  326. {
  327. m_log.ErrorFormat("[AGENT INVENTORY]: Could not find user profile for {0}", agentID);
  328. return null;
  329. }
  330. }
  331. /// <summary>
  332. /// This should delete all the items and folders in the given directory.
  333. /// </summary>
  334. /// <param name="remoteClient"></param>
  335. /// <param name="folderID"></param>
  336. public void HandlePurgeInventoryDescendents(IClientAPI remoteClient, LLUUID folderID)
  337. {
  338. CachedUserInfo userProfile;
  339. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  340. {
  341. if (!userProfile.PurgeFolder(folderID))
  342. {
  343. m_log.ErrorFormat(
  344. "[AGENT INVENTORY]: Failed to purge folder for user {0} {1}",
  345. remoteClient.Name, remoteClient.AgentId);
  346. }
  347. }
  348. else
  349. {
  350. m_log.ErrorFormat(
  351. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  352. remoteClient.Name, remoteClient.AgentId);
  353. }
  354. }
  355. public void HandleFetchInventory(IClientAPI remoteClient, LLUUID itemID, LLUUID ownerID)
  356. {
  357. if (ownerID == libraryRoot.Owner)
  358. {
  359. //Console.WriteLine("request info for library item");
  360. return;
  361. }
  362. CachedUserInfo userProfile;
  363. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  364. {
  365. if (userProfile.HasInventory)
  366. {
  367. InventoryItemBase item = userProfile.RootFolder.FindItem(itemID);
  368. if (item != null)
  369. {
  370. remoteClient.SendInventoryItemDetails(ownerID, item);
  371. }
  372. }
  373. }
  374. else
  375. {
  376. m_log.ErrorFormat(
  377. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  378. remoteClient.Name, remoteClient.AgentId);
  379. }
  380. }
  381. }
  382. }