EntityTransferStateMachine.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.Net;
  30. using System.Reflection;
  31. using System.Threading;
  32. using OpenMetaverse;
  33. using log4net;
  34. using Nini.Config;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Capabilities;
  37. using OpenSim.Framework.Client;
  38. using OpenSim.Region.Framework.Interfaces;
  39. using OpenSim.Region.Framework.Scenes;
  40. using OpenSim.Region.Physics.Manager;
  41. using OpenSim.Services.Interfaces;
  42. using GridRegion = OpenSim.Services.Interfaces.GridRegion;
  43. namespace OpenSim.Region.CoreModules.Framework.EntityTransfer
  44. {
  45. /// <summary>
  46. /// The possible states that an agent can be in when its being transferred between regions.
  47. /// </summary>
  48. /// <remarks>
  49. /// This is a state machine.
  50. ///
  51. /// [Entry] => Preparing
  52. /// Preparing => { Transferring || CleaningUp || [Exit] }
  53. /// Transferring => { ReceivedAtDestination || CleaningUp }
  54. /// ReceivedAtDestination => CleaningUp
  55. /// CleaningUp => [Exit]
  56. ///
  57. /// In other words, agents normally travel throwing Preparing => Transferring => ReceivedAtDestination => CleaningUp
  58. /// However, any state can transition to CleaningUp if the teleport has failed.
  59. /// </remarks>
  60. enum AgentTransferState
  61. {
  62. Preparing, // The agent is being prepared for transfer
  63. Transferring, // The agent is in the process of being transferred to a destination
  64. ReceivedAtDestination, // The destination has notified us that the agent has been successfully received
  65. CleaningUp // The agent is being changed to child/removed after a transfer
  66. }
  67. /// <summary>
  68. /// Records the state of entities when they are in transfer within or between regions (cross or teleport).
  69. /// </summary>
  70. public class EntityTransferStateMachine
  71. {
  72. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  73. /// <summary>
  74. /// If true then on a teleport, the source region waits for a callback from the destination region. If
  75. /// a callback fails to arrive within a set time then the user is pulled back into the source region.
  76. /// </summary>
  77. public bool EnableWaitForAgentArrivedAtDestination { get; set; }
  78. private EntityTransferModule m_mod;
  79. private Dictionary<UUID, AgentTransferState> m_agentsInTransit = new Dictionary<UUID, AgentTransferState>();
  80. public EntityTransferStateMachine(EntityTransferModule module)
  81. {
  82. m_mod = module;
  83. }
  84. /// <summary>
  85. /// Set that an agent is in transit.
  86. /// </summary>
  87. /// <param name='id'>The ID of the agent being teleported</param>
  88. /// <returns>true if the agent was not already in transit, false if it was</returns>
  89. internal bool SetInTransit(UUID id)
  90. {
  91. lock (m_agentsInTransit)
  92. {
  93. if (!m_agentsInTransit.ContainsKey(id))
  94. {
  95. m_agentsInTransit[id] = AgentTransferState.Preparing;
  96. return true;
  97. }
  98. }
  99. return false;
  100. }
  101. /// <summary>
  102. /// Updates the state of an agent that is already in transit.
  103. /// </summary>
  104. /// <param name='id'></param>
  105. /// <param name='newState'></param>
  106. /// <returns></returns>
  107. /// <exception cref='Exception'>Illegal transitions will throw an Exception</exception>
  108. internal void UpdateInTransit(UUID id, AgentTransferState newState)
  109. {
  110. lock (m_agentsInTransit)
  111. {
  112. // Illegal to try and update an agent that's not actually in transit.
  113. if (!m_agentsInTransit.ContainsKey(id))
  114. throw new Exception(
  115. string.Format(
  116. "Agent with ID {0} is not registered as in transit in {1}",
  117. id, m_mod.Scene.RegionInfo.RegionName));
  118. AgentTransferState oldState = m_agentsInTransit[id];
  119. bool transitionOkay = false;
  120. if (newState == AgentTransferState.CleaningUp && oldState != AgentTransferState.CleaningUp)
  121. transitionOkay = true;
  122. else if (newState == AgentTransferState.Transferring && oldState == AgentTransferState.Preparing)
  123. transitionOkay = true;
  124. else if (newState == AgentTransferState.ReceivedAtDestination && oldState == AgentTransferState.Transferring)
  125. transitionOkay = true;
  126. if (transitionOkay)
  127. m_agentsInTransit[id] = newState;
  128. else
  129. throw new Exception(
  130. string.Format(
  131. "Agent with ID {0} is not allowed to move from old transit state {1} to new state {2} in {3}",
  132. id, oldState, newState, m_mod.Scene.RegionInfo.RegionName));
  133. }
  134. }
  135. internal bool IsInTransit(UUID id)
  136. {
  137. lock (m_agentsInTransit)
  138. return m_agentsInTransit.ContainsKey(id);
  139. }
  140. /// <summary>
  141. /// Removes an agent from the transit state machine.
  142. /// </summary>
  143. /// <param name='id'></param>
  144. /// <returns>true if the agent was flagged as being teleported when this method was called, false otherwise</returns>
  145. internal bool ResetFromTransit(UUID id)
  146. {
  147. lock (m_agentsInTransit)
  148. {
  149. if (m_agentsInTransit.ContainsKey(id))
  150. {
  151. AgentTransferState state = m_agentsInTransit[id];
  152. if (state == AgentTransferState.Transferring || state == AgentTransferState.ReceivedAtDestination)
  153. {
  154. // FIXME: For now, we allow exit from any state since a thrown exception in teleport is now guranteed
  155. // to be handled properly - ResetFromTransit() could be invoked at any step along the process
  156. m_log.WarnFormat(
  157. "[ENTITY TRANSFER STATE MACHINE]: Agent with ID {0} should not exit directly from state {1}, should go to {2} state first in {3}",
  158. id, state, AgentTransferState.CleaningUp, m_mod.Scene.RegionInfo.RegionName);
  159. // throw new Exception(
  160. // "Agent with ID {0} cannot exit directly from state {1}, it must go to {2} state first",
  161. // state, AgentTransferState.CleaningUp);
  162. }
  163. m_agentsInTransit.Remove(id);
  164. m_log.DebugFormat(
  165. "[ENTITY TRANSFER STATE MACHINE]: Agent {0} cleared from transit in {1}",
  166. id, m_mod.Scene.RegionInfo.RegionName);
  167. return true;
  168. }
  169. }
  170. m_log.WarnFormat(
  171. "[ENTITY TRANSFER STATE MACHINE]: Agent {0} requested to clear from transit in {1} but was already cleared",
  172. id, m_mod.Scene.RegionInfo.RegionName);
  173. return false;
  174. }
  175. internal bool WaitForAgentArrivedAtDestination(UUID id)
  176. {
  177. if (!m_mod.WaitForAgentArrivedAtDestination)
  178. return true;
  179. lock (m_agentsInTransit)
  180. {
  181. if (!IsInTransit(id))
  182. throw new Exception(
  183. string.Format(
  184. "Asked to wait for destination callback for agent with ID {0} in {1} but agent is not in transit",
  185. id, m_mod.Scene.RegionInfo.RegionName));
  186. AgentTransferState currentState = m_agentsInTransit[id];
  187. if (currentState != AgentTransferState.Transferring && currentState != AgentTransferState.ReceivedAtDestination)
  188. throw new Exception(
  189. string.Format(
  190. "Asked to wait for destination callback for agent with ID {0} in {1} but agent is in state {2}",
  191. id, m_mod.Scene.RegionInfo.RegionName, currentState));
  192. }
  193. int count = 200;
  194. // There should be no race condition here since no other code should be removing the agent transfer or
  195. // changing the state to another other than Transferring => ReceivedAtDestination.
  196. while (m_agentsInTransit[id] != AgentTransferState.ReceivedAtDestination && count-- > 0)
  197. {
  198. // m_log.Debug(" >>> Waiting... " + count);
  199. Thread.Sleep(100);
  200. }
  201. return count > 0;
  202. }
  203. internal void SetAgentArrivedAtDestination(UUID id)
  204. {
  205. lock (m_agentsInTransit)
  206. {
  207. if (!m_agentsInTransit.ContainsKey(id))
  208. {
  209. m_log.WarnFormat(
  210. "[ENTITY TRANSFER STATE MACHINE]: Region {0} received notification of arrival in destination of agent {1} but no teleport request is active",
  211. m_mod.Scene.RegionInfo.RegionName, id);
  212. return;
  213. }
  214. AgentTransferState currentState = m_agentsInTransit[id];
  215. if (currentState == AgentTransferState.ReceivedAtDestination)
  216. {
  217. // An anomoly but don't make this an outright failure - destination region could be overzealous in sending notification.
  218. m_log.WarnFormat(
  219. "[ENTITY TRANSFER STATE MACHINE]: Region {0} received notification of arrival in destination of agent {1} but notification has already previously been received",
  220. m_mod.Scene.RegionInfo.RegionName, id);
  221. }
  222. else if (currentState != AgentTransferState.Transferring)
  223. {
  224. m_log.ErrorFormat(
  225. "[ENTITY TRANSFER STATE MACHINE]: Region {0} received notification of arrival in destination of agent {1} but agent is in state {2}",
  226. m_mod.Scene.RegionInfo.RegionName, id, currentState);
  227. return;
  228. }
  229. m_agentsInTransit[id] = AgentTransferState.ReceivedAtDestination;
  230. }
  231. }
  232. }
  233. }