SceneCommunicationService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 string LogHeader = "[SCENE COMMUNICATION SERVICE]";
  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 delegate void InformNeighbourThatRegionUpDelegate(INeighbourService nService, RegionInfo region, ulong regionhandle);
  60. private void InformNeighborsThatRegionisUpCompleted(IAsyncResult iar)
  61. {
  62. InformNeighbourThatRegionUpDelegate icon = (InformNeighbourThatRegionUpDelegate)iar.AsyncState;
  63. icon.EndInvoke(iar);
  64. }
  65. /// <summary>
  66. /// Asynchronous call to information neighbouring regions that this region is up
  67. /// </summary>
  68. /// <param name="region"></param>
  69. /// <param name="regionhandle"></param>
  70. private void InformNeighboursThatRegionIsUpAsync(INeighbourService neighbourService, RegionInfo region, ulong regionhandle)
  71. {
  72. uint x = 0, y = 0;
  73. Utils.LongToUInts(regionhandle, out x, out y);
  74. GridRegion neighbour = null;
  75. if (neighbourService != null)
  76. neighbour = neighbourService.HelloNeighbour(regionhandle, region);
  77. else
  78. m_log.ErrorFormat("{0} No neighbour service provided for region {1} to inform neigbhours of status", LogHeader, m_scene.Name);
  79. if (neighbour != null)
  80. {
  81. m_log.DebugFormat("{0} Region {1} successfully informed neighbour {2} at {3}-{4} that it is up",
  82. LogHeader, m_scene.Name, neighbour.RegionName, Util.WorldToRegionLoc(x), Util.WorldToRegionLoc(y));
  83. m_scene.EventManager.TriggerOnRegionUp(neighbour);
  84. }
  85. else
  86. {
  87. m_log.WarnFormat(
  88. "[SCENE COMMUNICATION SERVICE]: Region {0} failed to inform neighbour at {1}-{2} that it is up.",
  89. m_scene.Name, Util.WorldToRegionLoc(x), Util.WorldToRegionLoc(y));
  90. }
  91. }
  92. public void InformNeighborsThatRegionisUp(INeighbourService neighbourService, RegionInfo region)
  93. {
  94. //m_log.Info("[INTER]: " + debugRegionName + ": SceneCommunicationService: Sending InterRegion Notification that region is up " + region.RegionName);
  95. List<GridRegion> neighbours
  96. = m_scene.GridService.GetNeighbours(m_scene.RegionInfo.ScopeID, m_scene.RegionInfo.RegionID);
  97. List<GridRegion> onlineNeighbours = new List<GridRegion>();
  98. foreach (GridRegion n in neighbours)
  99. {
  100. OpenSim.Framework.RegionFlags? regionFlags = n.RegionFlags;
  101. // m_log.DebugFormat(
  102. // "{0}: Region flags for {1} as seen by {2} are {3}",
  103. // LogHeader, n.RegionName, m_scene.Name, regionFlags != null ? regionFlags.ToString() : "not present");
  104. // Robust services before 2015-01-14 do not return the regionFlags information. In this case, we could
  105. // make a separate RegionFlags call but this would involve a network call for each neighbour.
  106. if (regionFlags != null)
  107. {
  108. if ((regionFlags & OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  109. onlineNeighbours.Add(n);
  110. }
  111. else
  112. {
  113. onlineNeighbours.Add(n);
  114. }
  115. }
  116. m_log.DebugFormat(
  117. "{0} Informing {1} neighbours that region {2} is up",
  118. LogHeader, onlineNeighbours.Count, m_scene.Name);
  119. foreach (GridRegion n in onlineNeighbours)
  120. {
  121. InformNeighbourThatRegionUpDelegate d = InformNeighboursThatRegionIsUpAsync;
  122. d.BeginInvoke(neighbourService, region, n.RegionHandle,
  123. InformNeighborsThatRegionisUpCompleted,
  124. d);
  125. }
  126. }
  127. public delegate void SendChildAgentDataUpdateDelegate(AgentPosition cAgentData, UUID scopeID, GridRegion dest);
  128. /// <summary>
  129. /// This informs all neighboring regions about the settings of it's child agent.
  130. /// Calls an asynchronous method to do so.. so it doesn't lag the sim.
  131. ///
  132. /// This contains information, such as, Draw Distance, Camera location, Current Position, Current throttle settings, etc.
  133. ///
  134. /// </summary>
  135. private void SendChildAgentDataUpdateAsync(AgentPosition cAgentData, UUID scopeID, GridRegion dest)
  136. {
  137. //m_log.Info("[INTERGRID]: Informing neighbors about my agent in " + m_regionInfo.RegionName);
  138. try
  139. {
  140. m_scene.SimulationService.UpdateAgent(dest, cAgentData);
  141. }
  142. catch
  143. {
  144. // Ignore; we did our best
  145. }
  146. }
  147. private void SendChildAgentDataUpdateCompleted(IAsyncResult iar)
  148. {
  149. SendChildAgentDataUpdateDelegate icon = (SendChildAgentDataUpdateDelegate)iar.AsyncState;
  150. icon.EndInvoke(iar);
  151. }
  152. public void SendChildAgentDataUpdate(AgentPosition cAgentData, ScenePresence presence)
  153. {
  154. // m_log.DebugFormat(
  155. // "[SCENE COMMUNICATION SERVICE]: Sending child agent position updates for {0} in {1}",
  156. // presence.Name, m_scene.Name);
  157. // This assumes that we know what our neighbors are.
  158. try
  159. {
  160. uint x = 0, y = 0;
  161. List<string> simulatorList = new List<string>();
  162. foreach (ulong regionHandle in presence.KnownRegionHandles)
  163. {
  164. if (regionHandle != m_regionInfo.RegionHandle)
  165. {
  166. // we only want to send one update to each simulator; the simulator will
  167. // hand it off to the regions where a child agent exists, this does assume
  168. // that the region position is cached or performance will degrade
  169. Util.RegionHandleToWorldLoc(regionHandle, out x, out y);
  170. GridRegion dest = m_scene.GridService.GetRegionByPosition(UUID.Zero, (int)x, (int)y);
  171. if (dest == null)
  172. continue;
  173. if (!simulatorList.Contains(dest.ServerURI))
  174. {
  175. // we havent seen this simulator before, add it to the list
  176. // and send it an update
  177. simulatorList.Add(dest.ServerURI);
  178. // Let move this to sync. Mono definitely does not like async networking.
  179. m_scene.SimulationService.UpdateAgent(dest, cAgentData);
  180. // Leaving this here as a reminder that we tried, and it sucks.
  181. //SendChildAgentDataUpdateDelegate d = SendChildAgentDataUpdateAsync;
  182. //d.BeginInvoke(cAgentData, m_regionInfo.ScopeID, dest,
  183. // SendChildAgentDataUpdateCompleted,
  184. // d);
  185. }
  186. }
  187. }
  188. }
  189. catch (InvalidOperationException)
  190. {
  191. // We're ignoring a collection was modified error because this data gets old and outdated fast.
  192. }
  193. }
  194. public delegate void SendCloseChildAgentDelegate(UUID agentID, ulong regionHandle);
  195. /// <summary>
  196. /// This Closes child agents on neighboring regions
  197. /// Calls an asynchronous method to do so.. so it doesn't lag the sim.
  198. /// </summary>
  199. protected void SendCloseChildAgent(UUID agentID, ulong regionHandle, string auth_token)
  200. {
  201. // let's do our best, but there's not much we can do if the neighbour doesn't accept.
  202. //m_commsProvider.InterRegion.TellRegionToCloseChildConnection(regionHandle, agentID);
  203. uint x = 0, y = 0;
  204. Util.RegionHandleToWorldLoc(regionHandle, out x, out y);
  205. GridRegion destination = m_scene.GridService.GetRegionByPosition(m_regionInfo.ScopeID, (int)x, (int)y);
  206. if (destination == null)
  207. {
  208. m_log.DebugFormat(
  209. "[SCENE COMMUNICATION SERVICE]: Sending close agent ID {0} FAIL, region with handle {1} not found", agentID, regionHandle);
  210. return;
  211. }
  212. m_log.DebugFormat(
  213. "[SCENE COMMUNICATION SERVICE]: Sending close agent ID {0} to {1}", agentID, destination.RegionName);
  214. m_scene.SimulationService.CloseAgent(destination, agentID, auth_token);
  215. }
  216. /// <summary>
  217. /// Closes a child agents in a collection of regions. Does so asynchronously
  218. /// so that the caller doesn't wait.
  219. /// </summary>
  220. /// <param name="agentID"></param>
  221. /// <param name="regionslst"></param>
  222. public void SendCloseChildAgentConnections(UUID agentID, string auth_code, List<ulong> regionslst)
  223. {
  224. if (regionslst.Count == 0)
  225. return;
  226. // use a single thread job for all
  227. Util.FireAndForget(o =>
  228. {
  229. foreach (ulong handle in regionslst)
  230. {
  231. SendCloseChildAgent(agentID, handle, auth_code);
  232. }
  233. }, null, "SceneCommunicationService.SendCloseChildAgentConnections");
  234. }
  235. public List<GridRegion> RequestNamedRegions(string name, int maxNumber)
  236. {
  237. return m_scene.GridService.GetRegionsByName(UUID.Zero, name, maxNumber);
  238. }
  239. }
  240. }