SceneCommunicationService.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 RegionCommsListener regionCommsHost;
  56. protected List<UUID> m_agentsInTransit;
  57. /// <summary>
  58. /// An agent is crossing into this region
  59. /// </summary>
  60. public event AgentCrossing OnAvatarCrossingIntoRegion;
  61. /// <summary>
  62. /// A user will arrive shortly, set up appropriate credentials so it can connect
  63. /// </summary>
  64. // public event ExpectUserDelegate OnExpectUser;
  65. /// <summary>
  66. /// A Prim will arrive shortly
  67. /// </summary>
  68. public event CloseAgentConnection OnCloseAgentConnection;
  69. /// <summary>
  70. /// A new prim has arrived
  71. /// </summary>
  72. // public event PrimCrossing OnPrimCrossingIntoRegion;
  73. ///// <summary>
  74. ///// A New Region is up and available
  75. ///// </summary>
  76. //public event RegionUp OnRegionUp;
  77. /// <summary>
  78. /// We have a child agent for this avatar and we're getting a status update about it
  79. /// </summary>
  80. // public event ChildAgentUpdate OnChildAgentUpdate;
  81. //public event RemoveKnownRegionsFromAvatarList OnRemoveKnownRegionFromAvatar;
  82. /// <summary>
  83. /// Time to log one of our users off. Grid Service sends this mostly
  84. /// </summary>
  85. public event LogOffUser OnLogOffUser;
  86. /// <summary>
  87. /// A region wants land data from us!
  88. /// </summary>
  89. public event GetLandData OnGetLandData;
  90. // private AgentCrossing handlerAvatarCrossingIntoRegion = null; // OnAvatarCrossingIntoRegion;
  91. // private ExpectUserDelegate handlerExpectUser = null; // OnExpectUser;
  92. // private CloseAgentConnection handlerCloseAgentConnection = null; // OnCloseAgentConnection;
  93. // private PrimCrossing handlerPrimCrossingIntoRegion = null; // OnPrimCrossingIntoRegion;
  94. //private RegionUp handlerRegionUp = null; // OnRegionUp;
  95. // private ChildAgentUpdate handlerChildAgentUpdate = null; // OnChildAgentUpdate;
  96. //private RemoveKnownRegionsFromAvatarList handlerRemoveKnownRegionFromAvatar = null; // OnRemoveKnownRegionFromAvatar;
  97. // private LogOffUser handlerLogOffUser = null;
  98. // private GetLandData handlerGetLandData = null; // OnGetLandData
  99. public KiPrimitiveDelegate KiPrimitive;
  100. public SceneCommunicationService()
  101. {
  102. }
  103. public void SetScene(Scene s)
  104. {
  105. m_scene = s;
  106. m_regionInfo = s.RegionInfo;
  107. }
  108. /// <summary>
  109. /// Register a region with the grid
  110. /// </summary>
  111. /// <param name="regionInfos"></param>
  112. /// <exception cref="System.Exception">Thrown if region registration fails.</exception>
  113. public void RegisterRegion(IInterregionCommsOut comms_out, RegionInfo regionInfos)
  114. {
  115. }
  116. /// <summary>
  117. /// This region is shutting down, de-register all events!
  118. /// De-Register region from Grid!
  119. /// </summary>
  120. public void Close()
  121. {
  122. }
  123. public delegate void InformNeighbourThatRegionUpDelegate(INeighbourService nService, RegionInfo region, ulong regionhandle);
  124. private void InformNeighborsThatRegionisUpCompleted(IAsyncResult iar)
  125. {
  126. InformNeighbourThatRegionUpDelegate icon = (InformNeighbourThatRegionUpDelegate) iar.AsyncState;
  127. icon.EndInvoke(iar);
  128. }
  129. /// <summary>
  130. /// Asynchronous call to information neighbouring regions that this region is up
  131. /// </summary>
  132. /// <param name="region"></param>
  133. /// <param name="regionhandle"></param>
  134. private void InformNeighboursThatRegionIsUpAsync(INeighbourService neighbourService, RegionInfo region, ulong regionhandle)
  135. {
  136. uint x = 0, y = 0;
  137. Utils.LongToUInts(regionhandle, out x, out y);
  138. GridRegion neighbour = null;
  139. if (neighbourService != null)
  140. neighbour = neighbourService.HelloNeighbour(regionhandle, region);
  141. else
  142. m_log.DebugFormat("[SCS]: No neighbour service provided for informing neigbhours of this region");
  143. if (neighbour != null)
  144. {
  145. m_log.DebugFormat("[INTERGRID]: Successfully informed neighbour {0}-{1} that I'm here", x / Constants.RegionSize, y / Constants.RegionSize);
  146. m_scene.EventManager.TriggerOnRegionUp(neighbour);
  147. }
  148. else
  149. {
  150. m_log.InfoFormat("[INTERGRID]: Failed to inform neighbour {0}-{1} that I'm here.", x / Constants.RegionSize, y / Constants.RegionSize);
  151. }
  152. }
  153. public void InformNeighborsThatRegionisUp(INeighbourService neighbourService, RegionInfo region)
  154. {
  155. //m_log.Info("[INTER]: " + debugRegionName + ": SceneCommunicationService: Sending InterRegion Notification that region is up " + region.RegionName);
  156. List<GridRegion> neighbours = m_scene.GridService.GetNeighbours(m_scene.RegionInfo.ScopeID, m_scene.RegionInfo.RegionID);
  157. m_log.DebugFormat("[INTERGRID]: Informing {0} neighbours that this region is up", neighbours.Count);
  158. foreach (GridRegion n in neighbours)
  159. {
  160. InformNeighbourThatRegionUpDelegate d = InformNeighboursThatRegionIsUpAsync;
  161. d.BeginInvoke(neighbourService, region, n.RegionHandle,
  162. InformNeighborsThatRegionisUpCompleted,
  163. d);
  164. }
  165. }
  166. public delegate void SendChildAgentDataUpdateDelegate(AgentPosition cAgentData, UUID scopeID, GridRegion dest);
  167. /// <summary>
  168. /// This informs all neighboring regions about the settings of it's child agent.
  169. /// Calls an asynchronous method to do so.. so it doesn't lag the sim.
  170. ///
  171. /// This contains information, such as, Draw Distance, Camera location, Current Position, Current throttle settings, etc.
  172. ///
  173. /// </summary>
  174. private void SendChildAgentDataUpdateAsync(AgentPosition cAgentData, UUID scopeID, GridRegion dest)
  175. {
  176. //m_log.Info("[INTERGRID]: Informing neighbors about my agent in " + m_regionInfo.RegionName);
  177. try
  178. {
  179. m_scene.SimulationService.UpdateAgent(dest, cAgentData);
  180. }
  181. catch
  182. {
  183. // Ignore; we did our best
  184. }
  185. }
  186. private void SendChildAgentDataUpdateCompleted(IAsyncResult iar)
  187. {
  188. SendChildAgentDataUpdateDelegate icon = (SendChildAgentDataUpdateDelegate) iar.AsyncState;
  189. icon.EndInvoke(iar);
  190. }
  191. public void SendChildAgentDataUpdate(AgentPosition cAgentData, ScenePresence presence)
  192. {
  193. // This assumes that we know what our neighbors are.
  194. try
  195. {
  196. uint x = 0, y = 0;
  197. List<string> simulatorList = new List<string>();
  198. foreach (ulong regionHandle in presence.KnownChildRegionHandles)
  199. {
  200. if (regionHandle != m_regionInfo.RegionHandle)
  201. {
  202. // we only want to send one update to each simulator; the simulator will
  203. // hand it off to the regions where a child agent exists, this does assume
  204. // that the region position is cached or performance will degrade
  205. Utils.LongToUInts(regionHandle, out x, out y);
  206. GridRegion dest = m_scene.GridService.GetRegionByPosition(UUID.Zero, (int)x, (int)y);
  207. if (! simulatorList.Contains(dest.ServerURI))
  208. {
  209. // we havent seen this simulator before, add it to the list
  210. // and send it an update
  211. simulatorList.Add(dest.ServerURI);
  212. SendChildAgentDataUpdateDelegate d = SendChildAgentDataUpdateAsync;
  213. d.BeginInvoke(cAgentData, m_regionInfo.ScopeID, dest,
  214. SendChildAgentDataUpdateCompleted,
  215. d);
  216. }
  217. }
  218. }
  219. }
  220. catch (InvalidOperationException)
  221. {
  222. // We're ignoring a collection was modified error because this data gets old and outdated fast.
  223. }
  224. }
  225. //public delegate void SendCloseChildAgentDelegate(UUID agentID, ulong regionHandle);
  226. //private void SendCloseChildAgentCompleted(IAsyncResult iar)
  227. //{
  228. // SendCloseChildAgentDelegate icon = (SendCloseChildAgentDelegate)iar.AsyncState;
  229. // icon.EndInvoke(iar);
  230. //}
  231. /// <summary>
  232. /// Closes a child agent on a given region
  233. /// </summary>
  234. protected void SendCloseChildAgent(UUID agentID, ulong regionHandle)
  235. {
  236. m_log.Debug("[INTERGRID]: Sending close agent to " + regionHandle);
  237. // let's do our best, but there's not much we can do if the neighbour doesn't accept.
  238. //m_commsProvider.InterRegion.TellRegionToCloseChildConnection(regionHandle, agentID);
  239. uint x = 0, y = 0;
  240. Utils.LongToUInts(regionHandle, out x, out y);
  241. GridRegion destination = m_scene.GridService.GetRegionByPosition(m_regionInfo.ScopeID, (int)x, (int)y);
  242. m_scene.SimulationService.CloseAgent(destination, agentID);
  243. }
  244. /// <summary>
  245. /// Closes a child agents in a collection of regions. Does so asynchronously
  246. /// so that the caller doesn't wait.
  247. /// </summary>
  248. /// <param name="agentID"></param>
  249. /// <param name="regionslst"></param>
  250. public void SendCloseChildAgentConnections(UUID agentID, List<ulong> regionslst)
  251. {
  252. Util.FireAndForget(delegate
  253. {
  254. foreach (ulong handle in regionslst)
  255. {
  256. SendCloseChildAgent(agentID, handle);
  257. }
  258. });
  259. }
  260. public List<GridRegion> RequestNamedRegions(string name, int maxNumber)
  261. {
  262. return m_scene.GridService.GetRegionsByName(UUID.Zero, name, maxNumber);
  263. }
  264. }
  265. }