RemoteSimulationConnector.cs 9.5 KB

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