PresenceService.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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;
  28. using System.Collections.Generic;
  29. using System.Net;
  30. using System.Reflection;
  31. using Nini.Config;
  32. using log4net;
  33. using OpenSim.Framework;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Data;
  36. using OpenSim.Services.Interfaces;
  37. using OpenMetaverse;
  38. namespace OpenSim.Services.PresenceService
  39. {
  40. public class PresenceService : PresenceServiceBase, IPresenceService
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. protected bool m_allowDuplicatePresences = false;
  44. public PresenceService(IConfigSource config)
  45. : base(config)
  46. {
  47. m_log.Debug("[PRESENCE SERVICE]: Starting presence service");
  48. IConfig presenceConfig = config.Configs["PresenceService"];
  49. if (presenceConfig != null)
  50. {
  51. m_allowDuplicatePresences = presenceConfig.GetBoolean("AllowDuplicatePresences", m_allowDuplicatePresences);
  52. }
  53. }
  54. public bool LoginAgent(string userID, UUID sessionID, UUID secureSessionID)
  55. {
  56. PresenceData prevUser = GetUser(userID);
  57. if (!m_allowDuplicatePresences && (prevUser != null))
  58. m_Database.Delete("UserID", userID.ToString());
  59. PresenceData data = new PresenceData();
  60. data.UserID = userID;
  61. data.RegionID = UUID.Zero;
  62. data.SessionID = sessionID;
  63. data.Data = new Dictionary<string, string>();
  64. data.Data["SecureSessionID"] = secureSessionID.ToString();
  65. m_Database.Store(data);
  66. string prevUserStr = "";
  67. if (prevUser != null)
  68. prevUserStr = string.Format(". This user was already logged-in: session {0}, region {1}", prevUser.SessionID, prevUser.RegionID);
  69. m_log.DebugFormat("[PRESENCE SERVICE]: LoginAgent: session {0}, user {1}, region {2}, secure session {3}{4}",
  70. data.SessionID, data.UserID, data.RegionID, secureSessionID, prevUserStr);
  71. return true;
  72. }
  73. public bool LogoutAgent(UUID sessionID)
  74. {
  75. PresenceInfo presence = GetAgent(sessionID);
  76. m_log.DebugFormat("[PRESENCE SERVICE]: LogoutAgent: session {0}, user {1}, region {2}",
  77. sessionID,
  78. (presence == null) ? null : presence.UserID,
  79. (presence == null) ? null : presence.RegionID.ToString());
  80. return m_Database.Delete("SessionID", sessionID.ToString());
  81. }
  82. public bool LogoutRegionAgents(UUID regionID)
  83. {
  84. PresenceData[] prevSessions = GetRegionAgents(regionID);
  85. if ((prevSessions == null) || (prevSessions.Length == 0))
  86. return true;
  87. m_log.DebugFormat("[PRESENCE SERVICE]: Logout users in region {0}: {1}", regionID,
  88. string.Join(", ", Array.ConvertAll(prevSessions, session => session.UserID)));
  89. // There's a small chance that LogoutRegionAgents() will logout different users than the
  90. // list that was logged above, but it's unlikely and not worth dealing with.
  91. m_Database.LogoutRegionAgents(regionID);
  92. return true;
  93. }
  94. public bool ReportAgent(UUID sessionID, UUID regionID)
  95. {
  96. try
  97. {
  98. PresenceData presence = m_Database.Get(sessionID);
  99. bool success;
  100. if (presence == null)
  101. success = false;
  102. else
  103. success = m_Database.ReportAgent(sessionID, regionID);
  104. m_log.DebugFormat("[PRESENCE SERVICE]: ReportAgent{0}: session {1}, user {2}, region {3}. Previously: {4}",
  105. success ? "" : " failed",
  106. sessionID, (presence == null) ? null : presence.UserID, regionID,
  107. (presence == null) ? "not logged-in" : "region " + presence.RegionID);
  108. return success;
  109. }
  110. catch (Exception e)
  111. {
  112. m_log.Debug(string.Format("[PRESENCE SERVICE]: ReportAgent for session {0} threw exception ", sessionID), e);
  113. return false;
  114. }
  115. }
  116. public PresenceInfo GetAgent(UUID sessionID)
  117. {
  118. PresenceInfo ret = new PresenceInfo();
  119. PresenceData data = m_Database.Get(sessionID);
  120. if (data == null)
  121. return null;
  122. ret.UserID = data.UserID;
  123. ret.RegionID = data.RegionID;
  124. return ret;
  125. }
  126. public PresenceInfo[] GetAgents(string[] userIDs)
  127. {
  128. List<PresenceInfo> info = new List<PresenceInfo>();
  129. foreach (string userIDStr in userIDs)
  130. {
  131. PresenceData[] data = m_Database.Get("UserID", userIDStr);
  132. foreach (PresenceData d in data)
  133. {
  134. PresenceInfo ret = new PresenceInfo();
  135. ret.UserID = d.UserID;
  136. ret.RegionID = d.RegionID;
  137. info.Add(ret);
  138. }
  139. // m_log.DebugFormat(
  140. // "[PRESENCE SERVICE]: GetAgents for {0} found {1} presences", userIDStr, data.Length);
  141. }
  142. return info.ToArray();
  143. }
  144. /// <summary>
  145. /// Return the user's Presence. This only really works well if !AllowDuplicatePresences, but that's the default.
  146. /// </summary>
  147. private PresenceData GetUser(string userID)
  148. {
  149. PresenceData[] data = m_Database.Get("UserID", userID);
  150. if (data.Length > 0)
  151. return data[0];
  152. else
  153. return null;
  154. }
  155. private PresenceData[] GetRegionAgents(UUID regionID)
  156. {
  157. return m_Database.Get("RegionID", regionID.ToString());
  158. }
  159. }
  160. }