From 6d595dbccd3ee3a492c648e35c222449ee9c8227 Mon Sep 17 00:00:00 2001 From: UbitUmarov Date: Sat, 29 Jan 2022 16:31:45 +0000 Subject: [PATCH] a few more changes to hg im --- .../InstantMessage/HGMessageTransferModule.cs | 99 +++++----- .../InstantMessage/InstantMessageModule.cs | 87 ++++----- .../InstantMessage/OfflineMessageModule.cs | 7 +- .../HGInstantMessageService.cs | 182 +++++++++--------- 4 files changed, 188 insertions(+), 187 deletions(-) diff --git a/OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs b/OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs index aa77c605e2..857163dd9c 100644 --- a/OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs +++ b/OpenSim/Region/CoreModules/Avatar/InstantMessage/HGMessageTransferModule.cs @@ -75,8 +75,7 @@ namespace OpenSim.Region.CoreModules.Avatar.InstantMessage public virtual void Initialise(IConfigSource config) { IConfig cnf = config.Configs["Messaging"]; - if (cnf != null && cnf.GetString( - "MessageTransferModule", "MessageTransferModule") != Name) + if (cnf != null && cnf.GetString("MessageTransferModule", "MessageTransferModule") != Name) { m_log.Debug("[HG MESSAGE TRANSFER]: Disabled by configuration"); return; @@ -182,48 +181,59 @@ namespace OpenSim.Region.CoreModules.Avatar.InstantMessage } Util.FireAndForget(delegate - { - bool success = false; - if (foreigner && url.Length == 0) // we don't know about this user { - string recipientUUI = TryGetRecipientUUI(new UUID(im.fromAgentID), toAgentID); - //m_log.DebugFormat("[HG MESSAGE TRANSFER]: Got UUI {0}", recipientUUI); - if (recipientUUI != string.Empty) + UUID toDelAgentID = new UUID(im.toAgentID); + bool success = false; + if (foreigner && url.Length == 0) // we don't know about this user { - UUID id; string tourl = string.Empty, first = string.Empty, last = string.Empty, secret = string.Empty; - if (Util.ParseUniversalUserIdentifier(recipientUUI, out id, out tourl, out first, out last, out secret)) + string recipientUUI = TryGetRecipientUUI(new UUID(im.fromAgentID), toDelAgentID); + //m_log.DebugFormat("[HG MESSAGE TRANSFER]: Got UUI {0}", recipientUUI); + if (recipientUUI.Length > 0) { - success = m_IMService.OutgoingInstantMessage(im, tourl, true); - if (success) - UserManagementModule.AddUser(toAgentID, first, last, tourl); - } + if (Util.ParseUniversalUserIdentifier(recipientUUI, out UUID id, out string tourl, + out string first, out string last, out string secret)) + { + success = m_IMService.OutgoingInstantMessage(im, tourl, true); + if (success) + UserManagementModule.AddUser(toDelAgentID, first, last, tourl); + } + } } - } - else - success = m_IMService.OutgoingInstantMessage(im, url, foreigner); + else + success = m_IMService.OutgoingInstantMessage(im, url, foreigner); - if (!success && !foreigner) - HandleUndeliverableMessage(im, result); - else - result(success); - }, null, "HGMessageTransferModule.SendInstantMessage"); - - return; + if (!success && !foreigner) + HandleUndeliverableMessage(im, result); + else + result(success); + }, null, "HGMessageTransferModule.SendInstantMessage"); } protected bool SendIMToScene(GridInstantMessage gim, UUID toAgentID) { bool successful = false; + Scene childScene = null; foreach (Scene scene in m_Scenes) { ScenePresence sp = scene.GetScenePresence(toAgentID); - if(sp != null && !sp.IsChildAgent && !sp.IsDeleted) + if(sp != null && !sp.IsDeleted) { - scene.EventManager.TriggerIncomingInstantMessage(gim); - successful = true; + if(sp.IsChildAgent) + childScene = scene; + else + { + scene.EventManager.TriggerIncomingInstantMessage(gim); + successful = true; + } } } + if(childScene != null) + { + childScene.EventManager.TriggerIncomingInstantMessage(gim); + successful = true; + } + if (!successful) { // If the message can't be delivered to an agent, it @@ -264,29 +274,26 @@ namespace OpenSim.Region.CoreModules.Avatar.InstantMessage { // Let's call back the fromAgent's user agent service // Maybe that service knows about the toAgent - IClientAPI client = LocateClientObject(fromAgent); - if (client != null) + + AgentCircuitData circuit = m_Scenes[0].AuthenticateHandler.GetAgentCircuitData(fromAgent); + if (circuit != null) { - AgentCircuitData circuit = m_Scenes[0].AuthenticateHandler.GetAgentCircuitData(client.AgentId); - if (circuit != null) + if (circuit.ServiceURLs.ContainsKey("HomeURI")) { - if (circuit.ServiceURLs.ContainsKey("HomeURI")) + string uasURL = circuit.ServiceURLs["HomeURI"].ToString(); + m_log.DebugFormat("[HG MESSAGE TRANSFER]: getting UUI of user {0} from {1}", toAgent, uasURL); + UserAgentServiceConnector uasConn = new UserAgentServiceConnector(uasURL); + + string agentUUI = string.Empty; + try { - string uasURL = circuit.ServiceURLs["HomeURI"].ToString(); - m_log.DebugFormat("[HG MESSAGE TRANSFER]: getting UUI of user {0} from {1}", toAgent, uasURL); - UserAgentServiceConnector uasConn = new UserAgentServiceConnector(uasURL); - - string agentUUI = string.Empty; - try - { - agentUUI = uasConn.GetUUI(fromAgent, toAgent); - } - catch (Exception e) { - m_log.Debug("[HG MESSAGE TRANSFER]: GetUUI call failed ", e); - } - - return agentUUI; + agentUUI = uasConn.GetUUI(fromAgent, toAgent); } + catch (Exception e) { + m_log.Debug("[HG MESSAGE TRANSFER]: GetUUI call failed ", e); + } + + return agentUUI; } } diff --git a/OpenSim/Region/CoreModules/Avatar/InstantMessage/InstantMessageModule.cs b/OpenSim/Region/CoreModules/Avatar/InstantMessage/InstantMessageModule.cs index 6fdba0c26b..3a8d3c9802 100644 --- a/OpenSim/Region/CoreModules/Avatar/InstantMessage/InstantMessageModule.cs +++ b/OpenSim/Region/CoreModules/Avatar/InstantMessage/InstantMessageModule.cs @@ -144,69 +144,52 @@ namespace OpenSim.Region.CoreModules.Avatar.InstantMessage */ public virtual void OnInstantMessage(IClientAPI client, GridInstantMessage im) { - byte dialog = im.dialog; - - if (dialog != (byte)InstantMessageDialog.MessageFromAgent - && dialog != (byte)InstantMessageDialog.StartTyping - && dialog != (byte)InstantMessageDialog.StopTyping - && dialog != (byte)InstantMessageDialog.BusyAutoResponse - && dialog != (byte)InstantMessageDialog.MessageFromObject) - { + if (m_TransferModule == null) return; + + switch(im.dialog) + { + case (byte)InstantMessageDialog.MessageFromAgent: + case (byte)InstantMessageDialog.StartTyping: + case (byte)InstantMessageDialog.StopTyping: + case (byte)InstantMessageDialog.BusyAutoResponse: + case (byte)InstantMessageDialog.MessageFromObject: + break; + default: + return; } - //DateTime dt = DateTime.UtcNow; - - // Ticks from UtcNow, but make it look like local. Evil, huh? - //dt = DateTime.SpecifyKind(dt, DateTimeKind.Local); - - //try - //{ - // // Convert that to the PST timezone - // TimeZoneInfo timeZoneInfo = TimeZoneInfo.FindSystemTimeZoneById("America/Los_Angeles"); - // dt = TimeZoneInfo.ConvertTime(dt, timeZoneInfo); - //} - //catch - //{ - // //m_log.Info("[OFFLINE MESSAGING]: No PST timezone found on this machine. Saving with local timestamp."); - //} - - //// And make it look local again to fool the unix time util - //dt = DateTime.SpecifyKind(dt, DateTimeKind.Utc); - - // If client is null, this message comes from storage and IS offline if (client != null) im.offline = 0; if (im.offline == 0) im.timestamp = (uint)Util.UnixTimeSinceEpoch(); - if (m_TransferModule != null) - { - m_TransferModule.SendInstantMessage(im, - delegate(bool success) - { - if (dialog == (uint)InstantMessageDialog.StartTyping || - dialog == (uint)InstantMessageDialog.StopTyping || - dialog == (uint)InstantMessageDialog.MessageFromObject) - { - return; - } + m_TransferModule.SendInstantMessage(im, + delegate(bool success) + { + if(success || client == null) + return; - if ((client != null) && !success) - { - client.SendInstantMessage( - new GridInstantMessage( - null, new UUID(im.fromAgentID), "System", - new UUID(im.toAgentID), - (byte)InstantMessageDialog.BusyAutoResponse, - "Unable to send instant message. "+ - "User is not logged in.", false, - new Vector3())); - } + switch (im.dialog) + { + case (byte)InstantMessageDialog.StartTyping: + case (byte)InstantMessageDialog.StopTyping: + case (byte)InstantMessageDialog.MessageFromObject: + return; + default: + break; } - ); - } + + client.SendInstantMessage(new GridInstantMessage( + null, new UUID(im.fromAgentID), "System", + new UUID(im.toAgentID), + (byte)InstantMessageDialog.BusyAutoResponse, + "Unable to send instant message. User is not logged in.", + false, new Vector3()) + ); + } + ); } /// diff --git a/OpenSim/Region/CoreModules/Avatar/InstantMessage/OfflineMessageModule.cs b/OpenSim/Region/CoreModules/Avatar/InstantMessage/OfflineMessageModule.cs index bb42f9e7c1..8bd459aaaf 100644 --- a/OpenSim/Region/CoreModules/Avatar/InstantMessage/OfflineMessageModule.cs +++ b/OpenSim/Region/CoreModules/Avatar/InstantMessage/OfflineMessageModule.cs @@ -216,11 +216,12 @@ namespace OpenSim.Region.CoreModules.Avatar.InstantMessage // invitations // - im.offline = 1; - - Scene s = FindScene(client.AgentId); + Scene s = client.Scene as Scene; if (s != null) + { + im.offline = 1; s.EventManager.TriggerIncomingInstantMessage(im); + } } } } diff --git a/OpenSim/Services/HypergridService/HGInstantMessageService.cs b/OpenSim/Services/HypergridService/HGInstantMessageService.cs index ca571d81c5..b65f31fc11 100644 --- a/OpenSim/Services/HypergridService/HGInstantMessageService.cs +++ b/OpenSim/Services/HypergridService/HGInstantMessageService.cs @@ -26,18 +26,13 @@ */ using System; -using System.Collections.Generic; -using System.Net; using System.Reflection; using OpenSim.Framework; -using OpenSim.Services.Connectors.Friends; -using OpenSim.Services.Connectors.Hypergrid; using OpenSim.Services.Interfaces; using OpenSim.Services.Connectors.InstantMessage; using GridRegion = OpenSim.Services.Interfaces.GridRegion; using OpenSim.Server.Base; -using FriendInfo = OpenSim.Services.Interfaces.FriendInfo; using OpenMetaverse; using log4net; @@ -46,7 +41,7 @@ using Nini.Config; namespace OpenSim.Services.HypergridService { /// - /// Inter-grid IM + /// HG IM Service /// public class HGInstantMessageService : IInstantMessage { @@ -60,14 +55,14 @@ namespace OpenSim.Services.HypergridService protected static IOfflineIMService m_OfflineIMService; protected static IInstantMessageSimConnector m_IMSimConnector; - protected static readonly ExpiringCacheOS m_UserLocationMap = new ExpiringCacheOS(1000); + protected static readonly ExpiringCacheOS m_UserLocationMap = new ExpiringCacheOS(10000); + protected static readonly ExpiringCacheOS m_RegionsCache = new ExpiringCacheOS(60000); private static bool m_ForwardOfflineGroupMessages; private static bool m_InGatekeeper; private string m_messageKey; - public HGInstantMessageService(IConfigSource config) - : this(config, null) + public HGInstantMessageService(IConfigSource config) : this(config, null) { } @@ -122,14 +117,16 @@ namespace OpenSim.Services.HypergridService m_log.WarnFormat("[HG IM SERVICE]: Unable to load PresenceService"); } - m_InGatekeeper = serverConfig.GetBoolean("InGatekeeper", false); IConfig cnf = config.Configs["Messaging"]; if (cnf == null) + { + m_log.Debug("[HG IM SERVICE]: Starting (without [MEssaging])"); return; + } - m_messageKey = cnf.GetString("MessageKey", String.Empty); + m_messageKey = cnf.GetString("MessageKey", string.Empty); m_ForwardOfflineGroupMessages = cnf.GetBoolean("ForwardOfflineGroupMessages", false); if (m_InGatekeeper) @@ -173,93 +170,96 @@ namespace OpenSim.Services.HypergridService return TrySendInstantMessage(im, url, true, foreigner); } - protected bool TrySendInstantMessage(GridInstantMessage im, string previousLocation, bool firstTime, bool foreigner) + protected bool TrySendInstantMessage(GridInstantMessage im, string foreignerkurl, bool firstTime, bool foreigner) { UUID toAgentID = new UUID(im.toAgentID); - string url = null; - if(firstTime) + + // first try cache + if (m_UserLocationMap.TryGetValue(toAgentID, out url)) { - if(!string.IsNullOrEmpty(previousLocation)) - url = previousLocation; - else - m_UserLocationMap.TryGetValue(toAgentID, out url); + if (ForwardIMToGrid(url, im, toAgentID)) + return true; } - //m_log.DebugFormat("[XXX] Neeed lookup ? {0}", (lookupAgent ? "yes" : "no")); - // Are we needing to look-up an agent? - if (string.IsNullOrEmpty(url)) + // try the provided url (for a foreigner) + if(!string.IsNullOrEmpty(foreignerkurl)) { - PresenceInfo[] presences = m_PresenceService.GetAgents(new string[] { toAgentID.ToString() }); - if (presences != null && presences.Length > 0) + if (ForwardIMToGrid(foreignerkurl, im, toAgentID)) + return true; + } + + //try to find it in local grid + PresenceInfo[] presences = m_PresenceService.GetAgents(new string[] { toAgentID.ToString() }); + if (presences != null && presences.Length > 0) + { + foreach (PresenceInfo p in presences) { - foreach (PresenceInfo p in presences) + if (!p.RegionID.IsZero()) { - if (!p.RegionID.IsZero()) + //m_log.DebugFormat("[XXX]: Found presence in {0}", p.RegionID); + // stupid service does not cache region, even in region code + if(m_RegionsCache.TryGetValue(p.RegionID, out url)) + break; + + GridRegion reginfo = m_GridService.GetRegionByUUID(UUID.Zero, p.RegionID); + if (reginfo != null) { - //m_log.DebugFormat("[XXX]: Found presence in {0}", p.RegionID); - GridRegion reginfo = m_GridService.GetRegionByUUID(UUID.Zero, p.RegionID); - if (reginfo != null) - { - url = reginfo.ServerURI; - break; - } + url = reginfo.ServerURI; + m_RegionsCache.AddOrUpdate(p.RegionID, url, 300); + break; } } } - - if (!foreigner && string.IsNullOrEmpty(url) && m_UserAgentService != null) - { - // Let's check with the UAS if the user is elsewhere - m_log.DebugFormat("[HG IM SERVICE]: User is not present. Checking location with User Agent service"); - try - { - url = m_UserAgentService.LocateUser(toAgentID); - } - catch (Exception e) - { - m_log.Warn("[HG IM SERVICE]: LocateUser call failed ", e); - url = string.Empty; - } - } - - // check if we've tried this before.. - // This is one way to end the recursive loop - if (!firstTime && previousLocation.Equals(url)) - { - // m_log.Error("[GRID INSTANT MESSAGE]: Unable to deliver an instant message"); - m_log.DebugFormat("[HG IM SERVICE]: Fail 2 {0} {1}", previousLocation, url); - return false; - } } - if (!string.IsNullOrEmpty(url)) + if (string.IsNullOrEmpty(url) && !foreigner && m_UserAgentService != null) { - // ok, the user is around somewhere. Let's send back the reply with "success" - // even though the IM may still fail. Just don't keep the caller waiting for - // the entire time we're trying to deliver the IM - return ForwardIMToGrid(url, im, toAgentID, foreigner); + // Let's check with the UAS if the user is elsewhere in HG + m_log.DebugFormat("[HG IM SERVICE]: User is not present. Checking location with User Agent service"); + try + { + url = m_UserAgentService.LocateUser(toAgentID); + } + catch (Exception e) + { + m_log.Warn("[HG IM SERVICE]: LocateUser call failed ", e); + url = string.Empty; + } } - m_log.DebugFormat("[HG IM SERVICE]: Unable to locate user {0}", toAgentID); - return false; + if (string.IsNullOrEmpty(url)) + { + m_log.DebugFormat("[HG IM SERVICE]: Unable to locate user {0}", toAgentID); + return false; + } + + // check if we've tried this before.. + if (!string.IsNullOrEmpty(foreignerkurl) && url.Equals(foreignerkurl, StringComparison.InvariantCultureIgnoreCase)) + { + // m_log.Error("[GRID INSTANT MESSAGE]: Unable to deliver an instant message"); + m_log.DebugFormat("[HG IM SERVICE]: Unable to send to user {0}, at {1}", toAgentID, foreignerkurl); + return false; + } + + // ok, the user is around somewhere. Let's send back the reply with "success" + // even though the IM may still fail. Just don't keep the caller waiting for + // the entire time we're trying to deliver the IM + return ForwardIMToGrid(url, im, toAgentID); } - bool ForwardIMToGrid(string url, GridInstantMessage im, UUID toAgentID, bool foreigner) + bool ForwardIMToGrid(string url, GridInstantMessage im, UUID toAgentID) { if (InstantMessageServiceConnector.SendInstantMessage(url, im, m_messageKey)) { // IM delivery successful, so store the Agent's location in our local cache. - m_UserLocationMap.AddOrUpdate(toAgentID, url, 30); + m_UserLocationMap.AddOrUpdate(toAgentID, url, 120); return true; } else - { - // try again, but lookup user this time. m_UserLocationMap.Remove(toAgentID); - // This is recursive!!!!! - return TrySendInstantMessage(im, "", false, foreigner); - } + + return false; } private bool UndeliveredMessage(GridInstantMessage im) @@ -267,25 +267,35 @@ namespace OpenSim.Services.HypergridService if (m_OfflineIMService == null) return false; - if (im.dialog != (byte)InstantMessageDialog.MessageFromObject && - im.dialog != (byte)InstantMessageDialog.MessageFromAgent && - im.dialog != (byte)InstantMessageDialog.GroupNotice && - im.dialog != (byte)InstantMessageDialog.GroupInvitation && - im.dialog != (byte)InstantMessageDialog.InventoryOffered) + if (m_ForwardOfflineGroupMessages) { - return false; + switch (im.dialog) + { + case (byte)InstantMessageDialog.MessageFromObject: + case (byte)InstantMessageDialog.MessageFromAgent: + case (byte)InstantMessageDialog.GroupNotice: + case (byte)InstantMessageDialog.GroupInvitation: + case (byte)InstantMessageDialog.InventoryOffered: + break; + default: + return false; + } + } + else + { + switch (im.dialog) + { + case (byte)InstantMessageDialog.MessageFromObject: + case (byte)InstantMessageDialog.MessageFromAgent: + case (byte)InstantMessageDialog.InventoryOffered: + break; + default: + return false; + } } - if (!m_ForwardOfflineGroupMessages) - { - if (im.dialog == (byte)InstantMessageDialog.GroupNotice || - im.dialog == (byte)InstantMessageDialog.GroupInvitation) - return false; - } - -// m_log.DebugFormat("[HG IM SERVICE]: Message saved"); - string reason = string.Empty; - return m_OfflineIMService.StoreMessage(im, out reason); + //m_log.DebugFormat("[HG IM SERVICE]: Message saved"); + return m_OfflineIMService.StoreMessage(im, out string reason); } } }