SceneCommunicationService.cs 11 KB

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