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 && presence != null)
  102. {
  103. BySessionCache.Remove(presence.SessionID);
  104. ByUserCache.Remove(presence.UserID);
  105. }
  106. return ret;
  107. }
  108. public bool LogoutRegionAgents(UUID regionID)
  109. {
  110. PresenceData[] prevSessions = GetRegionAgents(regionID);
  111. if ((prevSessions == null) || (prevSessions.Length == 0))
  112. return true;
  113. m_log.DebugFormat("[PRESENCE SERVICE]: Logout users in region {0}", regionID);
  114. for (int i = 0; i < prevSessions.Length; ++i)
  115. {
  116. PresenceData pd = prevSessions[i];
  117. BySessionCache.Remove(pd.SessionID);
  118. ByUserCache.Remove(pd.UserID);
  119. }
  120. // There's a small chance that LogoutRegionAgents() will logout different users than the
  121. // list that was logged above, but it's unlikely and not worth dealing with.
  122. m_Database.LogoutRegionAgents(regionID);
  123. return true;
  124. }
  125. public bool ReportAgent(UUID sessionID, UUID regionID)
  126. {
  127. try
  128. {
  129. bool inCache = BySessionCache.TryGetValue(sessionID, out PresenceData presence);
  130. if(!inCache)
  131. presence = m_Database.Get(sessionID);
  132. bool success;
  133. if (presence == null)
  134. success = false;
  135. else
  136. success = m_Database.ReportAgent(sessionID, regionID);
  137. m_log.DebugFormat("[PRESENCE SERVICE]: ReportAgent{0}: session {1}, user {2}, region {3}. Previously: {4}",
  138. success ? "" : " failed",
  139. sessionID, (presence == null) ? null : presence.UserID, regionID,
  140. (presence == null) ? "not logged-in" : "region " + presence.RegionID);
  141. if (success)
  142. {
  143. presence.RegionID = regionID;
  144. BySessionCache.Add(sessionID, presence, EXPIREMS); // lastseen seems unused
  145. ByUserCache.Add(presence.UserID, presence, EXPIREMS); // lastseen seems unused
  146. }
  147. else if (inCache)
  148. {
  149. BySessionCache.Remove(sessionID);
  150. ByUserCache.Remove(presence.UserID);
  151. }
  152. return success;
  153. }
  154. catch (Exception e)
  155. {
  156. m_log.Debug(string.Format("[PRESENCE SERVICE]: ReportAgent for session {0} threw exception ", sessionID), e);
  157. return false;
  158. }
  159. }
  160. public PresenceInfo GetAgent(UUID sessionID)
  161. {
  162. if(!BySessionCache.TryGetValue(sessionID, out PresenceData data))
  163. data = m_Database.Get(sessionID);
  164. if (data == null)
  165. return null;
  166. BySessionCache.Add(sessionID, data, EXPIREMS);
  167. ByUserCache.Add(data.UserID, data, EXPIREMS);
  168. var ret = new PresenceInfo()
  169. {
  170. UserID = data.UserID,
  171. RegionID = data.RegionID
  172. };
  173. return ret;
  174. }
  175. public PresenceInfo[] GetAgents(string[] userIDs)
  176. {
  177. var info = new List<PresenceInfo>(userIDs.Length);
  178. PresenceInfo ret;
  179. foreach (string userIDStr in userIDs)
  180. {
  181. if(ByUserCache.TryGetValue(userIDStr, out PresenceData pd))
  182. {
  183. ByUserCache.Add(pd.UserID, pd, EXPIREMS);
  184. BySessionCache.Add(pd.SessionID, pd, EXPIREMS);
  185. ret = new PresenceInfo()
  186. {
  187. UserID = pd.UserID,
  188. RegionID = pd.RegionID
  189. };
  190. info.Add(ret);
  191. }
  192. else
  193. {
  194. PresenceData[] data = m_Database.Get("UserID", userIDStr);
  195. if(data.Length == 0)
  196. continue;
  197. PresenceData d = data[0];
  198. ByUserCache.Add(d.UserID, d, EXPIREMS);
  199. BySessionCache.Add(d.SessionID, d, EXPIREMS);
  200. ret = new PresenceInfo()
  201. {
  202. UserID = d.UserID,
  203. RegionID = d.RegionID
  204. };
  205. info.Add(ret);
  206. }
  207. //m_log.DebugFormat(
  208. // "[PRESENCE SERVICE]: GetAgents for {0} found {1} presences", userIDStr, data.Length);
  209. }
  210. return info.ToArray();
  211. }
  212. private PresenceData[] GetRegionAgents(UUID regionID)
  213. {
  214. return m_Database.Get("RegionID", regionID.ToString());
  215. }
  216. }
  217. }