1
0

PresenceService.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. const int EXPIREMS = 300000;
  45. static ExpiringCacheOS<UUID, PresenceData> BySessionCache = new ExpiringCacheOS<UUID, PresenceData>(60000);
  46. static ExpiringCacheOS<string, PresenceData> ByUserCache = new ExpiringCacheOS<string, PresenceData>(60000);
  47. public PresenceService(IConfigSource config)
  48. : base(config)
  49. {
  50. m_log.Debug("[PRESENCE SERVICE]: Starting presence service");
  51. IConfig presenceConfig = config.Configs["PresenceService"];
  52. if (presenceConfig != null)
  53. {
  54. m_allowDuplicatePresences = presenceConfig.GetBoolean("AllowDuplicatePresences", m_allowDuplicatePresences);
  55. }
  56. }
  57. public bool LoginAgent(string userID, UUID sessionID, UUID secureSessionID)
  58. {
  59. bool inCache = ByUserCache.TryGetValue(userID, out PresenceData prevUser);
  60. if (!inCache)
  61. {
  62. PresenceData[] dataprv = m_Database.Get("UserID", userID);
  63. if (dataprv.Length > 0)
  64. prevUser = dataprv[0];
  65. }
  66. if (!m_allowDuplicatePresences && (prevUser != null))
  67. {
  68. m_Database.Delete("UserID", userID.ToString());
  69. if(inCache)
  70. {
  71. BySessionCache.Remove(prevUser.SessionID);
  72. ByUserCache.Remove(userID);
  73. }
  74. }
  75. PresenceData data = new PresenceData();
  76. data.UserID = userID;
  77. data.RegionID = UUID.Zero;
  78. data.SessionID = sessionID;
  79. data.Data = new Dictionary<string, string>();
  80. data.Data["SecureSessionID"] = secureSessionID.ToString();
  81. m_Database.Store(data);
  82. BySessionCache.Add(sessionID, data, EXPIREMS);
  83. ByUserCache.Add(userID, data, EXPIREMS);
  84. string prevUserStr = "";
  85. if (prevUser != null)
  86. prevUserStr = string.Format(". This user was already logged-in: session {0}, region {1}", prevUser.SessionID, prevUser.RegionID);
  87. m_log.DebugFormat("[PRESENCE SERVICE]: LoginAgent: session {0}, user {1}, region {2}, secure session {3}{4}",
  88. data.SessionID, data.UserID, data.RegionID, secureSessionID, prevUserStr);
  89. return true;
  90. }
  91. public bool LogoutAgent(UUID sessionID)
  92. {
  93. bool inCache = BySessionCache.TryGetValue(sessionID, out PresenceData presence);
  94. if(!inCache)
  95. presence = m_Database.Get(sessionID);
  96. m_log.DebugFormat("[PRESENCE SERVICE]: LogoutAgent: session {0}, user {1}, region {2}",
  97. sessionID,
  98. (presence == null) ? null : presence.UserID,
  99. (presence == null) ? null : presence.RegionID.ToString());
  100. bool ret = m_Database.Delete("SessionID", sessionID.ToString());
  101. if(inCache)
  102. {
  103. BySessionCache.Remove(sessionID);
  104. if(presence is not null)
  105. ByUserCache.Remove(presence.UserID);
  106. }
  107. return ret;
  108. }
  109. public bool LogoutRegionAgents(UUID regionID)
  110. {
  111. PresenceData[] prevSessions = GetRegionAgents(regionID);
  112. if ((prevSessions is null) || (prevSessions.Length == 0))
  113. return true;
  114. m_log.DebugFormat("[PRESENCE SERVICE]: Logout users in region {0}", regionID);
  115. for (int i = 0; i < prevSessions.Length; ++i)
  116. {
  117. PresenceData pd = prevSessions[i];
  118. BySessionCache.Remove(pd.SessionID);
  119. ByUserCache.Remove(pd.UserID);
  120. }
  121. // There's a small chance that LogoutRegionAgents() will logout different users than the
  122. // list that was logged above, but it's unlikely and not worth dealing with.
  123. m_Database.LogoutRegionAgents(regionID);
  124. return true;
  125. }
  126. public bool ReportAgent(UUID sessionID, UUID regionID)
  127. {
  128. try
  129. {
  130. bool inCache = BySessionCache.TryGetValue(sessionID, out PresenceData presence);
  131. if(!inCache)
  132. presence = m_Database.Get(sessionID);
  133. bool success;
  134. if (presence == null)
  135. success = false;
  136. else
  137. success = m_Database.ReportAgent(sessionID, regionID);
  138. m_log.DebugFormat("[PRESENCE SERVICE]: ReportAgent{0}: session {1}, user {2}, region {3}. Previously: {4}",
  139. success ? "" : " failed",
  140. sessionID, (presence == null) ? null : presence.UserID, regionID,
  141. (presence == null) ? "not logged-in" : "region " + presence.RegionID);
  142. if (success)
  143. {
  144. presence.RegionID = regionID;
  145. BySessionCache.Add(sessionID, presence, EXPIREMS); // lastseen seems unused
  146. ByUserCache.Add(presence.UserID, presence, EXPIREMS); // lastseen seems unused
  147. }
  148. else if (inCache)
  149. {
  150. BySessionCache.Remove(sessionID);
  151. ByUserCache.Remove(presence.UserID);
  152. }
  153. return success;
  154. }
  155. catch (Exception e)
  156. {
  157. m_log.Debug(string.Format("[PRESENCE SERVICE]: ReportAgent for session {0} threw exception ", sessionID), e);
  158. return false;
  159. }
  160. }
  161. public PresenceInfo GetAgent(UUID sessionID)
  162. {
  163. if(!BySessionCache.TryGetValue(sessionID, out PresenceData data))
  164. data = m_Database.Get(sessionID);
  165. if (data == null)
  166. return null;
  167. BySessionCache.Add(sessionID, data, EXPIREMS);
  168. ByUserCache.Add(data.UserID, data, EXPIREMS);
  169. var ret = new PresenceInfo()
  170. {
  171. UserID = data.UserID,
  172. RegionID = data.RegionID
  173. };
  174. return ret;
  175. }
  176. public PresenceInfo[] GetAgents(string[] userIDs)
  177. {
  178. var info = new List<PresenceInfo>(userIDs.Length);
  179. PresenceInfo ret;
  180. foreach (string userIDStr in userIDs)
  181. {
  182. if(ByUserCache.TryGetValue(userIDStr, out PresenceData pd))
  183. {
  184. ByUserCache.Add(pd.UserID, pd, EXPIREMS);
  185. BySessionCache.Add(pd.SessionID, pd, EXPIREMS);
  186. ret = new PresenceInfo()
  187. {
  188. UserID = pd.UserID,
  189. RegionID = pd.RegionID
  190. };
  191. info.Add(ret);
  192. }
  193. else
  194. {
  195. PresenceData[] data = m_Database.Get("UserID", userIDStr);
  196. if(data.Length == 0)
  197. continue;
  198. PresenceData d = data[0];
  199. ByUserCache.Add(d.UserID, d, EXPIREMS);
  200. BySessionCache.Add(d.SessionID, d, EXPIREMS);
  201. ret = new PresenceInfo()
  202. {
  203. UserID = d.UserID,
  204. RegionID = d.RegionID
  205. };
  206. info.Add(ret);
  207. }
  208. //m_log.DebugFormat(
  209. // "[PRESENCE SERVICE]: GetAgents for {0} found {1} presences", userIDStr, data.Length);
  210. }
  211. return info.ToArray();
  212. }
  213. private PresenceData[] GetRegionAgents(UUID regionID)
  214. {
  215. return m_Database.Get("RegionID", regionID.ToString());
  216. }
  217. }
  218. }