NullPresenceData.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if (m_presenceData.ContainsKey(sessionID))
  64. {
  65. return m_presenceData[sessionID];
  66. }
  67. return null;
  68. }
  69. public void LogoutRegionAgents(UUID regionID)
  70. {
  71. if (Instance != this)
  72. {
  73. Instance.LogoutRegionAgents(regionID);
  74. return;
  75. }
  76. List<UUID> toBeDeleted = new List<UUID>();
  77. foreach (KeyValuePair<UUID, PresenceData> kvp in m_presenceData)
  78. if (kvp.Value.RegionID == regionID)
  79. toBeDeleted.Add(kvp.Key);
  80. foreach (UUID u in toBeDeleted)
  81. m_presenceData.Remove(u);
  82. }
  83. public bool ReportAgent(UUID sessionID, UUID regionID)
  84. {
  85. if (Instance != this)
  86. return Instance.ReportAgent(sessionID, regionID);
  87. if (m_presenceData.ContainsKey(sessionID))
  88. {
  89. m_presenceData[sessionID].RegionID = regionID;
  90. return true;
  91. }
  92. return false;
  93. }
  94. public PresenceData[] Get(string field, string data)
  95. {
  96. if (Instance != this)
  97. return Instance.Get(field, data);
  98. // m_log.DebugFormat(
  99. // "[NULL PRESENCE DATA]: Getting presence data for field {0} with parameter {1}", field, data);
  100. List<PresenceData> presences = new List<PresenceData>();
  101. if (field == "UserID")
  102. {
  103. foreach (PresenceData p in m_presenceData.Values)
  104. {
  105. if (p.UserID == data)
  106. {
  107. presences.Add(p);
  108. // Console.WriteLine("HOME for " + p.UserID + " is " + (p.Data.ContainsKey("HomeRegionID") ? p.Data["HomeRegionID"] : "Not found"));
  109. }
  110. }
  111. return presences.ToArray();
  112. }
  113. else if (field == "SessionID")
  114. {
  115. UUID session = UUID.Zero;
  116. if (!UUID.TryParse(data, out session))
  117. return presences.ToArray();
  118. if (m_presenceData.ContainsKey(session))
  119. {
  120. presences.Add(m_presenceData[session]);
  121. return presences.ToArray();
  122. }
  123. }
  124. else if (field == "RegionID")
  125. {
  126. UUID region = UUID.Zero;
  127. if (!UUID.TryParse(data, out region))
  128. return presences.ToArray();
  129. foreach (PresenceData p in m_presenceData.Values)
  130. if (p.RegionID == region)
  131. presences.Add(p);
  132. return presences.ToArray();
  133. }
  134. else
  135. {
  136. foreach (PresenceData p in m_presenceData.Values)
  137. {
  138. if (p.Data.ContainsKey(field) && p.Data[field] == data)
  139. presences.Add(p);
  140. }
  141. return presences.ToArray();
  142. }
  143. return presences.ToArray();
  144. }
  145. public bool Delete(string field, string data)
  146. {
  147. // m_log.DebugFormat(
  148. // "[NULL PRESENCE DATA]: Deleting presence data for field {0} with parameter {1}", field, data);
  149. if (Instance != this)
  150. return Instance.Delete(field, data);
  151. List<UUID> presences = new List<UUID>();
  152. if (field == "UserID")
  153. {
  154. foreach (KeyValuePair<UUID, PresenceData> p in m_presenceData)
  155. if (p.Value.UserID == data)
  156. presences.Add(p.Key);
  157. }
  158. else if (field == "SessionID")
  159. {
  160. UUID session = UUID.Zero;
  161. if (UUID.TryParse(data, out session))
  162. {
  163. if (m_presenceData.ContainsKey(session))
  164. {
  165. presences.Add(session);
  166. }
  167. }
  168. }
  169. else if (field == "RegionID")
  170. {
  171. UUID region = UUID.Zero;
  172. if (UUID.TryParse(data, out region))
  173. {
  174. foreach (KeyValuePair<UUID, PresenceData> p in m_presenceData)
  175. if (p.Value.RegionID == region)
  176. presences.Add(p.Key);
  177. }
  178. }
  179. else
  180. {
  181. foreach (KeyValuePair<UUID, PresenceData> p in m_presenceData)
  182. {
  183. if (p.Value.Data.ContainsKey(field) && p.Value.Data[field] == data)
  184. presences.Add(p.Key);
  185. }
  186. }
  187. foreach (UUID u in presences)
  188. m_presenceData.Remove(u);
  189. if (presences.Count == 0)
  190. return false;
  191. return true;
  192. }
  193. public bool VerifyAgent(UUID agentId, UUID secureSessionID)
  194. {
  195. if (Instance != this)
  196. return Instance.VerifyAgent(agentId, secureSessionID);
  197. return false;
  198. }
  199. }
  200. }