/Aurora/Modules/World/ActivitiyDetectors/ActivityDetector.cs

https://bitbucket.org/VirtualReality/software-testing · C# · 191 lines · 139 code · 20 blank · 32 comment · 21 complexity · 78d2f918aa0fb6f524bddbd190102695 MD5 · raw file

  1. /*
  2. * Copyright (c) Contributors, http://aurora-sim.org/, 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 Aurora-Sim 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 Aurora.Framework;
  28. using Aurora.Framework.ClientInterfaces;
  29. using Aurora.Framework.ConsoleFramework;
  30. using Aurora.Framework.Modules;
  31. using Aurora.Framework.PresenceInfo;
  32. using Aurora.Framework.SceneInfo;
  33. using Aurora.Framework.Services;
  34. using Aurora.Framework.Services.ClassHelpers.Other;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using System;
  38. using System.Collections.Generic;
  39. using System.Timers;
  40. namespace Aurora.Modules.ActivityDetectors
  41. {
  42. public class ActivityDetector : INonSharedRegionModule
  43. {
  44. private IScene m_scene;
  45. private readonly List<UUID> m_zombieAgents = new List<UUID>();
  46. private Timer m_presenceUpdateTimer;
  47. #region INonSharedRegionModule Members
  48. public void Initialise(IConfigSource source)
  49. {
  50. }
  51. public void Close()
  52. {
  53. }
  54. public void AddRegion(IScene scene)
  55. {
  56. scene.AuroraEventManager.RegisterEventHandler("AgentIsAZombie", OnGenericEvent);
  57. scene.EventManager.OnNewClient += OnNewClient;
  58. scene.EventManager.OnClosingClient += OnClosingClient;
  59. }
  60. public void RemoveRegion(IScene scene)
  61. {
  62. ISyncMessagePosterService syncMessage = scene.RequestModuleInterface<ISyncMessagePosterService>();
  63. if (syncMessage != null)
  64. syncMessage.PostToServer(SyncMessageHelper.LogoutRegionAgents(scene.RegionInfo.RegionID));
  65. scene.EventManager.OnNewClient -= OnNewClient;
  66. scene.EventManager.OnClosingClient -= OnClosingClient;
  67. m_scene = null;
  68. }
  69. public void RegionLoaded(IScene scene)
  70. {
  71. scene.EventManager.OnStartupFullyComplete += EventManager_OnStartupFullyComplete;
  72. m_scene = scene;
  73. if (m_presenceUpdateTimer == null)
  74. {
  75. m_presenceUpdateTimer = new Timer {Interval = 1000*60*28};
  76. //As agents move around, they could get to regions that won't update them in time, so we cut
  77. // the time in half, and then a bit less so that they are updated consistantly
  78. m_presenceUpdateTimer.Elapsed += m_presenceUpdateTimer_Elapsed;
  79. m_presenceUpdateTimer.Start();
  80. }
  81. }
  82. public string Name
  83. {
  84. get { return "ActivityDetector"; }
  85. }
  86. public Type ReplaceableInterface
  87. {
  88. get { return null; }
  89. }
  90. #endregion
  91. private void m_presenceUpdateTimer_Elapsed(object sender, ElapsedEventArgs e)
  92. {
  93. IAgentInfoService service = m_scene.RequestModuleInterface<IAgentInfoService>();
  94. if (service == null)
  95. return;
  96. foreach (IScenePresence sp in m_scene.GetScenePresences())
  97. {
  98. //This causes the last pos to be updated in the database, along with the last seen time
  99. sp.AddChildAgentUpdateTaint(1);
  100. }
  101. }
  102. private void EventManager_OnStartupFullyComplete(IScene scene, List<string> data)
  103. {
  104. //Just send the RegionIsOnline message, it will log out all the agents for the region as well
  105. ISyncMessagePosterService syncMessage = scene.RequestModuleInterface<ISyncMessagePosterService>();
  106. if (syncMessage != null)
  107. syncMessage.PostToServer(SyncMessageHelper.RegionIsOnline(scene.RegionInfo.RegionID));
  108. }
  109. public void OnNewClient(IClientAPI client)
  110. {
  111. client.OnConnectionClosed += OnConnectionClose;
  112. }
  113. private void OnClosingClient(IClientAPI client)
  114. {
  115. client.OnConnectionClosed -= OnConnectionClose;
  116. }
  117. public object OnGenericEvent(string functionName, object parameters)
  118. {
  119. m_zombieAgents.Add((UUID) parameters);
  120. return null;
  121. }
  122. public void OnConnectionClose(IClientAPI client)
  123. {
  124. IScenePresence sp = null;
  125. client.Scene.TryGetScenePresence(client.AgentId, out sp);
  126. if (client.IsLoggingOut && sp != null & !sp.IsChildAgent)
  127. {
  128. MainConsole.Instance.InfoFormat("[ActivityDetector]: Detected logout of user {0} in region {1}",
  129. client.Name,
  130. client.Scene.RegionInfo.RegionName);
  131. //Inform the grid service about it
  132. if (m_zombieAgents.Contains(client.AgentId))
  133. {
  134. m_zombieAgents.Remove(client.AgentId);
  135. return; //They are a known zombie, just clear them out and go on with life!
  136. }
  137. AgentPosition agentpos = new AgentPosition
  138. {
  139. AgentID = sp.UUID,
  140. AtAxis = sp.CameraAtAxis,
  141. Center = sp.CameraPosition,
  142. Far = sp.DrawDistance,
  143. LeftAxis = Vector3.Zero,
  144. Position = sp.AbsolutePosition
  145. };
  146. if (agentpos.Position.X > sp.Scene.RegionInfo.RegionSizeX)
  147. agentpos.Position.X = sp.Scene.RegionInfo.RegionSizeX;
  148. if (agentpos.Position.Y > sp.Scene.RegionInfo.RegionSizeY)
  149. agentpos.Position.Y = sp.Scene.RegionInfo.RegionSizeY;
  150. if (agentpos.Position.Z > sp.Scene.RegionInfo.RegionSizeZ)
  151. agentpos.Position.Z = sp.Scene.RegionInfo.RegionSizeZ;
  152. if (agentpos.Position.X < 0)
  153. agentpos.Position.X = 0;
  154. if (agentpos.Position.Y < 0)
  155. agentpos.Position.Y = 0;
  156. if (agentpos.Position.Z < 0)
  157. agentpos.Position.Z = 0;
  158. agentpos.RegionHandle = sp.Scene.RegionInfo.RegionHandle;
  159. agentpos.Size = sp.PhysicsActor != null ? sp.PhysicsActor.Size : new Vector3(0, 0, 1.8f);
  160. agentpos.UpAxis = Vector3.Zero;
  161. agentpos.Velocity = sp.Velocity;
  162. agentpos.UserGoingOffline = true; //Don't attempt to add us into other regions
  163. //Send the child agent data update
  164. ISyncMessagePosterService syncPoster = sp.Scene.RequestModuleInterface<ISyncMessagePosterService>();
  165. if (syncPoster != null)
  166. syncPoster.PostToServer(SyncMessageHelper.AgentLoggedOut(client.AgentId,
  167. client.Scene.RegionInfo.RegionID, agentpos));
  168. }
  169. }
  170. }
  171. }