UserProfileCacheService.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.Collections.Generic;
  28. using System.Reflection;
  29. using log4net;
  30. using OpenMetaverse;
  31. using OpenSim.Services.Interfaces;
  32. namespace OpenSim.Framework.Communications.Cache
  33. {
  34. /// <summary>
  35. /// Holds user profile information and retrieves it from backend services.
  36. /// </summary>
  37. public class UserProfileCacheService
  38. {
  39. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. /// <value>
  41. /// Standard format for names.
  42. /// </value>
  43. public const string NAME_FORMAT = "{0} {1}";
  44. /// <summary>
  45. /// The comms manager holds references to services (user, grid, inventory, etc.)
  46. /// </summary>
  47. private readonly CommunicationsManager m_commsManager;
  48. /// <summary>
  49. /// User profiles indexed by UUID
  50. /// </summary>
  51. private readonly Dictionary<UUID, CachedUserInfo> m_userProfilesById
  52. = new Dictionary<UUID, CachedUserInfo>();
  53. /// <summary>
  54. /// User profiles indexed by name
  55. /// </summary>
  56. private readonly Dictionary<string, CachedUserInfo> m_userProfilesByName
  57. = new Dictionary<string, CachedUserInfo>();
  58. /// <summary>
  59. /// The root library folder.
  60. /// </summary>
  61. public readonly InventoryFolderImpl LibraryRoot;
  62. private IInventoryService m_InventoryService;
  63. /// <summary>
  64. /// Constructor
  65. /// </summary>
  66. /// <param name="commsManager"></param>
  67. /// <param name="libraryRootFolder"></param>
  68. public UserProfileCacheService(CommunicationsManager commsManager, LibraryRootFolder libraryRootFolder)
  69. {
  70. m_commsManager = commsManager;
  71. LibraryRoot = libraryRootFolder;
  72. }
  73. public void SetInventoryService(IInventoryService invService)
  74. {
  75. m_InventoryService = invService;
  76. }
  77. /// <summary>
  78. /// A new user has moved into a region in this instance so retrieve their profile from the user service.
  79. /// </summary>
  80. ///
  81. /// It isn't strictly necessary to make this call since user data can be lazily requested later on. However,
  82. /// it might be helpful in order to avoid an initial response delay later on
  83. ///
  84. /// <param name="userID"></param>
  85. public void AddNewUser(UUID userID)
  86. {
  87. if (userID == UUID.Zero)
  88. return;
  89. //m_log.DebugFormat("[USER CACHE]: Adding user profile for {0}", userID);
  90. GetUserDetails(userID);
  91. }
  92. /// <summary>
  93. /// Remove this user's profile cache.
  94. /// </summary>
  95. /// <param name="userID"></param>
  96. /// <returns>true if the user was successfully removed, false otherwise</returns>
  97. public bool RemoveUser(UUID userId)
  98. {
  99. if (!RemoveFromCaches(userId))
  100. {
  101. m_log.WarnFormat(
  102. "[USER CACHE]: Tried to remove the profile of user {0}, but this was not in the scene", userId);
  103. return false;
  104. }
  105. return true;
  106. }
  107. /// <summary>
  108. /// Get details of the given user.
  109. /// </summary>
  110. /// If the user isn't in cache then the user is requested from the profile service.
  111. /// <param name="userID"></param>
  112. /// <returns>null if no user details are found</returns>
  113. public CachedUserInfo GetUserDetails(string fname, string lname)
  114. {
  115. lock (m_userProfilesByName)
  116. {
  117. CachedUserInfo userInfo;
  118. if (m_userProfilesByName.TryGetValue(string.Format(NAME_FORMAT, fname, lname), out userInfo))
  119. {
  120. return userInfo;
  121. }
  122. else
  123. {
  124. UserProfileData userProfile = m_commsManager.UserService.GetUserProfile(fname, lname);
  125. if (userProfile != null)
  126. return AddToCaches(userProfile);
  127. else
  128. return null;
  129. }
  130. }
  131. }
  132. /// <summary>
  133. /// Get details of the given user.
  134. /// </summary>
  135. /// If the user isn't in cache then the user is requested from the profile service.
  136. /// <param name="userID"></param>
  137. /// <returns>null if no user details are found</returns>
  138. public CachedUserInfo GetUserDetails(UUID userID)
  139. {
  140. if (userID == UUID.Zero)
  141. return null;
  142. lock (m_userProfilesById)
  143. {
  144. if (m_userProfilesById.ContainsKey(userID))
  145. {
  146. return m_userProfilesById[userID];
  147. }
  148. else
  149. {
  150. UserProfileData userProfile = m_commsManager.UserService.GetUserProfile(userID);
  151. if (userProfile != null)
  152. return AddToCaches(userProfile);
  153. else
  154. return null;
  155. }
  156. }
  157. }
  158. /// <summary>
  159. /// Update an existing profile
  160. /// </summary>
  161. /// <param name="userProfile"></param>
  162. /// <returns>true if a user profile was found to update, false otherwise</returns>
  163. // Commented out for now. The implementation needs to be improved by protecting against race conditions,
  164. // probably by making sure that the update doesn't use the UserCacheInfo.UserProfile directly (possibly via
  165. // returning a read only class from the cache).
  166. // public bool UpdateProfile(UserProfileData userProfile)
  167. // {
  168. // lock (m_userProfilesById)
  169. // {
  170. // CachedUserInfo userInfo = GetUserDetails(userProfile.ID);
  171. //
  172. // if (userInfo != null)
  173. // {
  174. // userInfo.m_userProfile = userProfile;
  175. // m_commsManager.UserService.UpdateUserProfile(userProfile);
  176. //
  177. // return true;
  178. // }
  179. // }
  180. //
  181. // return false;
  182. // }
  183. /// <summary>
  184. /// Populate caches with the given user profile
  185. /// </summary>
  186. /// <param name="userProfile"></param>
  187. protected CachedUserInfo AddToCaches(UserProfileData userProfile)
  188. {
  189. CachedUserInfo createdUserInfo = new CachedUserInfo(m_InventoryService, userProfile);
  190. lock (m_userProfilesById)
  191. {
  192. m_userProfilesById[createdUserInfo.UserProfile.ID] = createdUserInfo;
  193. lock (m_userProfilesByName)
  194. {
  195. m_userProfilesByName[createdUserInfo.UserProfile.Name] = createdUserInfo;
  196. }
  197. }
  198. return createdUserInfo;
  199. }
  200. /// <summary>
  201. /// Remove profile belong to the given uuid from the caches
  202. /// </summary>
  203. /// <param name="userUuid"></param>
  204. /// <returns>true if there was a profile to remove, false otherwise</returns>
  205. protected bool RemoveFromCaches(UUID userId)
  206. {
  207. lock (m_userProfilesById)
  208. {
  209. if (m_userProfilesById.ContainsKey(userId))
  210. {
  211. CachedUserInfo userInfo = m_userProfilesById[userId];
  212. m_userProfilesById.Remove(userId);
  213. lock (m_userProfilesByName)
  214. {
  215. m_userProfilesByName.Remove(userInfo.UserProfile.Name);
  216. }
  217. return true;
  218. }
  219. }
  220. return false;
  221. }
  222. /// <summary>
  223. /// Preloads User data into the region cache. Modules may use this service to add non-standard clients
  224. /// </summary>
  225. /// <param name="userData"></param>
  226. public void PreloadUserCache(UserProfileData userData)
  227. {
  228. AddToCaches(userData);
  229. }
  230. }
  231. }