LocalInterregionComms.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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, 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, out reason);
  134. }
  135. }
  136. // m_log.DebugFormat("[LOCAL COMMS]: Did not find region {0} for SendCreateChildAgent", regionHandle);
  137. reason = "Did not find region.";
  138. return false;
  139. }
  140. public bool SendChildAgentUpdate(ulong regionHandle, AgentData cAgentData)
  141. {
  142. foreach (Scene s in m_sceneList)
  143. {
  144. if (s.RegionInfo.RegionHandle == regionHandle)
  145. {
  146. //m_log.DebugFormat(
  147. // "[LOCAL COMMS]: Found region {0} {1} to send ChildAgentUpdate",
  148. // s.RegionInfo.RegionName, regionHandle);
  149. s.IncomingChildAgentDataUpdate(cAgentData);
  150. return true;
  151. }
  152. }
  153. // m_log.DebugFormat("[LOCAL COMMS]: Did not find region {0} for ChildAgentUpdate", regionHandle);
  154. return false;
  155. }
  156. public bool SendChildAgentUpdate(ulong regionHandle, AgentPosition cAgentData)
  157. {
  158. foreach (Scene s in m_sceneList)
  159. {
  160. if (s.RegionInfo.RegionHandle == regionHandle)
  161. {
  162. //m_log.Debug("[LOCAL COMMS]: Found region to send ChildAgentUpdate");
  163. s.IncomingChildAgentDataUpdate(cAgentData);
  164. return true;
  165. }
  166. }
  167. //m_log.Debug("[LOCAL COMMS]: region not found for ChildAgentUpdate");
  168. return false;
  169. }
  170. public bool SendRetrieveRootAgent(ulong regionHandle, UUID id, out IAgentData agent)
  171. {
  172. agent = null;
  173. foreach (Scene s in m_sceneList)
  174. {
  175. if (s.RegionInfo.RegionHandle == regionHandle)
  176. {
  177. //m_log.Debug("[LOCAL COMMS]: Found region to send ChildAgentUpdate");
  178. return s.IncomingRetrieveRootAgent(id, out agent);
  179. }
  180. }
  181. //m_log.Debug("[LOCAL COMMS]: region not found for ChildAgentUpdate");
  182. return false;
  183. }
  184. public bool SendReleaseAgent(ulong regionHandle, UUID id, string uri)
  185. {
  186. //uint x, y;
  187. //Utils.LongToUInts(regionHandle, out x, out y);
  188. //x = x / Constants.RegionSize;
  189. //y = y / Constants.RegionSize;
  190. //m_log.Debug("\n >>> Local SendReleaseAgent " + x + "-" + y);
  191. foreach (Scene s in m_sceneList)
  192. {
  193. if (s.RegionInfo.RegionHandle == regionHandle)
  194. {
  195. //m_log.Debug("[LOCAL COMMS]: Found region to SendReleaseAgent");
  196. return s.IncomingReleaseAgent(id);
  197. }
  198. }
  199. //m_log.Debug("[LOCAL COMMS]: region not found in SendReleaseAgent");
  200. return false;
  201. }
  202. public bool SendCloseAgent(ulong regionHandle, UUID id)
  203. {
  204. //uint x, y;
  205. //Utils.LongToUInts(regionHandle, out x, out y);
  206. //x = x / Constants.RegionSize;
  207. //y = y / Constants.RegionSize;
  208. //m_log.Debug("\n >>> Local SendCloseAgent " + x + "-" + y);
  209. foreach (Scene s in m_sceneList)
  210. {
  211. if (s.RegionInfo.RegionHandle == regionHandle)
  212. {
  213. //m_log.Debug("[LOCAL COMMS]: Found region to SendCloseAgent");
  214. return s.IncomingCloseAgent(id);
  215. }
  216. }
  217. //m_log.Debug("[LOCAL COMMS]: region not found in SendCloseAgent");
  218. return false;
  219. }
  220. /**
  221. * Object-related communications
  222. */
  223. public bool SendCreateObject(ulong regionHandle, SceneObjectGroup sog, bool isLocalCall)
  224. {
  225. foreach (Scene s in m_sceneList)
  226. {
  227. if (s.RegionInfo.RegionHandle == regionHandle)
  228. {
  229. //m_log.Debug("[LOCAL COMMS]: Found region to SendCreateObject");
  230. if (isLocalCall)
  231. {
  232. // We need to make a local copy of the object
  233. ISceneObject sogClone = sog.CloneForNewScene();
  234. sogClone.SetState(sog.GetStateSnapshot(),
  235. s.RegionInfo.RegionID);
  236. return s.IncomingCreateObject(sogClone);
  237. }
  238. else
  239. {
  240. // Use the object as it came through the wire
  241. return s.IncomingCreateObject(sog);
  242. }
  243. }
  244. }
  245. return false;
  246. }
  247. public bool SendCreateObject(ulong regionHandle, UUID userID, UUID itemID)
  248. {
  249. foreach (Scene s in m_sceneList)
  250. {
  251. if (s.RegionInfo.RegionHandle == regionHandle)
  252. {
  253. return s.IncomingCreateObject(userID, itemID);
  254. }
  255. }
  256. return false;
  257. }
  258. #endregion /* IInterregionComms */
  259. #region Misc
  260. public UUID GetRegionID(ulong regionhandle)
  261. {
  262. foreach (Scene s in m_sceneList)
  263. {
  264. if (s.RegionInfo.RegionHandle == regionhandle)
  265. return s.RegionInfo.RegionID;
  266. }
  267. // ? weird. should not happen
  268. return m_sceneList[0].RegionInfo.RegionID;
  269. }
  270. public bool IsLocalRegion(ulong regionhandle)
  271. {
  272. foreach (Scene s in m_sceneList)
  273. if (s.RegionInfo.RegionHandle == regionhandle)
  274. return true;
  275. return false;
  276. }
  277. #endregion
  278. }
  279. }