Cleaned up access to scenepresences in scenegraph. GetScenePresences and GetAvatars have been removed to consolidate locking and iteration within SceneGraph. All callers which used these to then iterate over presences have been refactored to instead pass their delegates to Scene.ForEachScenePresence(Action<ScenePresence>).

This commit is contained in:
Dan Lake
2010-03-19 05:51:16 -07:00
committed by John Hurliman
parent 73e9b0be72
commit 859bc717a4
22 changed files with 378 additions and 513 deletions

View File

@@ -3269,7 +3269,7 @@ namespace OpenSim.Region.Framework.Scenes
}
}
ScenePresence sp = m_sceneGraph.GetScenePresence(agent.AgentID);
ScenePresence sp = GetScenePresence(agent.AgentID);
if (sp != null)
{
m_log.DebugFormat(
@@ -3561,8 +3561,7 @@ namespace OpenSim.Region.Framework.Scenes
/// <param name="message">message to display to the user. Reason for being logged off</param>
public void HandleLogOffUserFromGrid(UUID AvatarID, UUID RegionSecret, string message)
{
ScenePresence loggingOffUser = null;
loggingOffUser = GetScenePresence(AvatarID);
ScenePresence loggingOffUser = GetScenePresence(AvatarID);
if (loggingOffUser != null)
{
UUID localRegionSecret = UUID.Zero;
@@ -3598,8 +3597,8 @@ namespace OpenSim.Region.Framework.Scenes
/// <param name="isFlying"></param>
public virtual void AgentCrossing(UUID agentID, Vector3 position, bool isFlying)
{
ScenePresence presence;
if(m_sceneGraph.TryGetAvatar(agentID, out presence))
ScenePresence presence = GetScenePresence(agentID);
if(presence != null)
{
try
{
@@ -3773,8 +3772,8 @@ namespace OpenSim.Region.Framework.Scenes
public void RequestTeleportLocation(IClientAPI remoteClient, ulong regionHandle, Vector3 position,
Vector3 lookAt, uint teleportFlags)
{
ScenePresence sp;
if(m_sceneGraph.TryGetAvatar(remoteClient.AgentId, out sp))
ScenePresence sp = GetScenePresence(remoteClient.AgentId);
if (sp != null)
{
uint regionX = m_regInfo.RegionLocX;
uint regionY = m_regInfo.RegionLocY;
@@ -3952,17 +3951,17 @@ namespace OpenSim.Region.Framework.Scenes
m_log.ErrorFormat("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16}{5,-16}{6,-16}", "Firstname", "Lastname",
"Agent ID", "Session ID", "Circuit", "IP", "World");
foreach (ScenePresence scenePresence in GetAvatars())
ForEachScenePresence(delegate(ScenePresence sp)
{
m_log.ErrorFormat("{0,-16}{1,-16}{2,-25}{3,-25}{4,-16},{5,-16}{6,-16}",
scenePresence.Firstname,
scenePresence.Lastname,
scenePresence.UUID,
scenePresence.ControllingClient.AgentId,
sp.Firstname,
sp.Lastname,
sp.UUID,
sp.ControllingClient.AgentId,
"Unknown",
"Unknown",
RegionInfo.RegionName);
}
});
break;
}
@@ -4128,72 +4127,42 @@ namespace OpenSim.Region.Framework.Scenes
m_sceneGraph.RemovePhysicalPrim(num);
}
//The idea is to have a group of method that return a list of avatars meeting some requirement
// ie it could be all m_scenePresences within a certain range of the calling prim/avatar.
//
// GetAvatars returns a new list of all root agent presences in the scene
// GetScenePresences returns a new list of all presences in the scene or a filter may be passed.
// GetScenePresence returns the presence with matching UUID or first/last name.
// ForEachScenePresence requests the Scene to run a delegate function against all presences.
/// <summary>
/// Return a list of all avatars in this region.
/// This list is a new object, so it can be iterated over without locking.
/// </summary>
/// <returns></returns>
public List<ScenePresence> GetAvatars()
public int GetRootAgentCount()
{
return m_sceneGraph.GetAvatars();
return m_sceneGraph.GetRootAgentCount();
}
public int GetChildAgentCount()
{
return m_sceneGraph.GetChildAgentCount();
}
/// <summary>
/// Return a list of all ScenePresences in this region. This returns child agents as well as root agents.
/// This list is a new object, so it can be iterated over without locking.
/// Request a scene presence by UUID. Fast, indexed lookup.
/// </summary>
/// <returns></returns>
public List<ScenePresence> GetScenePresences()
/// <param name="agentID"></param>
/// <returns>null if the presence was not found</returns>
public ScenePresence GetScenePresence(UUID agentID)
{
return m_sceneGraph.GetScenePresences();
return m_sceneGraph.GetScenePresence(agentID);
}
/// <summary>
/// Request a filtered list of ScenePresences in this region.
/// This list is a new object, so it can be iterated over without locking.
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
public List<ScenePresence> GetScenePresences(FilterAvatarList filter)
{
return m_sceneGraph.GetScenePresences(filter);
}
/// <summary>
/// Request a scene presence by UUID
/// </summary>
/// <param name="avatarID"></param>
/// <returns></returns>
public ScenePresence GetScenePresence(UUID avatarID)
{
return m_sceneGraph.GetScenePresence(avatarID);
}
/// <summary>
/// Request the ScenePresence in this region by first/last name.
/// Should normally only be a single match, but first is always returned
/// Request the scene presence by name.
/// </summary>
/// <param name="firstName"></param>
/// <param name="lastName"></param>
/// <returns></returns>
/// <returns>null if the presence was not found</returns>
public ScenePresence GetScenePresence(string firstName, string lastName)
{
return m_sceneGraph.GetScenePresence(firstName, lastName);
}
/// <summary>
/// Request the ScenePresence in this region by localID.
/// Request the scene presence by localID.
/// </summary>
/// <param name="localID"></param>
/// <returns></returns>
/// <returns>null if the presence was not found</returns>
public ScenePresence GetScenePresence(uint localID)
{
return m_sceneGraph.GetScenePresence(localID);

View File

@@ -699,116 +699,84 @@ namespace OpenSim.Region.Framework.Scenes
return null;
}
// The idea is to have a group of method that return a list of avatars meeting some requirement
// ie it could be all m_scenePresences within a certain range of the calling prim/avatar.
//
// GetAvatars returns a new list of all root agent presences in the scene
// GetScenePresences returns a new list of all presences in the scene or a filter may be passed.
// GetScenePresence returns the presence with matching UUID or the first presence matching the passed filter.
// ForEachScenePresence requests the Scene to run a delegate function against all presences.
/// <summary>
/// Request a list of all avatars in this region (no child agents)
/// This list is a new object, so it can be iterated over without locking.
/// </summary>
/// <returns></returns>
public List<ScenePresence> GetAvatars()
{
return GetScenePresences(delegate(ScenePresence scenePresence)
{
return !scenePresence.IsChildAgent;
});
}
/// <summary>
/// Request a list of m_scenePresences in this World
/// Returns a copy so it can be iterated without a lock.
/// Request a copy of m_scenePresences in this World
/// There is no guarantee that presences will remain in the scene after the list is returned.
/// This list should remain private to SceneGraph. Callers wishing to iterate should instead
/// pass a delegate to ForEachScenePresence.
/// </summary>
/// <returns></returns>
protected internal List<ScenePresence> GetScenePresences()
private List<ScenePresence> GetScenePresences()
{
List<ScenePresence> result;
lock (m_scenePresences)
{
result = new List<ScenePresence>(m_scenePresenceArray.Length);
result.AddRange(m_scenePresenceArray);
}
return result;
return new List<ScenePresence>(m_scenePresenceArray);
}
/// <summary>
/// Request a filtered list of m_scenePresences in this World
/// Returns a copy so it can be iterated without a lock.
/// There is no guarantee that presences will remain in the scene after the list is returned.
/// </summary>
/// <returns></returns>
protected internal List<ScenePresence> GetScenePresences(FilterAvatarList filter)
{
List<ScenePresence> result = new List<ScenePresence>();
// Check each ScenePresence against the filter
ForEachScenePresence(delegate(ScenePresence presence)
{
if (filter(presence))
result.Add(presence);
});
return result;
}
/// <summary>
/// Request the ScenePresence in this region matching filter.
/// Only the first match is returned.
///
/// </summary>
/// <param name="filter"></param>
/// <returns></returns>
protected internal ScenePresence GetScenePresence(FilterAvatarList filter)
{
ScenePresence result = null;
// Get all of the ScenePresences
List<ScenePresence> presences = GetScenePresences();
foreach (ScenePresence presence in presences)
{
if (filter(presence))
{
result = presence;
break;
}
}
return result;
}
protected internal ScenePresence GetScenePresence(string firstName, string lastName)
{
return GetScenePresence(delegate(ScenePresence presence)
{
return(presence.Firstname == firstName && presence.Lastname == lastName);
});
}
/// <summary>
/// Request a scene presence by UUID
/// Request a scene presence by UUID. Fast, indexed lookup.
/// </summary>
/// <param name="agentID"></param>
/// <returns>null if the agent was not found</returns>
/// <returns>null if the presence was not found</returns>
protected internal ScenePresence GetScenePresence(UUID agentID)
{
ScenePresence sp;
TryGetAvatar(agentID, out sp);
lock (m_scenePresences)
{
m_scenePresences.TryGetValue(agentID, out sp);
}
return sp;
}
/// <summary>
/// Request the ScenePresence in this region by localID.
/// Request the scene presence by name.
/// </summary>
/// <param name="firstName"></param>
/// <param name="lastName"></param>
/// <returns>null if the presence was not found</returns>
protected internal ScenePresence GetScenePresence(string firstName, string lastName)
{
foreach (ScenePresence presence in GetScenePresences())
{
if (presence.Firstname == firstName && presence.Lastname == lastName)
return presence;
}
return null;
}
/// <summary>
/// Request the scene presence by localID.
/// </summary>
/// <param name="localID"></param>
/// <returns></returns>
/// <returns>null if the presence was not found</returns>
protected internal ScenePresence GetScenePresence(uint localID)
{
return GetScenePresence(delegate(ScenePresence presence)
foreach (ScenePresence presence in GetScenePresences())
if (presence.LocalId == localID)
return presence;
return null;
}
protected internal bool TryGetAvatar(UUID agentID, out ScenePresence avatar)
{
lock (m_scenePresences)
{
return (presence.LocalId == localID);
});
m_scenePresences.TryGetValue(agentID, out avatar);
}
return (avatar != null);
}
protected internal bool TryGetAvatarByName(string name, out ScenePresence avatar)
{
avatar = null;
foreach (ScenePresence presence in GetScenePresences())
{
if (String.Compare(name, presence.ControllingClient.Name, true) == 0)
{
avatar = presence;
break;
}
}
return (avatar != null);
}
/// <summary>
@@ -962,24 +930,6 @@ namespace OpenSim.Region.Framework.Scenes
return group.GetChildPart(fullID);
}
protected internal bool TryGetAvatar(UUID avatarId, out ScenePresence avatar)
{
lock (m_scenePresences)
{
m_scenePresences.TryGetValue(avatarId, out avatar);
}
return (avatar != null);
}
protected internal bool TryGetAvatarByName(string avatarName, out ScenePresence avatar)
{
avatar = GetScenePresence(delegate(ScenePresence presence)
{
return (String.Compare(avatarName, presence.ControllingClient.Name, true) == 0);
});
return (avatar != null);
}
/// <summary>
/// Returns a list of the entities in the scene. This is a new list so no locking is required to iterate over
/// it
@@ -1042,6 +992,10 @@ namespace OpenSim.Region.Framework.Scenes
return UUID.Zero;
}
/// <summary>
/// Performs action on all scene object groups.
/// </summary>
/// <param name="action"></param>
protected internal void ForEachSOG(Action<SceneObjectGroup> action)
{
List<SceneObjectGroup> objlist = new List<SceneObjectGroup>(SceneObjectGroupsByFullID.Values);
@@ -1061,23 +1015,41 @@ namespace OpenSim.Region.Framework.Scenes
/// <summary>
/// Performs action on all scene presences.
/// Performs action on all scene presences. This can ultimately run the actions in parallel but
/// any delegates passed in will need to implement their own locking on data they reference and
/// modify outside of the scope of the delegate.
/// </summary>
/// <param name="action"></param>
public void ForEachScenePresence(Action<ScenePresence> action)
{
List<ScenePresence> presences = GetScenePresences();
try
{
foreach(ScenePresence presence in presences)
// Once all callers have their delegates configured for parallelism, we can unleash this
/*
Action<ScenePresence> protectedAction = new Action<ScenePresence>(delegate(ScenePresence sp)
{
action(presence);
}
}
catch (Exception e)
try
{
action(sp);
}
catch (Exception e)
{
m_log.Info("[BUG] in " + m_parentScene.RegionInfo.RegionName + ": " + e.ToString());
m_log.Info("[BUG] Stack Trace: " + e.StackTrace);
}
});
Parallel.ForEach<ScenePresence>(GetScenePresences(), protectedAction);
*/
// For now, perform actiona serially
foreach (ScenePresence sp in GetScenePresences())
{
m_log.Info("[BUG] in " + m_parentScene.RegionInfo.RegionName + ": " + e.ToString());
m_log.Info("[BUG] Stack Trace: " + e.StackTrace);
try
{
action(sp);
}
catch (Exception e)
{
m_log.Info("[BUG] in " + m_parentScene.RegionInfo.RegionName + ": " + e.ToString());
m_log.Info("[BUG] Stack Trace: " + e.StackTrace);
}
}
}

View File

@@ -454,8 +454,10 @@ namespace OpenSim.Region.Framework.Scenes
ForEachCurrentScene(delegate(Scene scene)
{
List<ScenePresence> scenePresences = scene.GetScenePresences();
presences.AddRange(scenePresences);
scene.ForEachScenePresence(delegate(ScenePresence sp)
{
presences.Add(sp);
});
});
return presences;

View File

@@ -1321,11 +1321,11 @@ namespace OpenSim.Region.Framework.Scenes
if (volume < 0)
volume = 0;
List<ScenePresence> avatarts = m_parentGroup.Scene.GetAvatars();
foreach (ScenePresence p in avatarts)
m_parentGroup.Scene.ForEachScenePresence(delegate(ScenePresence sp)
{
p.ControllingClient.SendAttachedSoundGainChange(UUID, (float)volume);
}
if(!sp.IsChildAgent)
sp.ControllingClient.SendAttachedSoundGainChange(UUID, (float)volume);
});
}
/// <summary>
@@ -2609,12 +2609,13 @@ namespace OpenSim.Region.Framework.Scenes
}
}
List<ScenePresence> avatarts = m_parentGroup.Scene.GetAvatars();
foreach (ScenePresence p in avatarts)
m_parentGroup.Scene.ForEachScenePresence(delegate(ScenePresence sp)
{
if (!(Util.GetDistanceTo(p.AbsolutePosition, AbsolutePosition) >= 100))
p.ControllingClient.SendPreLoadSound(objectID, objectID, soundID);
}
if (sp.IsChildAgent)
return;
if (!(Util.GetDistanceTo(sp.AbsolutePosition, AbsolutePosition) >= 100))
sp.ControllingClient.SendPreLoadSound(objectID, objectID, soundID);
});
}
public void RemFlag(PrimFlags flag)

