SceneCommunicationService.cs 11 KB

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