PageRenderTime 30ms CodeModel.GetById 22ms app.highlight 6ms RepoModel.GetById 0ms app.codeStats 0ms

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