SimulatorFeaturesHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.IO;
  29. using System.Reflection;
  30. using System.Text;
  31. using System.Collections.Generic;
  32. using System.Threading;
  33. using OpenMetaverse;
  34. using OpenMetaverse.StructuredData;
  35. using OpenSim;
  36. using OpenSim.Region;
  37. using OpenSim.Region.Framework;
  38. using OpenSim.Region.Framework.Scenes;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Framework;
  41. using OpenSim.Services.Interfaces;
  42. //using OpenSim.Framework.Capabilities;
  43. using Nini.Config;
  44. using log4net;
  45. using OSDMap = OpenMetaverse.StructuredData.OSDMap;
  46. using TeleportFlags = OpenSim.Framework.Constants.TeleportFlags;
  47. namespace OpenSim.Region.OptionalModules.ViewerSupport
  48. {
  49. public class SimulatorFeaturesHelper
  50. {
  51. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private IEntityTransferModule m_TransferModule;
  53. private Scene m_scene;
  54. private struct RegionSend {
  55. public UUID region;
  56. public bool send;
  57. };
  58. // Using a static cache so that we don't have to perform the time-consuming tests
  59. // in ShouldSend on Extra SimFeatures that go on the same response but come from
  60. // different modules.
  61. // This cached is indexed on the agentID and maps to a list of regions
  62. private static ExpiringCache<UUID, List<RegionSend>> m_Cache = new ExpiringCache<UUID, List<RegionSend>>();
  63. private const double TIMEOUT = 1.0; // time in cache
  64. public SimulatorFeaturesHelper(Scene scene, IEntityTransferModule et)
  65. {
  66. m_scene = scene;
  67. m_TransferModule = et;
  68. }
  69. public bool ShouldSend(UUID agentID)
  70. {
  71. List<RegionSend> rsendlist;
  72. RegionSend rsend;
  73. if (m_Cache.TryGetValue(agentID, out rsendlist))
  74. {
  75. rsend = rsendlist.Find(r => r.region == m_scene.RegionInfo.RegionID);
  76. if (rsend.region != UUID.Zero) // Found it
  77. {
  78. return rsend.send;
  79. }
  80. }
  81. // Relatively complex logic for deciding whether to send the extra SimFeature or not.
  82. // This is because the viewer calls this cap to all sims that it knows about,
  83. // including the departing sims and non-neighbors (those that are cached).
  84. rsend.region = m_scene.RegionInfo.RegionID;
  85. rsend.send = false;
  86. IClientAPI client = null;
  87. int counter = 200;
  88. // Let's wait a little to see if we get a client here
  89. while (!m_scene.TryGetClient(agentID, out client) && counter-- > 0)
  90. Thread.Sleep(50);
  91. if (client != null)
  92. {
  93. ScenePresence sp = WaitGetScenePresence(agentID);
  94. if (sp != null)
  95. {
  96. // On the receiving region, the call to this cap may arrive before
  97. // the agent is root. Make sure we only proceed from here when the agent
  98. // has been made root
  99. counter = 200;
  100. while ((sp.IsInTransit || sp.IsChildAgent) && counter-- > 0)
  101. {
  102. Thread.Sleep(50);
  103. }
  104. // The viewer calls this cap on the departing sims too. Make sure
  105. // that we only proceed after the agent is not in transit anymore.
  106. // The agent must be root and not going anywhere
  107. if (!sp.IsChildAgent && !m_TransferModule.IsInTransit(agentID))
  108. rsend.send = true;
  109. }
  110. }
  111. //else
  112. // m_log.DebugFormat("[XXX]: client is null");
  113. if (rsendlist == null)
  114. {
  115. rsendlist = new List<RegionSend>();
  116. m_Cache.AddOrUpdate(agentID, rsendlist, TIMEOUT);
  117. }
  118. rsendlist.Add(rsend);
  119. return rsend.send;
  120. }
  121. public int UserLevel(UUID agentID)
  122. {
  123. int level = 0;
  124. UserAccount account = m_scene.UserAccountService.GetUserAccount(m_scene.RegionInfo.ScopeID, agentID);
  125. if (account != null)
  126. level = account.UserLevel;
  127. return level;
  128. }
  129. protected virtual ScenePresence WaitGetScenePresence(UUID agentID)
  130. {
  131. int ntimes = 20;
  132. ScenePresence sp = null;
  133. while ((sp = m_scene.GetScenePresence(agentID)) == null && (ntimes-- > 0))
  134. Thread.Sleep(1000);
  135. if (sp == null)
  136. m_log.WarnFormat(
  137. "[XXX]: Did not find presence with id {0} in {1} before timeout",
  138. agentID, m_scene.RegionInfo.RegionName);
  139. else
  140. {
  141. ntimes = 10;
  142. while (sp.IsInTransit && (ntimes-- > 0))
  143. Thread.Sleep(1000);
  144. }
  145. return sp;
  146. }
  147. }
  148. }