RemoteSimulationConnector.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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;
  29. using System.IO;
  30. using System.Net;
  31. using System.Reflection;
  32. using System.Text;
  33. using log4net;
  34. using Mono.Addins;
  35. using Nini.Config;
  36. using OpenMetaverse;
  37. using OpenMetaverse.StructuredData;
  38. using OpenSim.Framework;
  39. using OpenSim.Framework.Communications;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. using OpenSim.Region.Framework.Scenes.Serialization;
  43. using OpenSim.Services.Interfaces;
  44. using OpenSim.Services.Connectors.Simulation;
  45. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  46. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Simulation
  47. {
  48. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "RemoteSimulationConnectorModule")]
  49. public class RemoteSimulationConnectorModule : ISharedRegionModule, ISimulationService
  50. {
  51. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  52. private bool initialized = false;
  53. protected bool m_enabled = false;
  54. protected Scene m_aScene;
  55. // RemoteSimulationConnector does not care about local regions; it delegates that to the Local module
  56. protected LocalSimulationConnectorModule m_localBackend;
  57. protected SimulationServiceConnector m_remoteConnector;
  58. protected bool m_safemode;
  59. protected IPAddress m_thisIP;
  60. #region Region Module interface
  61. public virtual void Initialise(IConfigSource configSource)
  62. {
  63. IConfig moduleConfig = configSource.Configs["Modules"];
  64. if (moduleConfig != null)
  65. {
  66. string name = moduleConfig.GetString("SimulationServices", "");
  67. if (name == Name)
  68. {
  69. m_localBackend = new LocalSimulationConnectorModule();
  70. m_localBackend.InitialiseService(configSource);
  71. m_remoteConnector = new SimulationServiceConnector();
  72. m_enabled = true;
  73. m_log.Info("[REMOTE SIMULATION CONNECTOR]: Remote simulation enabled.");
  74. }
  75. }
  76. }
  77. public virtual void PostInitialise()
  78. {
  79. }
  80. public virtual void Close()
  81. {
  82. }
  83. public void AddRegion(Scene scene)
  84. {
  85. if (!m_enabled)
  86. return;
  87. if (!initialized)
  88. {
  89. InitOnce(scene);
  90. initialized = true;
  91. }
  92. InitEach(scene);
  93. }
  94. public void RemoveRegion(Scene scene)
  95. {
  96. if (m_enabled)
  97. {
  98. m_localBackend.RemoveScene(scene);
  99. scene.UnregisterModuleInterface<ISimulationService>(this);
  100. }
  101. }
  102. public void RegionLoaded(Scene scene)
  103. {
  104. if (!m_enabled)
  105. return;
  106. }
  107. public Type ReplaceableInterface
  108. {
  109. get { return null; }
  110. }
  111. public virtual string Name
  112. {
  113. get { return "RemoteSimulationConnectorModule"; }
  114. }
  115. protected virtual void InitEach(Scene scene)
  116. {
  117. m_localBackend.Init(scene);
  118. scene.RegisterModuleInterface<ISimulationService>(this);
  119. }
  120. protected virtual void InitOnce(Scene scene)
  121. {
  122. m_aScene = scene;
  123. //m_regionClient = new RegionToRegionClient(m_aScene, m_hyperlinkService);
  124. m_thisIP = Util.GetHostFromDNS(scene.RegionInfo.ExternalHostName);
  125. }
  126. #endregion
  127. #region IInterregionComms
  128. public IScene GetScene(UUID regionId)
  129. {
  130. return m_localBackend.GetScene(regionId);
  131. }
  132. public ISimulationService GetInnerService()
  133. {
  134. return m_localBackend;
  135. }
  136. /**
  137. * Agent-related communications
  138. */
  139. public bool CreateAgent(GridRegion destination, AgentCircuitData aCircuit, uint teleportFlags, out string reason)
  140. {
  141. if (destination == null)
  142. {
  143. reason = "Given destination was null";
  144. m_log.DebugFormat("[REMOTE SIMULATION CONNECTOR]: CreateAgent was given a null destination");
  145. return false;
  146. }
  147. // Try local first
  148. if (m_localBackend.CreateAgent(destination, aCircuit, teleportFlags, out reason))
  149. return true;
  150. // else do the remote thing
  151. if (!m_localBackend.IsLocalRegion(destination.RegionID))
  152. {
  153. return m_remoteConnector.CreateAgent(destination, aCircuit, teleportFlags, out reason);
  154. }
  155. return false;
  156. }
  157. public bool UpdateAgent(GridRegion destination, AgentData cAgentData)
  158. {
  159. if (destination == null)
  160. return false;
  161. // Try local first
  162. if (m_localBackend.IsLocalRegion(destination.RegionID))
  163. return m_localBackend.UpdateAgent(destination, cAgentData);
  164. return m_remoteConnector.UpdateAgent(destination, cAgentData);
  165. }
  166. public bool UpdateAgent(GridRegion destination, AgentPosition cAgentData)
  167. {
  168. if (destination == null)
  169. return false;
  170. // Try local first
  171. if (m_localBackend.IsLocalRegion(destination.RegionID))
  172. return m_localBackend.UpdateAgent(destination, cAgentData);
  173. return m_remoteConnector.UpdateAgent(destination, cAgentData);
  174. }
  175. public bool QueryAccess(GridRegion destination, UUID id, Vector3 position, out string version, out string reason)
  176. {
  177. reason = "Communications failure";
  178. version = "Unknown";
  179. if (destination == null)
  180. return false;
  181. // Try local first
  182. if (m_localBackend.QueryAccess(destination, id, position, out version, out reason))
  183. return true;
  184. // else do the remote thing
  185. if (!m_localBackend.IsLocalRegion(destination.RegionID))
  186. return m_remoteConnector.QueryAccess(destination, id, position, out version, out reason);
  187. return false;
  188. }
  189. public bool ReleaseAgent(UUID origin, UUID id, string uri)
  190. {
  191. // Try local first
  192. if (m_localBackend.ReleaseAgent(origin, id, uri))
  193. return true;
  194. // else do the remote thing
  195. if (!m_localBackend.IsLocalRegion(origin))
  196. return m_remoteConnector.ReleaseAgent(origin, id, uri);
  197. return false;
  198. }
  199. public bool CloseAgent(GridRegion destination, UUID id, string auth_token)
  200. {
  201. if (destination == null)
  202. return false;
  203. // Try local first
  204. if (m_localBackend.CloseAgent(destination, id, auth_token))
  205. return true;
  206. // else do the remote thing
  207. if (!m_localBackend.IsLocalRegion(destination.RegionID))
  208. return m_remoteConnector.CloseAgent(destination, id, auth_token);
  209. return false;
  210. }
  211. /**
  212. * Object-related communications
  213. */
  214. public bool CreateObject(GridRegion destination, Vector3 newPosition, ISceneObject sog, bool isLocalCall)
  215. {
  216. if (destination == null)
  217. return false;
  218. // Try local first
  219. if (m_localBackend.CreateObject(destination, newPosition, sog, isLocalCall))
  220. {
  221. //m_log.Debug("[REST COMMS]: LocalBackEnd SendCreateObject succeeded");
  222. return true;
  223. }
  224. // else do the remote thing
  225. if (!m_localBackend.IsLocalRegion(destination.RegionID))
  226. return m_remoteConnector.CreateObject(destination, newPosition, sog, isLocalCall);
  227. return false;
  228. }
  229. #endregion /* IInterregionComms */
  230. }
  231. }