NullPresenceData.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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;
  29. using System.Collections.Generic;
  30. using System.Reflection;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Data;
  35. namespace OpenSim.Data.Null
  36. {
  37. public class NullPresenceData : IPresenceData
  38. {
  39. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  40. public static NullPresenceData Instance;
  41. Dictionary<UUID, PresenceData> m_presenceData = new Dictionary<UUID, PresenceData>();
  42. public NullPresenceData(string connectionString, string realm)
  43. {
  44. if (Instance == null)
  45. {
  46. Instance = this;
  47. //Console.WriteLine("[XXX] NullRegionData constructor");
  48. }
  49. }
  50. public bool Store(PresenceData data)
  51. {
  52. if (Instance != this)
  53. return Instance.Store(data);
  54. // m_log.DebugFormat("[NULL PRESENCE DATA]: Storing presence {0}", data.UserID);
  55. // Console.WriteLine("HOME for " + data.UserID + " is " + (data.Data.ContainsKey("HomeRegionID") ? data.Data["HomeRegionID"] : "Not found"));
  56. m_presenceData[data.SessionID] = data;
  57. return true;
  58. }
  59. public PresenceData Get(UUID sessionID)
  60. {
  61. if (Instance != this)
  62. return Instance.Get(sessionID);
  63. return m_presenceData.TryGetValue(sessionID, out PresenceData pd) ? pd : null;
  64. }
  65. public void LogoutRegionAgents(UUID regionID)
  66. {
  67. if (Instance != this)
  68. {
  69. Instance.LogoutRegionAgents(regionID);
  70. return;
  71. }
  72. List<UUID> toBeDeleted = new List<UUID>();
  73. foreach (KeyValuePair<UUID, PresenceData> kvp in m_presenceData)
  74. if (kvp.Value.RegionID == regionID)
  75. toBeDeleted.Add(kvp.Key);
  76. foreach (UUID u in toBeDeleted)
  77. m_presenceData.Remove(u);
  78. }
  79. public bool ReportAgent(UUID sessionID, UUID regionID)
  80. {
  81. if (Instance != this)
  82. return Instance.ReportAgent(sessionID, regionID);
  83. if (m_presenceData.TryGetValue(sessionID, out PresenceData pd))
  84. {
  85. pd.RegionID = regionID;
  86. return true;
  87. }
  88. return false;
  89. }
  90. public PresenceData[] Get(string field, string data)
  91. {
  92. if (Instance != this)
  93. return Instance.Get(field, data);
  94. // m_log.DebugFormat(
  95. // "[NULL PRESENCE DATA]: Getting presence data for field {0} with parameter {1}", field, data);
  96. List<PresenceData> presences = new List<PresenceData>();
  97. if (field == "UserID")
  98. {
  99. foreach (PresenceData p in m_presenceData.Values)
  100. {
  101. if (p.UserID == data)
  102. {
  103. presences.Add(p);
  104. // Console.WriteLine("HOME for " + p.UserID + " is " + (p.Data.ContainsKey("HomeRegionID") ? p.Data["HomeRegionID"] : "Not found"));
  105. }
  106. }
  107. return presences.ToArray();
  108. }
  109. else if (field == "SessionID")
  110. {
  111. if (!UUID.TryParse(data, out UUID session))
  112. return presences.ToArray();
  113. if (m_presenceData.TryGetValue(session, out PresenceData pd))
  114. {
  115. presences.Add(pd);
  116. return presences.ToArray();
  117. }
  118. }
  119. else if (field == "RegionID")
  120. {
  121. UUID region = UUID.Zero;
  122. if (!UUID.TryParse(data, out region))
  123. return presences.ToArray();
  124. foreach (PresenceData p in m_presenceData.Values)
  125. if (p.RegionID == region)
  126. presences.Add(p);
  127. return presences.ToArray();
  128. }
  129. else
  130. {
  131. foreach (PresenceData p in m_presenceData.Values)
  132. {
  133. if (p.Data.TryGetValue(field, out string spd) && spd == data)
  134. presences.Add(p);
  135. }
  136. return presences.ToArray();
  137. }
  138. return presences.ToArray();
  139. }
  140. public bool Delete(string field, string data)
  141. {
  142. // m_log.DebugFormat(
  143. // "[NULL PRESENCE DATA]: Deleting presence data for field {0} with parameter {1}", field, data);
  144. if (Instance != this)
  145. return Instance.Delete(field, data);
  146. List<UUID> presences = new List<UUID>();
  147. if (field == "UserID")
  148. {
  149. foreach (KeyValuePair<UUID, PresenceData> p in m_presenceData)
  150. if (p.Value.UserID == data)
  151. presences.Add(p.Key);
  152. }
  153. else if (field == "SessionID")
  154. {
  155. UUID session = UUID.Zero;
  156. if (UUID.TryParse(data, out session))
  157. {
  158. if (m_presenceData.ContainsKey(session))
  159. {
  160. presences.Add(session);
  161. }
  162. }
  163. }
  164. else if (field == "RegionID")
  165. {
  166. UUID region = UUID.Zero;
  167. if (UUID.TryParse(data, out region))
  168. {
  169. foreach (KeyValuePair<UUID, PresenceData> p in m_presenceData)
  170. if (p.Value.RegionID == region)
  171. presences.Add(p.Key);
  172. }
  173. }
  174. else
  175. {
  176. foreach (KeyValuePair<UUID, PresenceData> p in m_presenceData)
  177. {
  178. if (p.Value.Data.ContainsKey(field) && p.Value.Data[field] == data)
  179. presences.Add(p.Key);
  180. }
  181. }
  182. foreach (UUID u in presences)
  183. m_presenceData.Remove(u);
  184. if (presences.Count == 0)
  185. return false;
  186. return true;
  187. }
  188. public bool VerifyAgent(UUID agentId, UUID secureSessionID)
  189. {
  190. if (Instance != this)
  191. return Instance.VerifyAgent(agentId, secureSessionID);
  192. return false;
  193. }
  194. }
  195. }