SceneCommunicationService.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.Generic;
  29. using System.Net;
  30. using System.Reflection;
  31. using System.Threading;
  32. using OpenMetaverse;
  33. using OpenMetaverse.StructuredData;
  34. using log4net;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Client;
  37. using OpenSim.Framework.Capabilities;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Services.Interfaces;
  40. using OSD = OpenMetaverse.StructuredData.OSD;
  41. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  42. namespace OpenSim.Region.Framework.Scenes
  43. {
  44. public delegate void RemoveKnownRegionsFromAvatarList(UUID avatarID, List<ulong> regionlst);
  45. /// <summary>
  46. /// Class that Region communications runs through
  47. /// </summary>
  48. public class SceneCommunicationService //one instance per region
  49. {
  50. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  51. private static readonly string LogHeader = "[SCENE COMM]";
  52. protected RegionInfo m_regionInfo;
  53. protected Scene m_scene;
  54. public void SetScene(Scene s)
  55. {
  56. m_scene = s;
  57. m_regionInfo = s.RegionInfo;
  58. }
  59. public void InformNeighborsThatRegionisUp(INeighbourService neighbourService, RegionInfo region)
  60. {
  61. //m_log.Info("[INTER]: " + debugRegionName + ": SceneCommunicationService: Sending InterRegion Notification that region is up " + region.RegionName);
  62. if (neighbourService == null)
  63. {
  64. m_log.ErrorFormat("{0} No neighbour service provided for region {1} to inform neigbhours of status", LogHeader, m_scene.Name);
  65. return;
  66. }
  67. List<GridRegion> neighbours
  68. = m_scene.GridService.GetNeighbours(m_scene.RegionInfo.ScopeID, m_scene.RegionInfo.RegionID);
  69. List<ulong> onlineNeighbours = new List<ulong>();
  70. foreach (GridRegion n in neighbours)
  71. {
  72. //m_log.DebugFormat(
  73. // "{0}: Region flags for {1} as seen by {2} are {3}",
  74. // LogHeader, n.RegionName, m_scene.Name, regionFlags != null ? regionFlags.ToString() : "not present");
  75. // Robust services before 2015-01-14 do not return the regionFlags information. In this case, we could
  76. // make a separate RegionFlags call but this would involve a network call for each neighbour.
  77. if (n.RegionFlags != null)
  78. {
  79. if ((n.RegionFlags & OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  80. onlineNeighbours.Add(n.RegionHandle);
  81. }
  82. else
  83. {
  84. onlineNeighbours.Add(n.RegionHandle);
  85. }
  86. }
  87. if(onlineNeighbours.Count > 0)
  88. {
  89. Util.FireAndForget(o =>
  90. {
  91. foreach (ulong regionhandle in onlineNeighbours)
  92. {
  93. Util.RegionHandleToRegionLoc(regionhandle, out uint rx, out uint ry);
  94. GridRegion neighbour = neighbourService.HelloNeighbour(regionhandle, region);
  95. if (neighbour != null)
  96. {
  97. m_log.DebugFormat("{0} Region {1} successfully informed neighbour {2} at {3}-{4} that it is up",
  98. LogHeader, m_scene.Name, neighbour.RegionName, rx, ry);
  99. m_scene.EventManager.TriggerOnRegionUp(neighbour);
  100. }
  101. else
  102. {
  103. m_log.WarnFormat("{0} Region {1} failed to inform neighbour at {2}-{3} that it is up.",
  104. LogHeader, m_scene.Name, rx, ry);
  105. }
  106. }
  107. });
  108. }
  109. }
  110. /// <summary>
  111. /// This informs all neighboring regions about the settings of it's child agent.
  112. /// This contains information, such as, Draw Distance, Camera location, Current Position, Current throttle settings, etc.
  113. /// </summary>
  114. public void SendChildAgentDataUpdate(AgentPosition cAgentData, ScenePresence presence)
  115. {
  116. //m_log.DebugFormat(
  117. // "[SCENE COMMUNICATION SERVICE]: Sending child agent position updates for {0} in {1}",
  118. // presence.Name, m_scene.Name);
  119. // This assumes that we know what our neighbors are.
  120. try
  121. {
  122. List<string> simulatorList = new List<string>();
  123. foreach (ulong regionHandle in presence.KnownRegionHandles)
  124. {
  125. if (regionHandle != m_regionInfo.RegionHandle)
  126. {
  127. // we only want to send one update to each simulator; the simulator will
  128. // hand it off to the regions where a child agent exists, this does assume
  129. // that the region position is cached or performance will degrade
  130. GridRegion dest = m_scene.GridService.GetRegionByHandle(UUID.Zero, regionHandle);
  131. if (dest == null)
  132. continue;
  133. if (!simulatorList.Contains(dest.ServerURI))
  134. {
  135. // we havent seen this simulator before, add it to the list
  136. // and send it an update
  137. simulatorList.Add(dest.ServerURI);
  138. m_scene.SimulationService.UpdateAgent(dest, cAgentData);
  139. }
  140. }
  141. }
  142. }
  143. catch (InvalidOperationException)
  144. {
  145. // We're ignoring a collection was modified error because this data gets old and outdated fast.
  146. }
  147. }
  148. /// <summary>
  149. /// Closes a child agents in a collection of regions. Does so asynchronously
  150. /// so that the caller doesn't wait.
  151. /// </summary>
  152. /// <param name="agentID"></param>
  153. /// <param name="regionslst"></param>
  154. public void SendCloseChildAgentConnections(UUID agentID, string auth_code, List<ulong> regionslst)
  155. {
  156. if (regionslst.Count == 0)
  157. return;
  158. // use a single thread job for all
  159. Util.FireAndForget(o =>
  160. {
  161. foreach (ulong regionHandle in regionslst)
  162. {
  163. // let's do our best, but there's not much we can do if the neighbour doesn't accept.
  164. GridRegion destination = m_scene.GridService.GetRegionByHandle(m_regionInfo.ScopeID, regionHandle);
  165. if (destination == null)
  166. {
  167. m_log.DebugFormat(
  168. "[SCENE COMMUNICATION SERVICE]: Sending close agent ID {0} FAIL, region with handle {1} not found", agentID, regionHandle);
  169. return;
  170. }
  171. m_log.DebugFormat(
  172. "[SCENE COMMUNICATION SERVICE]: Sending close agent ID {0} to {1}", agentID, destination.RegionName);
  173. m_scene.SimulationService.CloseAgent(destination, agentID, auth_code);
  174. }
  175. }, null, "SCOMM.SendCloseChildAgentConnections");
  176. }
  177. public List<GridRegion> RequestNamedRegions(string name, int maxNumber)
  178. {
  179. return m_scene.GridService.GetRegionsByName(UUID.Zero, name, maxNumber);
  180. }
  181. }
  182. }