From 23d04fa25c937d6c56db4de3a9befa3e476ee17a Mon Sep 17 00:00:00 2001 From: "Justin Clark-Casey (justincc)" Date: Wed, 28 Mar 2012 02:51:34 +0100 Subject: [PATCH] Add "friends show cache " command for debugging purposes. This adds a reverse lookup (name -> ID) to IUserManagement instead of hitting the UserAccountService directly. --- .../Avatar/Friends/FriendsModule.cs | 76 ++++++++++++++++++- .../UserManagement/UserManagementModule.cs | 29 +++++++ .../Framework/Interfaces/IUserManagement.cs | 15 ++++ 3 files changed, 119 insertions(+), 1 deletion(-) diff --git a/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs b/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs index c266fe58ca..f59ba2b5ce 100644 --- a/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs +++ b/OpenSim/Region/CoreModules/Avatar/Friends/FriendsModule.cs @@ -214,6 +214,11 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends public void RegionLoaded(Scene scene) { + scene.AddCommand( + "Friends", this, "friends show cache", + "friends show cache [ ]", + "Show the friends cache for the given user", + HandleFriendsShowCacheCommand); } public void RemoveRegion(Scene scene) @@ -876,7 +881,9 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends /// Get friends from local cache only /// /// - /// + /// + /// An empty array if the user has no friends or friends have not been cached. + /// protected FriendInfo[] GetFriends(UUID agentID) { UserFriendData friendsData; @@ -925,6 +932,17 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends } } + /// + /// Are friends cached on this simulator for a particular user? + /// + /// + /// + protected bool AreFriendsCached(UUID userID) + { + lock (m_Friends) + return m_Friends.ContainsKey(userID); + } + protected virtual bool StoreRights(UUID agentID, UUID friendID, int rights) { FriendsService.StoreFriend(agentID.ToString(), friendID.ToString(), rights); @@ -950,5 +968,61 @@ namespace OpenSim.Region.CoreModules.Avatar.Friends } #endregion + + protected void HandleFriendsShowCacheCommand(string module, string[] cmd) + { + if (cmd.Length != 5) + { + MainConsole.Instance.OutputFormat("Usage: friends show cache [ ]"); + return; + } + + string firstName = cmd[3]; + string lastName = cmd[4]; + + IUserManagement umModule = m_Scenes[0].RequestModuleInterface(); + UUID userId = umModule.GetUserIdByName(firstName, lastName); + +// UserAccount ua +// = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, firstName, lastName); + + if (userId == UUID.Zero) + { + MainConsole.Instance.OutputFormat("No such user as {0} {1}", firstName, lastName); + return; + } + + if (!AreFriendsCached(userId)) + { + MainConsole.Instance.OutputFormat("No friends cached on this simulator for {0} {1}", firstName, lastName); + return; + } + + MainConsole.Instance.OutputFormat("Cached friends for {0} {1}:", firstName, lastName); + + MainConsole.Instance.OutputFormat("UUID\n"); + + FriendInfo[] friends = GetFriends(userId); + + foreach (FriendInfo friend in friends) + { +// MainConsole.Instance.OutputFormat(friend.PrincipalID.ToString()); + +// string friendFirstName, friendLastName; +// +// UserAccount friendUa +// = m_Scenes[0].UserAccountService.GetUserAccount(m_Scenes[0].RegionInfo.ScopeID, friend.PrincipalID); + + UUID friendId; + string friendName; + + if (UUID.TryParse(friend.Friend, out friendId)) + friendName = umModule.GetUserName(friendId); + else + friendName = friend.Friend; + + MainConsole.Instance.OutputFormat("{0} {1} {2}", friendName, friend.MyFlags, friend.TheirFlags); + } + } } } diff --git a/OpenSim/Region/CoreModules/Framework/UserManagement/UserManagementModule.cs b/OpenSim/Region/CoreModules/Framework/UserManagement/UserManagementModule.cs index 554af14071..59b3ce8ca7 100644 --- a/OpenSim/Region/CoreModules/Framework/UserManagement/UserManagementModule.cs +++ b/OpenSim/Region/CoreModules/Framework/UserManagement/UserManagementModule.cs @@ -224,6 +224,35 @@ namespace OpenSim.Region.CoreModules.Framework.UserManagement #region IUserManagement + public UUID GetUserIdByName(string name) + { + string[] parts = name.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries); + if (parts.Length < 2) + throw new Exception("Name must have 2 components"); + + return GetUserIdByName(parts[0], parts[1]); + } + + public UUID GetUserIdByName(string firstName, string lastName) + { + // TODO: Optimize for reverse lookup if this gets used by non-console commands. + lock (m_UserCache) + { + foreach (UserData user in m_UserCache.Values) + { + if (user.FirstName == firstName && user.LastName == lastName) + return user.Id; + } + } + + UserAccount account = m_Scenes[0].UserAccountService.GetUserAccount(UUID.Zero, firstName, lastName); + + if (account != null) + return account.PrincipalID; + + return UUID.Zero; + } + public string GetUserName(UUID uuid) { //m_log.DebugFormat("[XXX] GetUserName {0}", uuid); diff --git a/OpenSim/Region/Framework/Interfaces/IUserManagement.cs b/OpenSim/Region/Framework/Interfaces/IUserManagement.cs index bfb8369437..24cd06978e 100644 --- a/OpenSim/Region/Framework/Interfaces/IUserManagement.cs +++ b/OpenSim/Region/Framework/Interfaces/IUserManagement.cs @@ -15,6 +15,21 @@ namespace OpenSim.Region.Framework.Interfaces string GetUserUUI(UUID uuid); string GetUserServerURL(UUID uuid, string serverType); + /// + /// Get user ID by the given name. + /// + /// + /// UUID.Zero if no user with that name is found or if the name is "Unknown User" + UUID GetUserIdByName(string name); + + /// + /// Get user ID by the given name. + /// + /// + /// + /// UUID.Zero if no user with that name is found or if the name is "Unknown User" + UUID GetUserIdByName(string firstName, string lastName); + /// /// Add a user. ///