UserProfileCacheService.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. }
  113. else
  114. {
  115. m_log.ErrorFormat("[USER CACHE]: RequestInventoryForUser() - user profile for user {0} not found", userID);
  116. }
  117. }
  118. /// <summary>
  119. /// Get the details of the given user. A caller should try this method first if it isn't sure that
  120. /// a user profile exists for the given user.
  121. /// </summary>
  122. /// <param name="userID"></param>
  123. /// <returns>null if no user details are found</returns>
  124. public CachedUserInfo GetUserDetails(LLUUID userID)
  125. {
  126. if (m_userProfiles.ContainsKey(userID))
  127. return m_userProfiles[userID];
  128. else
  129. return null;
  130. }
  131. /// <summary>
  132. /// Handle an inventory folder creation request from the client.
  133. /// </summary>
  134. /// <param name="remoteClient"></param>
  135. /// <param name="folderID"></param>
  136. /// <param name="folderType"></param>
  137. /// <param name="folderName"></param>
  138. /// <param name="parentID"></param>
  139. public void HandleCreateInventoryFolder(IClientAPI remoteClient, LLUUID folderID, ushort folderType,
  140. string folderName, LLUUID parentID)
  141. {
  142. CachedUserInfo userProfile;
  143. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  144. {
  145. if (!userProfile.CreateFolder(folderName, folderID, folderType, parentID))
  146. {
  147. m_log.ErrorFormat(
  148. "[AGENT INVENTORY]: Failed to create folder for user {0} {1}",
  149. remoteClient.Name, remoteClient.AgentId);
  150. }
  151. }
  152. else
  153. {
  154. m_log.ErrorFormat(
  155. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  156. remoteClient.Name, remoteClient.AgentId);
  157. }
  158. }
  159. /// <summary>
  160. /// Handle a client request to update the inventory folder
  161. ///
  162. /// FIXME: We call add new inventory folder because in the data layer, we happen to use an SQL REPLACE
  163. /// so this will work to rename an existing folder. Needless to say, to rely on this is very confusing,
  164. /// and needs to be changed.
  165. /// </summary>
  166. /// <param name="remoteClient"></param>
  167. /// <param name="folderID"></param>
  168. /// <param name="type"></param>
  169. /// <param name="name"></param>
  170. /// <param name="parentID"></param>
  171. public void HandleUpdateInventoryFolder(IClientAPI remoteClient, LLUUID folderID, ushort type, string name,
  172. LLUUID parentID)
  173. {
  174. // m_log.DebugFormat(
  175. // "[AGENT INVENTORY]: Updating inventory folder {0} {1} for {2} {3}", folderID, name, remoteClient.Name, remoteClient.AgentId);
  176. CachedUserInfo userProfile;
  177. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  178. {
  179. if (!userProfile.UpdateFolder(name, folderID, type, parentID))
  180. {
  181. m_log.ErrorFormat(
  182. "[AGENT INVENTORY]: Failed to update folder for user {0} {1}",
  183. remoteClient.Name, remoteClient.AgentId);
  184. }
  185. }
  186. else
  187. {
  188. m_log.ErrorFormat(
  189. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  190. remoteClient.Name, remoteClient.AgentId);
  191. }
  192. }
  193. /// <summary>
  194. /// Handle an inventory folder move request from the client.
  195. /// </summary>
  196. /// <param name="remoteClient"></param>
  197. /// <param name="folderID"></param>
  198. /// <param name="parentID"></param>
  199. public void HandleMoveInventoryFolder(IClientAPI remoteClient, LLUUID folderID, LLUUID parentID)
  200. {
  201. CachedUserInfo userProfile;
  202. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  203. {
  204. if (!userProfile.MoveFolder(folderID, parentID))
  205. {
  206. m_log.ErrorFormat(
  207. "[AGENT INVENTORY]: Failed to move folder for user {0} {1}",
  208. remoteClient.Name, remoteClient.AgentId);
  209. }
  210. }
  211. else
  212. {
  213. m_log.ErrorFormat(
  214. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  215. remoteClient.Name, remoteClient.AgentId);
  216. }
  217. }
  218. /// <summary>
  219. /// Tell the client about the various child items and folders contained in the requested folder.
  220. /// </summary>
  221. /// <param name="remoteClient"></param>
  222. /// <param name="folderID"></param>
  223. /// <param name="ownerID"></param>
  224. /// <param name="fetchFolders"></param>
  225. /// <param name="fetchItems"></param>
  226. /// <param name="sortOrder"></param>
  227. public void HandleFetchInventoryDescendents(IClientAPI remoteClient, LLUUID folderID, LLUUID ownerID,
  228. bool fetchFolders, bool fetchItems, int sortOrder)
  229. {
  230. // FIXME MAYBE: We're not handling sortOrder!
  231. InventoryFolderImpl fold = null;
  232. if ((fold = libraryRoot.FindFolder(folderID)) != null)
  233. {
  234. remoteClient.SendInventoryFolderDetails(
  235. libraryRoot.Owner, folderID, fold.RequestListOfItems(),
  236. fold.RequestListOfFolders(), fetchFolders, fetchItems);
  237. return;
  238. }
  239. CachedUserInfo userProfile;
  240. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  241. {
  242. userProfile.SendInventoryDecendents(remoteClient, folderID, fetchFolders, fetchItems);
  243. }
  244. else
  245. {
  246. m_log.ErrorFormat(
  247. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  248. remoteClient.Name, remoteClient.AgentId);
  249. }
  250. }
  251. /// <summary>
  252. /// Handle the caps inventory descendents fetch.
  253. ///
  254. /// Since the folder structure is sent to the client on login, I believe we only need to handle items.
  255. /// </summary>
  256. /// <param name="agentID"></param>
  257. /// <param name="folderID"></param>
  258. /// <param name="ownerID"></param>
  259. /// <param name="fetchFolders"></param>
  260. /// <param name="fetchItems"></param>
  261. /// <param name="sortOrder"></param>
  262. /// <returns>null if the inventory look up failed</returns>
  263. public List<InventoryItemBase> HandleFetchInventoryDescendentsCAPS(LLUUID agentID, LLUUID folderID, LLUUID ownerID,
  264. bool fetchFolders, bool fetchItems, int sortOrder)
  265. {
  266. // m_log.DebugFormat(
  267. // "[INVENTORY CACHE]: Fetching folders ({0}), items ({1}) from {2} for agent {3}",
  268. // fetchFolders, fetchItems, folderID, agentID);
  269. // FIXME MAYBE: We're not handling sortOrder!
  270. InventoryFolderImpl fold;
  271. if ((fold = libraryRoot.FindFolder(folderID)) != null)
  272. {
  273. return fold.RequestListOfItems();
  274. }
  275. CachedUserInfo userProfile;
  276. if (m_userProfiles.TryGetValue(agentID, out userProfile))
  277. {
  278. // XXX: When a client crosses into a scene, their entire inventory is fetched
  279. // asynchronously. If the client makes a request before the inventory is received, we need
  280. // to give the inventory a chance to come in.
  281. //
  282. // This is a crude way of dealing with that by retrying the lookup. It's not quite as bad
  283. // in CAPS as doing this with the udp request, since here it won't hold up other packets.
  284. // In fact, here we'll be generous and try for longer.
  285. if (!userProfile.HasInventory)
  286. {
  287. int attempts = 0;
  288. while (attempts++ < 30)
  289. {
  290. m_log.DebugFormat(
  291. "[INVENTORY CACHE]: Poll number {0} for inventory items in folder {1} for user {2}",
  292. attempts, folderID, agentID);
  293. Thread.Sleep(2000);
  294. if (userProfile.HasInventory)
  295. {
  296. break;
  297. }
  298. }
  299. }
  300. if (userProfile.HasInventory)
  301. {
  302. if ((fold = userProfile.RootFolder.FindFolder(folderID)) != null)
  303. {
  304. return fold.RequestListOfItems();
  305. }
  306. else
  307. {
  308. m_log.WarnFormat(
  309. "[AGENT INVENTORY]: Could not find folder {0} requested by user {1}",
  310. folderID, agentID);
  311. return null;
  312. }
  313. }
  314. else
  315. {
  316. m_log.ErrorFormat("[INVENTORY CACHE]: Could not find root folder for user {0}", agentID);
  317. return null;
  318. }
  319. }
  320. else
  321. {
  322. m_log.ErrorFormat("[AGENT INVENTORY]: Could not find user profile for {0}", agentID);
  323. return null;
  324. }
  325. }
  326. /// <summary>
  327. /// This should delete all the items and folders in the given directory.
  328. /// </summary>
  329. /// <param name="remoteClient"></param>
  330. /// <param name="folderID"></param>
  331. public void HandlePurgeInventoryDescendents(IClientAPI remoteClient, LLUUID folderID)
  332. {
  333. CachedUserInfo userProfile;
  334. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  335. {
  336. if (!userProfile.PurgeFolder(folderID))
  337. {
  338. m_log.ErrorFormat(
  339. "[AGENT INVENTORY]: Failed to purge folder for user {0} {1}",
  340. remoteClient.Name, remoteClient.AgentId);
  341. }
  342. }
  343. else
  344. {
  345. m_log.ErrorFormat(
  346. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  347. remoteClient.Name, remoteClient.AgentId);
  348. }
  349. }
  350. public void HandleFetchInventory(IClientAPI remoteClient, LLUUID itemID, LLUUID ownerID)
  351. {
  352. if (ownerID == libraryRoot.Owner)
  353. {
  354. //Console.WriteLine("request info for library item");
  355. return;
  356. }
  357. CachedUserInfo userProfile;
  358. if (m_userProfiles.TryGetValue(remoteClient.AgentId, out userProfile))
  359. {
  360. if (userProfile.HasInventory)
  361. {
  362. InventoryItemBase item = userProfile.RootFolder.FindItem(itemID);
  363. if (item != null)
  364. {
  365. remoteClient.SendInventoryItemDetails(ownerID, item);
  366. }
  367. }
  368. }
  369. else
  370. {
  371. m_log.ErrorFormat(
  372. "[AGENT INVENTORY]: Could not find user profile for {0} {1}",
  373. remoteClient.Name, remoteClient.AgentId);
  374. }
  375. }
  376. }
  377. }