LocalSimulationConnector.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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.Linq;
  30. using System.Reflection;
  31. using log4net;
  32. using Mono.Addins;
  33. using Nini.Config;
  34. using OpenMetaverse;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Framework.Interfaces;
  37. using OpenSim.Region.Framework.Scenes;
  38. using OpenSim.Services.Interfaces;
  39. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  40. namespace OpenSim.Region.CoreModules.ServiceConnectorsOut.Simulation
  41. {
  42. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "LocalSimulationConnectorModule")]
  43. public class LocalSimulationConnectorModule : ISharedRegionModule, ISimulationService
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. /// <summary>
  47. /// Map region ID to scene.
  48. /// </summary>
  49. private Dictionary<UUID, Scene> m_scenes = new Dictionary<UUID, Scene>();
  50. /// <summary>
  51. /// Is this module enabled?
  52. /// </summary>
  53. private bool m_ModuleEnabled = false;
  54. #region Region Module interface
  55. public void Initialise(IConfigSource configSource)
  56. {
  57. IConfig moduleConfig = configSource.Configs["Modules"];
  58. if (moduleConfig != null)
  59. {
  60. string name = moduleConfig.GetString("SimulationServices", "");
  61. if (name == Name)
  62. {
  63. InitialiseService(configSource);
  64. m_ModuleEnabled = true;
  65. m_log.Info("[LOCAL SIMULATION CONNECTOR]: Local simulation enabled.");
  66. }
  67. }
  68. }
  69. public void InitialiseService(IConfigSource configSource)
  70. {
  71. }
  72. public void PostInitialise()
  73. {
  74. }
  75. public void AddRegion(Scene scene)
  76. {
  77. if (!m_ModuleEnabled)
  78. return;
  79. Init(scene);
  80. scene.RegisterModuleInterface<ISimulationService>(this);
  81. }
  82. public void RemoveRegion(Scene scene)
  83. {
  84. if (!m_ModuleEnabled)
  85. return;
  86. RemoveScene(scene);
  87. scene.UnregisterModuleInterface<ISimulationService>(this);
  88. }
  89. public void RegionLoaded(Scene scene)
  90. {
  91. }
  92. public void Close()
  93. {
  94. }
  95. public Type ReplaceableInterface
  96. {
  97. get { return null; }
  98. }
  99. public string Name
  100. {
  101. get { return "LocalSimulationConnectorModule"; }
  102. }
  103. /// <summary>
  104. /// Can be called from other modules.
  105. /// </summary>
  106. /// <param name="scene"></param>
  107. public void RemoveScene(Scene scene)
  108. {
  109. lock (m_scenes)
  110. {
  111. if (m_scenes.ContainsKey(scene.RegionInfo.RegionID))
  112. m_scenes.Remove(scene.RegionInfo.RegionID);
  113. else
  114. m_log.WarnFormat(
  115. "[LOCAL SIMULATION CONNECTOR]: Tried to remove region {0} but it was not present",
  116. scene.RegionInfo.RegionName);
  117. }
  118. }
  119. /// <summary>
  120. /// Can be called from other modules.
  121. /// </summary>
  122. /// <param name="scene"></param>
  123. public void Init(Scene scene)
  124. {
  125. lock (m_scenes)
  126. {
  127. if (!m_scenes.ContainsKey(scene.RegionInfo.RegionID))
  128. m_scenes[scene.RegionInfo.RegionID] = scene;
  129. else
  130. m_log.WarnFormat(
  131. "[LOCAL SIMULATION CONNECTOR]: Tried to add region {0} but it is already present",
  132. scene.RegionInfo.RegionName);
  133. }
  134. }
  135. #endregion
  136. #region ISimulationService
  137. public IScene GetScene(UUID regionId)
  138. {
  139. if (m_scenes.ContainsKey(regionId))
  140. {
  141. return m_scenes[regionId];
  142. }
  143. else
  144. {
  145. // FIXME: This was pre-existing behaviour but possibly not a good idea, since it hides an error rather
  146. // than making it obvious and fixable. Need to see if the error message comes up in practice.
  147. Scene s = m_scenes.Values.ToArray()[0];
  148. m_log.ErrorFormat(
  149. "[LOCAL SIMULATION CONNECTOR]: Region with id {0} not found. Returning {1} {2} instead",
  150. regionId, s.RegionInfo.RegionName, s.RegionInfo.RegionID);
  151. return s;
  152. }
  153. }
  154. public ISimulationService GetInnerService()
  155. {
  156. return this;
  157. }
  158. /**
  159. * Agent-related communications
  160. */
  161. public bool CreateAgent(GridRegion source, GridRegion destination, AgentCircuitData aCircuit, uint teleportFlags, EntityTransferContext ctx, out string reason)
  162. {
  163. if (destination == null)
  164. {
  165. reason = "Given destination was null";
  166. m_log.DebugFormat("[LOCAL SIMULATION CONNECTOR]: CreateAgent was given a null destination");
  167. return false;
  168. }
  169. if (m_scenes.ContainsKey(destination.RegionID))
  170. {
  171. // m_log.DebugFormat("[LOCAL SIMULATION CONNECTOR]: Found region {0} to send SendCreateChildAgent", destination.RegionName);
  172. return m_scenes[destination.RegionID].NewUserConnection(aCircuit, teleportFlags, source, out reason);
  173. }
  174. reason = "Did not find region " + destination.RegionName;
  175. return false;
  176. }
  177. public bool UpdateAgent(GridRegion destination, AgentData cAgentData, EntityTransferContext ctx)
  178. {
  179. if (destination == null)
  180. return false;
  181. if (m_scenes.ContainsKey(destination.RegionID))
  182. {
  183. // m_log.DebugFormat(
  184. // "[LOCAL SIMULATION CONNECTOR]: Found region {0} {1} to send AgentUpdate",
  185. // destination.RegionName, destination.RegionID);
  186. return m_scenes[destination.RegionID].IncomingUpdateChildAgent(cAgentData);
  187. }
  188. // m_log.DebugFormat(
  189. // "[LOCAL COMMS]: Did not find region {0} {1} for ChildAgentUpdate",
  190. // destination.RegionName, destination.RegionID);
  191. return false;
  192. }
  193. public bool UpdateAgent(GridRegion destination, AgentPosition agentPosition)
  194. {
  195. if (destination == null)
  196. return false;
  197. // We limit the number of messages sent for a position change to just one per
  198. // simulator so when we receive the update we need to hand it to each of the
  199. // scenes; scenes each check to see if the is a scene presence for the avatar
  200. // note that we really don't need the GridRegion for this call
  201. foreach (Scene s in m_scenes.Values)
  202. {
  203. // m_log.Debug("[LOCAL COMMS]: Found region to send ChildAgentUpdate");
  204. s.IncomingUpdateChildAgent(agentPosition);
  205. }
  206. //m_log.Debug("[LOCAL COMMS]: region not found for ChildAgentUpdate");
  207. return true;
  208. }
  209. public bool QueryAccess(GridRegion destination, UUID agentID, string agentHomeURI, bool viaTeleport, Vector3 position, List<UUID> features, EntityTransferContext ctx, out string reason)
  210. {
  211. reason = "Communications failure";
  212. if (destination == null)
  213. return false;
  214. if (m_scenes.ContainsKey(destination.RegionID))
  215. {
  216. // m_log.DebugFormat(
  217. // "[LOCAL SIMULATION CONNECTOR]: Found region {0} {1} to send AgentUpdate",
  218. // s.RegionInfo.RegionName, destination.RegionHandle);
  219. uint sizeX = m_scenes[destination.RegionID].RegionInfo.RegionSizeX;
  220. uint sizeY = m_scenes[destination.RegionID].RegionInfo.RegionSizeY;
  221. // Var regions here, and the requesting simulator is in an older version.
  222. // We will forbide this, because it crashes the viewers
  223. if (ctx.OutboundVersion < 0.3f && (sizeX != 256 || sizeY != 256))
  224. {
  225. reason = "Destination is a variable-sized region, and source is an old simulator. Consider upgrading.";
  226. m_log.DebugFormat("[LOCAL SIMULATION CONNECTOR]: Request to access this variable-sized region from older simulator was denied");
  227. return false;
  228. }
  229. return m_scenes[destination.RegionID].QueryAccess(agentID, agentHomeURI, viaTeleport, position, features, out reason);
  230. }
  231. //m_log.Debug("[LOCAL COMMS]: region not found for QueryAccess");
  232. return false;
  233. }
  234. public bool ReleaseAgent(UUID originId, UUID agentId, string uri)
  235. {
  236. if (m_scenes.ContainsKey(originId))
  237. {
  238. // m_log.DebugFormat(
  239. // "[LOCAL SIMULATION CONNECTOR]: Found region {0} {1} to send AgentUpdate",
  240. // s.RegionInfo.RegionName, destination.RegionHandle);
  241. m_scenes[originId].EntityTransferModule.AgentArrivedAtDestination(agentId);
  242. return true;
  243. }
  244. //m_log.Debug("[LOCAL COMMS]: region not found in SendReleaseAgent " + origin);
  245. return false;
  246. }
  247. public bool CloseAgent(GridRegion destination, UUID id, string auth_token)
  248. {
  249. if (destination == null)
  250. return false;
  251. if (m_scenes.ContainsKey(destination.RegionID))
  252. {
  253. // m_log.DebugFormat(
  254. // "[LOCAL SIMULATION CONNECTOR]: Found region {0} {1} to send AgentUpdate",
  255. // s.RegionInfo.RegionName, destination.RegionHandle);
  256. m_scenes[destination.RegionID].CloseAgent(id, false, auth_token);
  257. return true;
  258. }
  259. //m_log.Debug("[LOCAL COMMS]: region not found in SendCloseAgent");
  260. return false;
  261. }
  262. /**
  263. * Object-related communications
  264. */
  265. public bool CreateObject(GridRegion destination, Vector3 newPosition, ISceneObject sog, bool isLocalCall)
  266. {
  267. if (destination == null)
  268. return false;
  269. if (m_scenes.ContainsKey(destination.RegionID))
  270. {
  271. // m_log.DebugFormat(
  272. // "[LOCAL SIMULATION CONNECTOR]: Found region {0} {1} to send AgentUpdate",
  273. // s.RegionInfo.RegionName, destination.RegionHandle);
  274. Scene s = m_scenes[destination.RegionID];
  275. if (isLocalCall)
  276. {
  277. // We need to make a local copy of the object
  278. ISceneObject sogClone = sog.CloneForNewScene();
  279. sogClone.SetState(sog.GetStateSnapshot(), s);
  280. return s.IncomingCreateObject(newPosition, sogClone);
  281. }
  282. else
  283. {
  284. // Use the object as it came through the wire
  285. return s.IncomingCreateObject(newPosition, sog);
  286. }
  287. }
  288. return false;
  289. }
  290. #endregion
  291. #region Misc
  292. public bool IsLocalRegion(ulong regionhandle)
  293. {
  294. foreach (Scene s in m_scenes.Values)
  295. if (s.RegionInfo.RegionHandle == regionhandle)
  296. return true;
  297. return false;
  298. }
  299. public bool IsLocalRegion(UUID id)
  300. {
  301. return m_scenes.ContainsKey(id);
  302. }
  303. #endregion
  304. }
  305. }