LocalInterregionComms.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.Reflection;
  30. using log4net;
  31. using Nini.Config;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework.Interfaces;
  35. using OpenSim.Region.Framework.Scenes;
  36. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Interregion
  37. {
  38. public class LocalInterregionComms : ISharedRegionModule, IInterregionCommsOut, IInterregionCommsIn
  39. {
  40. private bool m_enabled = false;
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. private List<Scene> m_sceneList = new List<Scene>();
  43. #region Events
  44. public event ChildAgentUpdateReceived OnChildAgentUpdate;
  45. #endregion /* Events */
  46. #region IRegionModule
  47. public void Initialise(IConfigSource config)
  48. {
  49. if (m_sceneList.Count == 0)
  50. {
  51. IConfig startupConfig = config.Configs["Communications"];
  52. if ((startupConfig != null) && (startupConfig.GetString("InterregionComms", "RESTComms") == "LocalComms"))
  53. {
  54. m_log.Debug("[LOCAL COMMS]: Enabling InterregionComms LocalComms module");
  55. m_enabled = true;
  56. }
  57. }
  58. }
  59. public void PostInitialise()
  60. {
  61. }
  62. public void AddRegion(Scene scene)
  63. {
  64. }
  65. public void RemoveRegion(Scene scene)
  66. {
  67. if (m_enabled)
  68. {
  69. RemoveScene(scene);
  70. }
  71. }
  72. public void RegionLoaded(Scene scene)
  73. {
  74. if (m_enabled)
  75. {
  76. Init(scene);
  77. }
  78. }
  79. public void Close()
  80. {
  81. }
  82. public Type ReplaceableInterface
  83. {
  84. get { return null; }
  85. }
  86. public string Name
  87. {
  88. get { return "LocalInterregionCommsModule"; }
  89. }
  90. /// <summary>
  91. /// Can be called from other modules.
  92. /// </summary>
  93. /// <param name="scene"></param>
  94. public void RemoveScene(Scene scene)
  95. {
  96. lock (m_sceneList)
  97. {
  98. if (m_sceneList.Contains(scene))
  99. {
  100. m_sceneList.Remove(scene);
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// Can be called from other modules.
  106. /// </summary>
  107. /// <param name="scene"></param>
  108. public void Init(Scene scene)
  109. {
  110. if (!m_sceneList.Contains(scene))
  111. {
  112. lock (m_sceneList)
  113. {
  114. m_sceneList.Add(scene);
  115. if (m_enabled)
  116. scene.RegisterModuleInterface<IInterregionCommsOut>(this);
  117. scene.RegisterModuleInterface<IInterregionCommsIn>(this);
  118. }
  119. }
  120. }
  121. #endregion /* IRegionModule */
  122. #region IInterregionComms
  123. /**
  124. * Agent-related communications
  125. */
  126. public bool SendCreateChildAgent(ulong regionHandle, AgentCircuitData aCircuit, uint teleportFlags, out string reason)
  127. {
  128. foreach (Scene s in m_sceneList)
  129. {
  130. if (s.RegionInfo.RegionHandle == regionHandle)
  131. {
  132. // m_log.DebugFormat("[LOCAL COMMS]: Found region {0} to send SendCreateChildAgent", regionHandle);
  133. return s.NewUserConnection(aCircuit, teleportFlags, out reason);
  134. }
  135. }
  136. // m_log.DebugFormat("[LOCAL COMMS]: Did not find region {0} for SendCreateChildAgent", regionHandle);
  137. uint x = 0, y = 0;
  138. Utils.LongToUInts(regionHandle, out x, out y);
  139. reason = "Did not find region " + x + "-" + y;
  140. return false;
  141. }
  142. public bool SendChildAgentUpdate(ulong regionHandle, AgentData cAgentData)
  143. {
  144. foreach (Scene s in m_sceneList)
  145. {
  146. if (s.RegionInfo.RegionHandle == regionHandle)
  147. {
  148. //m_log.DebugFormat(
  149. // "[LOCAL COMMS]: Found region {0} {1} to send ChildAgentUpdate",
  150. // s.RegionInfo.RegionName, regionHandle);
  151. s.IncomingChildAgentDataUpdate(cAgentData);
  152. return true;
  153. }
  154. }
  155. // m_log.DebugFormat("[LOCAL COMMS]: Did not find region {0} for ChildAgentUpdate", regionHandle);
  156. return false;
  157. }
  158. public bool SendChildAgentUpdate(ulong regionHandle, AgentPosition cAgentData)
  159. {
  160. foreach (Scene s in m_sceneList)
  161. {
  162. if (s.RegionInfo.RegionHandle == regionHandle)
  163. {
  164. //m_log.Debug("[LOCAL COMMS]: Found region to send ChildAgentUpdate");
  165. s.IncomingChildAgentDataUpdate(cAgentData);
  166. return true;
  167. }
  168. }
  169. //m_log.Debug("[LOCAL COMMS]: region not found for ChildAgentUpdate");
  170. return false;
  171. }
  172. public bool SendRetrieveRootAgent(ulong regionHandle, UUID id, out IAgentData agent)
  173. {
  174. agent = null;
  175. foreach (Scene s in m_sceneList)
  176. {
  177. if (s.RegionInfo.RegionHandle == regionHandle)
  178. {
  179. //m_log.Debug("[LOCAL COMMS]: Found region to send ChildAgentUpdate");
  180. return s.IncomingRetrieveRootAgent(id, out agent);
  181. }
  182. }
  183. //m_log.Debug("[LOCAL COMMS]: region not found for ChildAgentUpdate");
  184. return false;
  185. }
  186. public bool SendReleaseAgent(ulong regionHandle, UUID id, string uri)
  187. {
  188. //uint x, y;
  189. //Utils.LongToUInts(regionHandle, out x, out y);
  190. //x = x / Constants.RegionSize;
  191. //y = y / Constants.RegionSize;
  192. //m_log.Debug("\n >>> Local SendReleaseAgent " + x + "-" + y);
  193. foreach (Scene s in m_sceneList)
  194. {
  195. if (s.RegionInfo.RegionHandle == regionHandle)
  196. {
  197. //m_log.Debug("[LOCAL COMMS]: Found region to SendReleaseAgent");
  198. return s.IncomingReleaseAgent(id);
  199. }
  200. }
  201. //m_log.Debug("[LOCAL COMMS]: region not found in SendReleaseAgent");
  202. return false;
  203. }
  204. public bool SendCloseAgent(ulong regionHandle, UUID id)
  205. {
  206. //uint x, y;
  207. //Utils.LongToUInts(regionHandle, out x, out y);
  208. //x = x / Constants.RegionSize;
  209. //y = y / Constants.RegionSize;
  210. //m_log.Debug("\n >>> Local SendCloseAgent " + x + "-" + y);
  211. foreach (Scene s in m_sceneList)
  212. {
  213. if (s.RegionInfo.RegionHandle == regionHandle)
  214. {
  215. //m_log.Debug("[LOCAL COMMS]: Found region to SendCloseAgent");
  216. return s.IncomingCloseAgent(id);
  217. }
  218. }
  219. //m_log.Debug("[LOCAL COMMS]: region not found in SendCloseAgent");
  220. return false;
  221. }
  222. /**
  223. * Object-related communications
  224. */
  225. public bool SendCreateObject(ulong regionHandle, SceneObjectGroup sog, bool isLocalCall)
  226. {
  227. foreach (Scene s in m_sceneList)
  228. {
  229. if (s.RegionInfo.RegionHandle == regionHandle)
  230. {
  231. //m_log.Debug("[LOCAL COMMS]: Found region to SendCreateObject");
  232. if (isLocalCall)
  233. {
  234. // We need to make a local copy of the object
  235. ISceneObject sogClone = sog.CloneForNewScene();
  236. sogClone.SetState(sog.GetStateSnapshot(), s);
  237. return s.IncomingCreateObject(sogClone);
  238. }
  239. else
  240. {
  241. // Use the object as it came through the wire
  242. return s.IncomingCreateObject(sog);
  243. }
  244. }
  245. }
  246. return false;
  247. }
  248. public bool SendCreateObject(ulong regionHandle, UUID userID, UUID itemID)
  249. {
  250. foreach (Scene s in m_sceneList)
  251. {
  252. if (s.RegionInfo.RegionHandle == regionHandle)
  253. {
  254. return s.IncomingCreateObject(userID, itemID);
  255. }
  256. }
  257. return false;
  258. }
  259. #endregion /* IInterregionComms */
  260. #region Misc
  261. public Scene GetScene(ulong regionhandle)
  262. {
  263. foreach (Scene s in m_sceneList)
  264. {
  265. if (s.RegionInfo.RegionHandle == regionhandle)
  266. return s;
  267. }
  268. // ? weird. should not happen
  269. return m_sceneList[0];
  270. }
  271. public bool IsLocalRegion(ulong regionhandle)
  272. {
  273. foreach (Scene s in m_sceneList)
  274. if (s.RegionInfo.RegionHandle == regionhandle)
  275. return true;
  276. return false;
  277. }
  278. #endregion
  279. }
  280. }