SceneCommunicationService.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 COMMUNICATION 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. List<GridRegion> onlineNeighbours = new List<GridRegion>();
  99. foreach (GridRegion n in neighbours)
  100. {
  101. OpenSim.Framework.RegionFlags? regionFlags = n.RegionFlags;
  102. // m_log.DebugFormat(
  103. // "{0}: Region flags for {1} as seen by {2} are {3}",
  104. // LogHeader, n.RegionName, m_scene.Name, regionFlags != null ? regionFlags.ToString() : "not present");
  105. // Robust services before 2015-01-14 do not return the regionFlags information. In this case, we could
  106. // make a separate RegionFlags call but this would involve a network call for each neighbour.
  107. if (regionFlags != null)
  108. {
  109. if ((regionFlags & OpenSim.Framework.RegionFlags.RegionOnline) != 0)
  110. onlineNeighbours.Add(n);
  111. }
  112. else
  113. {
  114. onlineNeighbours.Add(n);
  115. }
  116. }
  117. m_log.DebugFormat(
  118. "{0} Informing {1} neighbours that region {2} is up",
  119. LogHeader, onlineNeighbours.Count, m_scene.Name);
  120. foreach (GridRegion n in onlineNeighbours)
  121. {
  122. InformNeighbourThatRegionUpDelegate d = InformNeighboursThatRegionIsUpAsync;
  123. d.BeginInvoke(neighbourService, region, n.RegionHandle,
  124. InformNeighborsThatRegionisUpCompleted,
  125. d);
  126. }
  127. }
  128. public delegate void SendChildAgentDataUpdateDelegate(AgentPosition cAgentData, UUID scopeID, GridRegion dest);
  129. /// <summary>
  130. /// This informs all neighboring regions about the settings of it's child agent.
  131. /// Calls an asynchronous method to do so.. so it doesn't lag the sim.
  132. ///
  133. /// This contains information, such as, Draw Distance, Camera location, Current Position, Current throttle settings, etc.
  134. ///
  135. /// </summary>
  136. private void SendChildAgentDataUpdateAsync(AgentPosition cAgentData, UUID scopeID, GridRegion dest)
  137. {
  138. //m_log.Info("[INTERGRID]: Informing neighbors about my agent in " + m_regionInfo.RegionName);
  139. try
  140. {
  141. m_scene.SimulationService.UpdateAgent(dest, cAgentData);
  142. }
  143. catch
  144. {
  145. // Ignore; we did our best
  146. }
  147. }
  148. private void SendChildAgentDataUpdateCompleted(IAsyncResult iar)
  149. {
  150. SendChildAgentDataUpdateDelegate icon = (SendChildAgentDataUpdateDelegate) iar.AsyncState;
  151. icon.EndInvoke(iar);
  152. }
  153. public void SendChildAgentDataUpdate(AgentPosition cAgentData, ScenePresence presence)
  154. {
  155. // m_log.DebugFormat(
  156. // "[SCENE COMMUNICATION SERVICE]: Sending child agent position updates for {0} in {1}",
  157. // presence.Name, m_scene.Name);
  158. // This assumes that we know what our neighbors are.
  159. try
  160. {
  161. uint x = 0, y = 0;
  162. List<string> simulatorList = new List<string>();
  163. foreach (ulong regionHandle in presence.KnownRegionHandles)
  164. {
  165. if (regionHandle != m_regionInfo.RegionHandle)
  166. {
  167. // we only want to send one update to each simulator; the simulator will
  168. // hand it off to the regions where a child agent exists, this does assume
  169. // that the region position is cached or performance will degrade
  170. Util.RegionHandleToWorldLoc(regionHandle, out x, out y);
  171. GridRegion dest = m_scene.GridService.GetRegionByPosition(UUID.Zero, (int)x, (int)y);
  172. if (dest == null)
  173. continue;
  174. if (!simulatorList.Contains(dest.ServerURI))
  175. {
  176. // we havent seen this simulator before, add it to the list
  177. // and send it an update
  178. simulatorList.Add(dest.ServerURI);
  179. // Let move this to sync. Mono definitely does not like async networking.
  180. m_scene.SimulationService.UpdateAgent(dest, cAgentData);
  181. // Leaving this here as a reminder that we tried, and it sucks.
  182. //SendChildAgentDataUpdateDelegate d = SendChildAgentDataUpdateAsync;
  183. //d.BeginInvoke(cAgentData, m_regionInfo.ScopeID, dest,
  184. // SendChildAgentDataUpdateCompleted,
  185. // d);
  186. }
  187. }
  188. }
  189. }
  190. catch (InvalidOperationException)
  191. {
  192. // We're ignoring a collection was modified error because this data gets old and outdated fast.
  193. }
  194. }
  195. /// <summary>
  196. /// Closes a child agent on a given region
  197. /// </summary>
  198. protected void SendCloseChildAgent(UUID agentID, ulong regionHandle, string auth_token)
  199. {
  200. // let's do our best, but there's not much we can do if the neighbour doesn't accept.
  201. //m_commsProvider.InterRegion.TellRegionToCloseChildConnection(regionHandle, agentID);
  202. uint x = 0, y = 0;
  203. Util.RegionHandleToWorldLoc(regionHandle, out x, out y);
  204. GridRegion destination = m_scene.GridService.GetRegionByPosition(m_regionInfo.ScopeID, (int)x, (int)y);
  205. m_log.DebugFormat(
  206. "[SCENE COMMUNICATION SERVICE]: Sending close agent ID {0} to {1}", agentID, destination.RegionName);
  207. m_scene.SimulationService.CloseAgent(destination, agentID, auth_token);
  208. }
  209. /// <summary>
  210. /// Closes a child agents in a collection of regions. Does so asynchronously
  211. /// so that the caller doesn't wait.
  212. /// </summary>
  213. /// <param name="agentID"></param>
  214. /// <param name="regionslst"></param>
  215. public void SendCloseChildAgentConnections(UUID agentID, string auth_code, List<ulong> regionslst)
  216. {
  217. foreach (ulong handle in regionslst)
  218. {
  219. // We must take a copy here since handle is acts like a reference when used in an iterator.
  220. // This leads to race conditions if directly passed to SendCloseChildAgent with more than one neighbour region.
  221. ulong handleCopy = handle;
  222. Util.FireAndForget(
  223. o => SendCloseChildAgent(agentID, handleCopy, auth_code),
  224. null,
  225. "SceneCommunicationService.SendCloseChildAgentConnections");
  226. }
  227. }
  228. public List<GridRegion> RequestNamedRegions(string name, int maxNumber)
  229. {
  230. return m_scene.GridService.GetRegionsByName(UUID.Zero, name, maxNumber);
  231. }
  232. }
  233. }