LocalSimulationConnector.cs 13 KB

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