From e0fc854f05b137c353196356e5b26d11b6ee6867 Mon Sep 17 00:00:00 2001 From: Melanie Date: Mon, 28 Dec 2009 23:42:08 +0000 Subject: [PATCH] Adding new fields and home location methid to presence. Adding cleanup (deleting all but one presence record) on logout so that they don't pile up. --- OpenSim/Data/IPresenceData.cs | 3 ++ .../Data/MySQL/MySQLGenericTableHandler.cs | 2 + OpenSim/Data/MySQL/MySQLPresenceData.cs | 52 +++++++++++++++++++ .../Presence/LocalPresenceServiceConnector.cs | 5 ++ .../RemotePresenceServiceConnector.cs | 5 ++ .../Presence/PresenceServerPostHandler.cs | 29 +++++++++++ .../Presence/PresenceServiceConnector.cs | 46 ++++++++++++++++ .../Services/Interfaces/IPresenceService.cs | 4 ++ .../PresenceService/PresenceService.cs | 34 ++++++++++-- 9 files changed, 176 insertions(+), 4 deletions(-) diff --git a/OpenSim/Data/IPresenceData.cs b/OpenSim/Data/IPresenceData.cs index 98353ed2b1..20eb7f640e 100644 --- a/OpenSim/Data/IPresenceData.cs +++ b/OpenSim/Data/IPresenceData.cs @@ -51,6 +51,9 @@ namespace OpenSim.Data PresenceData Get(UUID sessionID); void LogoutRegionAgents(UUID regionID); bool ReportAgent(UUID sessionID, UUID regionID, string position, string lookAt); + bool SetHomeLocation(string userID, UUID regionID, Vector3 position, Vector3 lookAt); PresenceData[] Get(string field, string data); + void Prune(string userID); + bool Delete(string field, string val); } } diff --git a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs index b2bd5f60c8..873d6d474d 100644 --- a/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs +++ b/OpenSim/Data/MySQL/MySQLGenericTableHandler.cs @@ -177,6 +177,8 @@ namespace OpenSim.Data.MySQL result.Add(row); } + reader.Close(); + CloseReaderCommand(cmd); return result.ToArray(); diff --git a/OpenSim/Data/MySQL/MySQLPresenceData.cs b/OpenSim/Data/MySQL/MySQLPresenceData.cs index 8ccad9045d..72b8a0cd73 100644 --- a/OpenSim/Data/MySQL/MySQLPresenceData.cs +++ b/OpenSim/Data/MySQL/MySQLPresenceData.cs @@ -93,5 +93,57 @@ namespace OpenSim.Data.MySQL return true; } + + public bool SetHomeLocation(string userID, UUID regionID, Vector3 position, Vector3 lookAt) + { + PresenceData[] pd = Get("UserID", userID); + if (pd.Length == 0) + return false; + + MySqlCommand cmd = new MySqlCommand(); + + cmd.CommandText = String.Format("update {0} set HomeRegionID=?HomeRegionID, HomePosition=?HomePosition, HomeLookAt=?HomeLookAt where UserID=?UserID", m_Realm); + + cmd.Parameters.AddWithValue("?UserID", userID); + cmd.Parameters.AddWithValue("?HomeRegionID", regionID.ToString()); + cmd.Parameters.AddWithValue("?HomePosition", position); + cmd.Parameters.AddWithValue("?HomeLookAt", lookAt); + + if (ExecuteNonQuery(cmd) == 0) + return false; + + return true; + } + + public void Prune(string userID) + { + MySqlCommand cmd = new MySqlCommand(); + + cmd.CommandText = String.Format("select * from {0} where UserID=?UserID", m_Realm); + + cmd.Parameters.AddWithValue("?UserID", userID); + + IDataReader reader = ExecuteReader(cmd); + + List deleteSessions = new List(); + int online = 0; + + while(reader.Read()) + { + if (bool.Parse(reader["Online"].ToString())) + online++; + else + deleteSessions.Add(new UUID(reader["SessionID"].ToString())); + } + + if (online == 0 && deleteSessions.Count > 0) + deleteSessions.RemoveAt(0); + + reader.Close(); + CloseReaderCommand(cmd); + + foreach (UUID s in deleteSessions) + Delete("SessionID", s.ToString()); + } } } diff --git a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Presence/LocalPresenceServiceConnector.cs b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Presence/LocalPresenceServiceConnector.cs index a80a355a14..644d7558bf 100644 --- a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Presence/LocalPresenceServiceConnector.cs +++ b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Presence/LocalPresenceServiceConnector.cs @@ -180,6 +180,11 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Presence return m_PresenceService.GetAgents(userIDs); } + public bool SetHomeLocation(string userID, UUID regionID, Vector3 position, Vector3 lookAt) + { + return m_PresenceService.SetHomeLocation(userID, regionID, position, lookAt); + } + #endregion } diff --git a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Presence/RemotePresenceServiceConnector.cs b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Presence/RemotePresenceServiceConnector.cs index e652773464..e8e140a0ae 100644 --- a/OpenSim/Region/CoreModules/ServiceConnectorsOut/Presence/RemotePresenceServiceConnector.cs +++ b/OpenSim/Region/CoreModules/ServiceConnectorsOut/Presence/RemotePresenceServiceConnector.cs @@ -153,6 +153,11 @@ namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Presence return m_RemoteConnector.GetAgents(userIDs); } + public bool SetHomeLocation(string userID, UUID regionID, Vector3 position, Vector3 lookAt) + { + return m_RemoteConnector.SetHomeLocation(userID, regionID, position, lookAt); + } + #endregion } diff --git a/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs b/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs index 580cb15ded..bb00a00d1d 100644 --- a/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs +++ b/OpenSim/Server/Handlers/Presence/PresenceServerPostHandler.cs @@ -90,6 +90,8 @@ namespace OpenSim.Server.Handlers.Presence return GetAgent(request); case "getagents": return GetAgents(request); + case "sethome": + return SetHome(request); } m_log.DebugFormat("[PRESENCE HANDLER]: unknown method request: {0}", method); } @@ -303,5 +305,32 @@ namespace OpenSim.Server.Handlers.Presence return ms.ToArray(); } + + byte[] SetHome(Dictionary request) + { + UUID region = UUID.Zero; + Vector3 position = new Vector3(128, 128, 70); + Vector3 look = Vector3.Zero; + + if (!request.ContainsKey("SessionID") || !request.ContainsKey("RegionID")) + return FailureResult(); + + string user = request["UserID"].ToString(); + + if (!UUID.TryParse(request["RegionID"].ToString(), out region)) + return FailureResult(); + + if (request.ContainsKey("position")) + Vector3.TryParse(request["position"].ToString(), out position); + + if (request.ContainsKey("lookAt")) + Vector3.TryParse(request["lookAt"].ToString(), out look); + + if (m_PresenceService.SetHomeLocation(user, region, position, look)) + return SuccessResult(); + + return FailureResult(); + } + } } diff --git a/OpenSim/Services/Connectors/Presence/PresenceServiceConnector.cs b/OpenSim/Services/Connectors/Presence/PresenceServiceConnector.cs index 4ad457f233..482862dcd8 100644 --- a/OpenSim/Services/Connectors/Presence/PresenceServiceConnector.cs +++ b/OpenSim/Services/Connectors/Presence/PresenceServiceConnector.cs @@ -371,6 +371,52 @@ namespace OpenSim.Services.Connectors } + public bool SetHomeLocation(string userID, UUID regionID, Vector3 position, Vector3 lookAt) + { + Dictionary sendData = new Dictionary(); + //sendData["SCOPEID"] = scopeID.ToString(); + sendData["VERSIONMIN"] = ProtocolVersions.ClientProtocolVersionMin.ToString(); + sendData["VERSIONMAX"] = ProtocolVersions.ClientProtocolVersionMax.ToString(); + sendData["METHOD"] = "sethome"; + + sendData["UserID"] = userID; + sendData["RegionID"] = regionID.ToString(); + sendData["position"] = position.ToString(); + sendData["lookAt"] = lookAt.ToString(); + + string reqString = ServerUtils.BuildQueryString(sendData); + // m_log.DebugFormat("[PRESENCE CONNECTOR]: queryString = {0}", reqString); + try + { + string reply = SynchronousRestFormsRequester.MakeRequest("POST", + m_ServerURI + "/presence", + reqString); + if (reply != string.Empty) + { + Dictionary replyData = ServerUtils.ParseXmlResponse(reply); + + if (replyData.ContainsKey("Result")) + { + if (replyData["Result"].ToString().ToLower() == "success") + return true; + else + return false; + } + else + m_log.DebugFormat("[PRESENCE CONNECTOR]: SetHomeLocation reply data does not contain result field"); + + } + else + m_log.DebugFormat("[PRESENCE CONNECTOR]: SetHomeLocation received empty reply"); + } + catch (Exception e) + { + m_log.DebugFormat("[PRESENCE CONNECTOR]: Exception when contacting presence server: {0}", e.Message); + } + + return false; + } + #endregion } diff --git a/OpenSim/Services/Interfaces/IPresenceService.cs b/OpenSim/Services/Interfaces/IPresenceService.cs index de3813a4f4..258264807c 100644 --- a/OpenSim/Services/Interfaces/IPresenceService.cs +++ b/OpenSim/Services/Interfaces/IPresenceService.cs @@ -41,6 +41,9 @@ namespace OpenSim.Services.Interfaces public DateTime Logout; public Vector3 Position; public Vector3 LookAt; + public UUID HomeRegionID; + public Vector3 HomePosition; + public Vector3 HomeLookAt; public PresenceInfo() { @@ -87,6 +90,7 @@ namespace OpenSim.Services.Interfaces bool LogoutRegionAgents(UUID regionID); bool ReportAgent(UUID sessionID, UUID regionID, Vector3 position, Vector3 lookAt); + bool SetHomeLocation(string userID, UUID regionID, Vector3 position, Vector3 lookAt); PresenceInfo GetAgent(UUID sessionID); PresenceInfo[] GetAgents(string[] userIDs); diff --git a/OpenSim/Services/PresenceService/PresenceService.cs b/OpenSim/Services/PresenceService/PresenceService.cs index adbf430a04..b3d819463e 100644 --- a/OpenSim/Services/PresenceService/PresenceService.cs +++ b/OpenSim/Services/PresenceService/PresenceService.cs @@ -54,9 +54,10 @@ namespace OpenSim.Services.PresenceService public bool LoginAgent(string userID, UUID sessionID, UUID secureSessionID) { - // We have just logged in. If there is any info in the table - // it's OK to overwrite. So we won't bother reading it first - // + m_Database.Prune(userID); + + PresenceData[] d = m_Database.Get("UserID", userID); + PresenceData data = new PresenceData(); data.UserID = userID; @@ -64,6 +65,12 @@ namespace OpenSim.Services.PresenceService data.SessionID = sessionID; data.Data["SecureSessionID"] = secureSessionID.ToString(); data.Data["Login"] = Util.UnixTimeSinceEpoch().ToString(); + if (d.Length > 0) + { + data.Data["HomeRegionID"] = d[0].Data["HomeRegionID"]; + data.Data["HomePosition"] = d[0].Data["HomePosition"]; + data.Data["HomeLookAt"] = d[0].Data["HomeLookAt"]; + } m_Database.Store(data); @@ -76,12 +83,20 @@ namespace OpenSim.Services.PresenceService if (data == null) return false; + PresenceData[] d = m_Database.Get("UserID", data.UserID); + + if (d.Length > 1) + { + m_Database.Delete("SessionID", sessionID.ToString()); + return true; + } + data.Data["Online"] = "false"; data.Data["Logout"] = Util.UnixTimeSinceEpoch().ToString(); m_Database.Store(data); - return false; + return true; } public bool LogoutRegionAgents(UUID regionID) @@ -114,6 +129,9 @@ namespace OpenSim.Services.PresenceService ret.Logout = Util.ToDateTime(Convert.ToInt32(data.Data["Logout"])); ret.Position = Vector3.Parse(data.Data["Position"]); ret.LookAt = Vector3.Parse(data.Data["LookAt"]); + ret.HomeRegionID = new UUID(data.Data["HomeRegionID"]); + ret.HomePosition = Vector3.Parse(data.Data["HomePosition"]); + ret.HomeLookAt = Vector3.Parse(data.Data["HomeLookAt"]); return ret; } @@ -140,6 +158,9 @@ namespace OpenSim.Services.PresenceService d.Data["Logout"])); ret.Position = Vector3.Parse(d.Data["Position"]); ret.LookAt = Vector3.Parse(d.Data["LookAt"]); + ret.HomeRegionID = new UUID(d.Data["HomeRegionID"]); + ret.HomePosition = Vector3.Parse(d.Data["HomePosition"]); + ret.HomeLookAt = Vector3.Parse(d.Data["HomeLookAt"]); info.Add(ret); } @@ -147,5 +168,10 @@ namespace OpenSim.Services.PresenceService return info.ToArray(); } + + public bool SetHomeLocation(string userID, UUID regionID, Vector3 position, Vector3 lookAt) + { + return m_Database.SetHomeLocation(userID, regionID, position, lookAt); + } } }