SceneCommunicationService.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.Communications;
  38. using OpenSim.Framework.Capabilities;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Services.Interfaces;
  41. using OSD = OpenMetaverse.StructuredData.OSD;
  42. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  43. namespace OpenSim.Region.Framework.Scenes
  44. {
  45. public delegate void KiPrimitiveDelegate(uint localID);
  46. public delegate void RemoveKnownRegionsFromAvatarList(UUID avatarID, List<ulong> regionlst);
  47. /// <summary>
  48. /// Class that Region communications runs through
  49. /// </summary>
  50. public class SceneCommunicationService //one instance per region
  51. {
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. protected RegionInfo m_regionInfo;
  54. protected Scene m_scene;
  55. protected List<UUID> m_agentsInTransit;
  56. public void SetScene(Scene s)
  57. {
  58. m_scene = s;
  59. m_regionInfo = s.RegionInfo;
  60. }
  61. public delegate void InformNeighbourThatRegionUpDelegate(INeighbourService nService, RegionInfo region, ulong regionhandle);
  62. private void InformNeighborsThatRegionisUpCompleted(IAsyncResult iar)
  63. {
  64. InformNeighbourThatRegionUpDelegate icon = (InformNeighbourThatRegionUpDelegate) iar.AsyncState;
  65. icon.EndInvoke(iar);
  66. }
  67. /// <summary>
  68. /// Asynchronous call to information neighbouring regions that this region is up
  69. /// </summary>
  70. /// <param name="region"></param>
  71. /// <param name="regionhandle"></param>
  72. private void InformNeighboursThatRegionIsUpAsync(INeighbourService neighbourService, RegionInfo region, ulong regionhandle)
  73. {
  74. uint x = 0, y = 0;
  75. Utils.LongToUInts(regionhandle, out x, out y);
  76. GridRegion neighbour = null;
  77. if (neighbourService != null)
  78. neighbour = neighbourService.HelloNeighbour(regionhandle, region);
  79. else
  80. m_log.DebugFormat("[SCS]: No neighbour service provided for informing neigbhours of this region");
  81. if (neighbour != null)
  82. {
  83. m_log.DebugFormat("[INTERGRID]: Successfully informed neighbour {0}-{1} that I'm here", x / Constants.RegionSize, y / Constants.RegionSize);
  84. m_scene.EventManager.TriggerOnRegionUp(neighbour);
  85. }
  86. else
  87. {
  88. m_log.InfoFormat("[INTERGRID]: Failed to inform neighbour {0}-{1} that I'm here.", x / Constants.RegionSize, y / Constants.RegionSize);
  89. }
  90. }
  91. public void InformNeighborsThatRegionisUp(INeighbourService neighbourService, RegionInfo region)
  92. {
  93. //m_log.Info("[INTER]: " + debugRegionName + ": SceneCommunicationService: Sending InterRegion Notification that region is up " + region.RegionName);
  94. List<GridRegion> neighbours = m_scene.GridService.GetNeighbours(m_scene.RegionInfo.ScopeID, m_scene.RegionInfo.RegionID);
  95. m_log.DebugFormat("[INTERGRID]: Informing {0} neighbours that this region is up", neighbours.Count);
  96. foreach (GridRegion n in neighbours)
  97. {
  98. InformNeighbourThatRegionUpDelegate d = InformNeighboursThatRegionIsUpAsync;
  99. d.BeginInvoke(neighbourService, region, n.RegionHandle,
  100. InformNeighborsThatRegionisUpCompleted,
  101. d);
  102. }
  103. }
  104. public delegate void SendChildAgentDataUpdateDelegate(AgentPosition cAgentData, UUID scopeID, GridRegion dest);
  105. /// <summary>
  106. /// This informs all neighboring regions about the settings of it's child agent.
  107. /// Calls an asynchronous method to do so.. so it doesn't lag the sim.
  108. ///
  109. /// This contains information, such as, Draw Distance, Camera location, Current Position, Current throttle settings, etc.
  110. ///
  111. /// </summary>
  112. private void SendChildAgentDataUpdateAsync(AgentPosition cAgentData, UUID scopeID, GridRegion dest)
  113. {
  114. //m_log.Info("[INTERGRID]: Informing neighbors about my agent in " + m_regionInfo.RegionName);
  115. try
  116. {
  117. m_scene.SimulationService.UpdateAgent(dest, cAgentData);
  118. }
  119. catch
  120. {
  121. // Ignore; we did our best
  122. }
  123. }
  124. private void SendChildAgentDataUpdateCompleted(IAsyncResult iar)
  125. {
  126. SendChildAgentDataUpdateDelegate icon = (SendChildAgentDataUpdateDelegate) iar.AsyncState;
  127. icon.EndInvoke(iar);
  128. }
  129. public void SendChildAgentDataUpdate(AgentPosition cAgentData, ScenePresence presence)
  130. {
  131. // This assumes that we know what our neighbors are.
  132. try
  133. {
  134. uint x = 0, y = 0;
  135. List<string> simulatorList = new List<string>();
  136. foreach (ulong regionHandle in presence.KnownChildRegionHandles)
  137. {
  138. if (regionHandle != m_regionInfo.RegionHandle)
  139. {
  140. // we only want to send one update to each simulator; the simulator will
  141. // hand it off to the regions where a child agent exists, this does assume
  142. // that the region position is cached or performance will degrade
  143. Utils.LongToUInts(regionHandle, out x, out y);
  144. GridRegion dest = m_scene.GridService.GetRegionByPosition(UUID.Zero, (int)x, (int)y);
  145. if (! simulatorList.Contains(dest.ServerURI))
  146. {
  147. // we havent seen this simulator before, add it to the list
  148. // and send it an update
  149. simulatorList.Add(dest.ServerURI);
  150. SendChildAgentDataUpdateDelegate d = SendChildAgentDataUpdateAsync;
  151. d.BeginInvoke(cAgentData, m_regionInfo.ScopeID, dest,
  152. SendChildAgentDataUpdateCompleted,
  153. d);
  154. }
  155. }
  156. }
  157. }
  158. catch (InvalidOperationException)
  159. {
  160. // We're ignoring a collection was modified error because this data gets old and outdated fast.
  161. }
  162. }
  163. /// <summary>
  164. /// Closes a child agent on a given region
  165. /// </summary>
  166. protected void SendCloseChildAgent(UUID agentID, ulong regionHandle)
  167. {
  168. // let's do our best, but there's not much we can do if the neighbour doesn't accept.
  169. //m_commsProvider.InterRegion.TellRegionToCloseChildConnection(regionHandle, agentID);
  170. uint x = 0, y = 0;
  171. Utils.LongToUInts(regionHandle, out x, out y);
  172. GridRegion destination = m_scene.GridService.GetRegionByPosition(m_regionInfo.ScopeID, (int)x, (int)y);
  173. m_log.DebugFormat(
  174. "[INTERGRID]: Sending close agent {0} to region at {1}-{2}",
  175. agentID, destination.RegionCoordX, destination.RegionCoordY);
  176. m_scene.SimulationService.CloseAgent(destination, agentID);
  177. }
  178. /// <summary>
  179. /// Closes a child agents in a collection of regions. Does so asynchronously
  180. /// so that the caller doesn't wait.
  181. /// </summary>
  182. /// <param name="agentID"></param>
  183. /// <param name="regionslst"></param>
  184. public void SendCloseChildAgentConnections(UUID agentID, List<ulong> regionslst)
  185. {
  186. Util.FireAndForget(delegate
  187. {
  188. foreach (ulong handle in regionslst)
  189. {
  190. SendCloseChildAgent(agentID, handle);
  191. }
  192. });
  193. }
  194. public List<GridRegion> RequestNamedRegions(string name, int maxNumber)
  195. {
  196. return m_scene.GridService.GetRegionsByName(UUID.Zero, name, maxNumber);
  197. }
  198. }
  199. }