From e0b821f8753c6d412f341fe109b8f83df14662a5 Mon Sep 17 00:00:00 2001 From: Teravus Ovares Date: Sun, 1 Jun 2008 14:13:29 +0000 Subject: * This enables grid-wide instant messaging in a peer to peer with tracker style way over XMLRPC. * Friend status updates are still only local, so you still won't know before instant messaging someone if they're online. * The server each user is on and the user server must be updated or the instant message won't get to the destination. --- .../Avatar/InstantMessage/InstantMessageModule.cs | 190 ++++++++++++++++++--- 1 file changed, 167 insertions(+), 23 deletions(-) (limited to 'OpenSim/Region/Environment/Modules/Avatar/InstantMessage/InstantMessageModule.cs') diff --git a/OpenSim/Region/Environment/Modules/Avatar/InstantMessage/InstantMessageModule.cs b/OpenSim/Region/Environment/Modules/Avatar/InstantMessage/InstantMessageModule.cs index fdded90..36184d2 100644 --- a/OpenSim/Region/Environment/Modules/Avatar/InstantMessage/InstantMessageModule.cs +++ b/OpenSim/Region/Environment/Modules/Avatar/InstantMessage/InstantMessageModule.cs @@ -161,11 +161,24 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage if (gridmode) { // Still here, try send via Grid - // TODO + + // don't send session drop yet, as it's not reliable somehow. + if (dialog != (byte)InstantMessageDialog.SessionDrop) + { + SendGridInstantMessageViaXMLRPC(client, fromAgentID, + fromAgentSession, toAgentID, + imSessionID, timestamp, fromAgentName, + message, dialog, fromGroup, offline, + ParentEstateID, Position, RegionID, + binaryBucket, getLocalRegionHandleFromUUID(RegionID), 0); + } + } + else + { if (client != null) { if (dialog != (byte)InstantMessageDialog.StartTyping && dialog != (byte)InstantMessageDialog.StopTyping && dialog != (byte)InstantMessageDialog.SessionDrop) - client.SendInstantMessage(toAgentID, fromAgentSession, "Unable to send instant message", fromAgentID, imSessionID, "System", (byte)InstantMessageDialog.BusyAutoResponse, (uint)Util.UnixTimeSinceEpoch());// SendAlertMessage("Unable to send instant message"); + client.SendInstantMessage(toAgentID, fromAgentSession, "Unable to send instant message. User is not logged in.", fromAgentID, imSessionID, "System", (byte)InstantMessageDialog.BusyAutoResponse, (uint)Util.UnixTimeSinceEpoch());// SendAlertMessage("Unable to send instant message"); } } } @@ -175,7 +188,10 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage // Trusty OSG1 called method. This method also gets called from the FriendsModule // Turns out the sim has to send an instant message to the user to get it to show an accepted friend. - + /// + /// + /// + /// private void OnGridInstantMessage(GridInstantMessage msg) { // Trigger the above event handler @@ -185,6 +201,15 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage new LLVector3(msg.Position.x, msg.Position.y, msg.Position.z), new LLUUID(msg.RegionID), msg.binaryBucket); } + + + /// + /// Process a XMLRPC Grid Instant Message + /// + /// XMLRPC parameters from_agent_id from_agent_session to_agent_id im_session_id timestamp + /// from_agent_name message dialog from_group offline parent_estate_id position_x position_y position_z region_id + /// binary_bucket region_handle + /// Nothing much protected virtual XmlRpcResponse processXMLRPCGridInstantMessage(XmlRpcRequest request) { bool successful = false; @@ -207,11 +232,11 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage float pos_x = 0; float pos_y = 0; float pos_z = 0; - + //m_log.Info("Processing IM"); Hashtable requestData = (Hashtable)request.Params[0]; - + // Check if it's got all the data if (requestData.ContainsKey("from_agent_id") && requestData.ContainsKey("from_agent_session") && requestData.ContainsKey("to_agent_id") && requestData.ContainsKey("im_session_id") && requestData.ContainsKey("timestamp") && requestData.ContainsKey("from_agent_name") @@ -222,6 +247,7 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage && requestData.ContainsKey("position_z") && requestData.ContainsKey("region_id") && requestData.ContainsKey("binary_bucket") && requestData.ContainsKey("region_handle")) { + // Do the easy way of validating the UUIDs Helpers.TryParse((string)requestData["from_agent_id"], out fromAgentID); Helpers.TryParse((string)requestData["from_agent_session"], out fromAgentSession); Helpers.TryParse((string)requestData["to_agent_id"], out toAgentID); @@ -246,12 +272,16 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage fromAgentName = (string)requestData["from_agent_name"]; message = (string)requestData["message"]; - dialog = (byte)requestData["dialog"]; + + // Bytes don't transfer well over XMLRPC, so, we Base64 Encode them. + byte[] dialogdata = Convert.FromBase64String((string)requestData["dialog"]); + dialog = dialogdata[0]; if ((string)requestData["from_group"] == "TRUE") fromGroup = true; - offline = (byte)requestData["offline"]; + byte[] offlinedata = Convert.FromBase64String((string)requestData["offline"]); + offline = offlinedata[0]; # region ParentEstateID try @@ -316,7 +346,9 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage # endregion Position = new LLVector3(pos_x, pos_y, pos_z); - binaryBucket = (byte[])requestData["binary_bucket"]; + binaryBucket = Convert.FromBase64String((string)requestData["binary_bucket"]); + + // Create a New GridInstantMessageObject the the data GridInstantMessage gim = new GridInstantMessage(); gim.fromAgentID = fromAgentID.UUID; gim.fromAgentName = fromAgentName; @@ -334,6 +366,7 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage gim.binaryBucket = binaryBucket; + // Trigger the Instant message in the scene. foreach (Scene scene in m_scenes) { if (scene.Entities.ContainsKey(toAgentID) && scene.Entities[toAgentID] is ScenePresence) @@ -343,13 +376,16 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage if (!user.IsChildAgent) { scene.EventManager.TriggerGridInstantMessage(gim, InstantMessageReceiver.FriendsModule | InstantMessageReceiver.GroupsModule | InstantMessageReceiver.IMModule); + successful = true; } } } - OnGridInstantMessage(gim); - successful = true; + //OnGridInstantMessage(gim); + } + //Send response back to region calling if it was successful + // calling region uses this to know when to look up a user's location again. XmlRpcResponse resp = new XmlRpcResponse(); Hashtable respdata = new Hashtable(); if (successful) @@ -361,6 +397,41 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage return resp; } + #region Asynchronous setup + /// + /// delegate for sending a grid instant message asynchronously + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + public delegate void GridInstantMessageDelegate(IClientAPI client, LLUUID fromAgentID, + LLUUID fromAgentSession, LLUUID toAgentID, + LLUUID imSessionID, uint timestamp, string fromAgentName, + string message, byte dialog, bool fromGroup, byte offline, + uint ParentEstateID, LLVector3 Position, LLUUID RegionID, + byte[] binaryBucket, ulong regionhandle, ulong prevRegionHandle); + + private void GridInstantMessageCompleted(IAsyncResult iar) + { + GridInstantMessageDelegate icon = (GridInstantMessageDelegate)iar.AsyncState; + icon.EndInvoke(iar); + } + + protected virtual void SendGridInstantMessageViaXMLRPC(IClientAPI client, LLUUID fromAgentID, LLUUID fromAgentSession, LLUUID toAgentID, LLUUID imSessionID, uint timestamp, string fromAgentName, @@ -368,6 +439,33 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage uint ParentEstateID, LLVector3 Position, LLUUID RegionID, byte[] binaryBucket, ulong regionhandle, ulong prevRegionHandle) { + GridInstantMessageDelegate d = SendGridInstantMessageViaXMLRPCAsync; + + d.BeginInvoke(client,fromAgentID, + fromAgentSession,toAgentID, + imSessionID,timestamp, fromAgentName, + message, dialog, fromGroup, offline, + ParentEstateID, Position, RegionID, + binaryBucket, regionhandle, prevRegionHandle, + GridInstantMessageCompleted, + d); + } + + #endregion + + + /// + /// Recursive SendGridInstantMessage over XMLRPC method. The prevRegionHandle contains the last regionhandle tried + /// if it's the same as the user's looked up region handle, then we end the recursive loop + /// + /// + protected virtual void SendGridInstantMessageViaXMLRPCAsync(IClientAPI client, LLUUID fromAgentID, + LLUUID fromAgentSession, LLUUID toAgentID, + LLUUID imSessionID, uint timestamp, string fromAgentName, + string message, byte dialog, bool fromGroup, byte offline, + uint ParentEstateID, LLVector3 Position, LLUUID RegionID, + byte[] binaryBucket, ulong regionhandle, ulong prevRegionHandle) + { UserAgentData upd = null; bool lookupAgent = false; @@ -389,16 +487,34 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage } } + // Are we needing to look-up an agent? if (lookupAgent) { + // Non-cached user agent lookup. upd = m_scenes[0].CommsManager.UserService.GetAgentByUUID(toAgentID); - // check if we've tried this before.. - if (upd.Handle == prevRegionHandle) + if (upd != null) + { + // check if we've tried this before.. This is one way to end the recursive loop + if (upd.Handle == prevRegionHandle) + { + m_log.Error("[GRID INSTANT MESSAGE]: Unable to deliver an instant message"); + if (client != null) + { + if (dialog != (byte)InstantMessageDialog.StartTyping && dialog != (byte)InstantMessageDialog.StopTyping && dialog != (byte)InstantMessageDialog.SessionDrop) + client.SendInstantMessage(toAgentID, fromAgentSession, "Unable to send instant message", fromAgentID, imSessionID, "System", (byte)InstantMessageDialog.BusyAutoResponse, (uint)Util.UnixTimeSinceEpoch());// SendAlertMessage("Unable to send instant message"); + } + return; + } + } + else { m_log.Error("[GRID INSTANT MESSAGE]: Unable to deliver an instant message"); if (client != null) - client.SendInstantMessage(toAgentID, fromAgentSession, "Unable to send instant message", fromAgentID, imSessionID, "System", (byte)InstantMessageDialog.MessageFromObject,(uint)Util.UnixTimeSinceEpoch());// SendAlertMessage("Unable to send instant message"); + { + if (dialog != (byte)InstantMessageDialog.StartTyping && dialog != (byte)InstantMessageDialog.StopTyping && dialog != (byte)InstantMessageDialog.SessionDrop) + client.SendInstantMessage(toAgentID, fromAgentSession, "Unable to send instant message", fromAgentID, imSessionID, "System", (byte)InstantMessageDialog.BusyAutoResponse, (uint)Util.UnixTimeSinceEpoch());// SendAlertMessage("Unable to send instant message"); + } return; } } @@ -431,6 +547,7 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage bool imresult = doIMSending(reginfo, msgdata); if (imresult) { + // IM delivery successful, so store the Agent's location in our local cache. lock (m_userRegionMap) { if (m_userRegionMap.ContainsKey(toAgentID)) @@ -442,12 +559,18 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage m_userRegionMap.Add(toAgentID, upd.Handle); } } - m_log.Info("[GRID INSTANT MESSAGE]: Successfully sent a message"); + //m_log.Info("[GRID INSTANT MESSAGE]: Successfully sent a message"); } else { // try again, but lookup user this time. - SendGridInstantMessageViaXMLRPC(client, fromAgentID, + // Warning, this must call the Async version + // of this method or we'll be making thousands of threads + // The version within the spawned thread is SendGridInstantMessageViaXMLRPCAsync + // The version that spawns the thread is SendGridInstantMessageViaXMLRPC + + // This is recursive!!!!! + SendGridInstantMessageViaXMLRPCAsync(client, fromAgentID, fromAgentSession, toAgentID, imSessionID, timestamp, fromAgentName, message, dialog, fromGroup, offline, @@ -461,18 +584,27 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage { // send Agent Offline message if (client != null) - client.SendInstantMessage(toAgentID, fromAgentSession, "Unable to send instant message", fromAgentID, imSessionID, "System", (byte)InstantMessageDialog.MessageFromObject, (uint)Util.UnixTimeSinceEpoch());// SendAlertMessage("Unable to send instant message"); + { + if (dialog != (byte)InstantMessageDialog.StartTyping && dialog != (byte)InstantMessageDialog.StopTyping && dialog != (byte)InstantMessageDialog.SessionDrop) + client.SendInstantMessage(toAgentID, fromAgentSession, "Unable to send instant message: Agent Offline", fromAgentID, imSessionID, "System", (byte)InstantMessageDialog.BusyAutoResponse, (uint)Util.UnixTimeSinceEpoch());// SendAlertMessage("Unable to send instant message"); + } } } else { - // send Agent Offline message + // send Agent doesn't exist message if (client != null) - client.SendInstantMessage(toAgentID, fromAgentSession, "Unable to send instant message", fromAgentID, imSessionID, "System", (byte)InstantMessageDialog.MessageFromObject, (uint)Util.UnixTimeSinceEpoch());// SendAlertMessage("Unable to send instant message"); + client.SendInstantMessage(toAgentID, fromAgentSession, "Unable to send instant message: Are you sure this agent exists anymore?", fromAgentID, imSessionID, "System", (byte)InstantMessageDialog.MessageFromObject, (uint)Util.UnixTimeSinceEpoch());// SendAlertMessage("Unable to send instant message"); } } + /// + /// This actually does the XMLRPC Request + /// + /// RegionInfo we pull the data out of to send the request to + /// The Instant Message data Hashtable + /// Bool if the message was successfully delivered at the other side. private bool doIMSending(RegionInfo reginfo, Hashtable xmlrpcdata) { @@ -502,7 +634,7 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage return false; } } - catch (WebException) + catch (WebException e) { m_log.ErrorFormat("[GRID INSTANT MESSAGE]: Error sending message to {0} the host didn't respond", "http://" + reginfo.ExternalHostName + ":" + reginfo.HttpPort); } @@ -510,6 +642,12 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage return false; } + /// + /// Get ulong region handle for region by it's Region UUID. + /// We use region handles over grid comms because there's all sorts of free and cool caching. + /// + /// UUID of region to get the region handle for + /// private ulong getLocalRegionHandleFromUUID(LLUUID regionID) { ulong returnhandle = 0; @@ -528,6 +666,11 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage return returnhandle; } + /// + /// Takes a GridInstantMessage and converts it into a Hashtable for XMLRPC + /// + /// The GridInstantMessage object + /// Hashtable containing the XMLRPC request private Hashtable ConvertGridInstantMessageToXMLRPC(GridInstantMessage msg) { Hashtable gim = new Hashtable(); @@ -538,20 +681,21 @@ namespace OpenSim.Region.Environment.Modules.Avatar.InstantMessage gim["timestamp"] = msg.timestamp.ToString(); gim["from_agent_name"] = msg.fromAgentName; gim["message"] = msg.message; - gim["dialog"] = msg.dialog; + byte[] dialogdata = new byte[1];dialogdata[0] = msg.dialog; + gim["dialog"] = Convert.ToBase64String(dialogdata,Base64FormattingOptions.None); if (msg.fromGroup) gim["from_group"] = "TRUE"; else gim["from_group"] = "FALSE"; - - gim["offline"] = msg.offline; + byte[] offlinedata = new byte[1]; offlinedata[0] = msg.offline; + gim["offline"] = Convert.ToBase64String(offlinedata, Base64FormattingOptions.None); gim["parent_estate_id"] = msg.ParentEstateID.ToString(); gim["position_x"] = msg.Position.x.ToString(); gim["position_y"] = msg.Position.y.ToString(); gim["position_z"] = msg.Position.z.ToString(); gim["region_id"] = msg.RegionID.ToString(); - gim["binary_bucket"] = msg.binaryBucket; + gim["binary_bucket"] = Convert.ToBase64String(msg.binaryBucket,Base64FormattingOptions.None); return gim; } -- cgit v1.1