View File

@@ -2396,35 +2396,33 @@ namespace OpenSim.Region.Framework.Scenes
List<Vector3> CoarseLocations = new List<Vector3>();
List<UUID> AvatarUUIDs = new List<UUID>();
List<ScenePresence> avatars = m_scene.GetAvatars();
for (int i = 0; i < avatars.Count; i++)
m_scene.ForEachScenePresence(delegate(ScenePresence sp)
{
// Requested by LibOMV. Send Course Location on self.
//if (avatars[i] != this)
//{
if (avatars[i].ParentID != 0)
if (sp.IsChildAgent)
return;
if (sp.ParentID != 0)
{
// sitting avatar
SceneObjectPart sop = m_scene.GetSceneObjectPart(sp.ParentID);
if (sop != null)
{
// sitting avatar
SceneObjectPart sop = m_scene.GetSceneObjectPart(avatars[i].ParentID);
if (sop != null)
{
CoarseLocations.Add(sop.AbsolutePosition + avatars[i].m_pos);
AvatarUUIDs.Add(avatars[i].UUID);
}
else
{
// we can't find the parent.. ! arg!
CoarseLocations.Add(avatars[i].m_pos);
AvatarUUIDs.Add(avatars[i].UUID);
}
CoarseLocations.Add(sop.AbsolutePosition + sp.m_pos);
AvatarUUIDs.Add(sp.UUID);
}
else
{
CoarseLocations.Add(avatars[i].m_pos);
AvatarUUIDs.Add(avatars[i].UUID);
// we can't find the parent.. ! arg!
CoarseLocations.Add(sp.m_pos);
AvatarUUIDs.Add(sp.UUID);
}
//}
}
}
else
{
CoarseLocations.Add(sp.m_pos);
AvatarUUIDs.Add(sp.UUID);
}
});
m_controllingClient.SendCoarseLocationUpdate(AvatarUUIDs, CoarseLocations);
@@ -2498,13 +2496,15 @@ namespace OpenSim.Region.Framework.Scenes
m_perfMonMS = Util.EnvironmentTickCount();
// only send update from root agents to other clients; children are only "listening posts"
List<ScenePresence> avatars = m_scene.GetAvatars();
foreach (ScenePresence avatar in avatars)
int count = 0;
m_scene.ForEachScenePresence(delegate(ScenePresence sp)
{
SendFullUpdateToOtherClient(avatar);
}
m_scene.StatsReporter.AddAgentUpdates(avatars.Count);
if (sp.IsChildAgent)
return;
SendFullUpdateToOtherClient(sp);
++count;
});
m_scene.StatsReporter.AddAgentUpdates(count);
m_scene.StatsReporter.AddAgentTime(Util.EnvironmentTickCountSubtract(m_perfMonMS));
Animator.SendAnimPack();