ScenePresenceStateMachine.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. namespace OpenSim.Region.Framework.Scenes
  29. {
  30. /// <summary>
  31. /// The possible states that a scene presence can be in. This is currently orthagonal to whether a scene presence
  32. /// is root or child.
  33. /// </summary>
  34. /// <remarks>
  35. /// This is a state machine.
  36. ///
  37. /// [Entry] => Running
  38. /// Running => PreRemove, Removing
  39. /// PreRemove => Running, Removing
  40. /// Removing => Removed
  41. ///
  42. /// All other methods should only see the scene presence in running state - this is the normal operational state
  43. /// Removed state occurs when the presence has been removed. This is the end state with no exit.
  44. /// </remarks>
  45. public enum ScenePresenceState
  46. {
  47. Running, // Normal operation state. The scene presence is available.
  48. PreRemove, // The presence is due to be removed but can still be returning to running.
  49. Removing, // The presence is in the process of being removed from the scene via Scene.RemoveClient.
  50. Removed, // The presence has been removed from the scene and is effectively dead.
  51. // There is no exit from this state.
  52. }
  53. internal class ScenePresenceStateMachine
  54. {
  55. private ScenePresence m_sp;
  56. private ScenePresenceState m_state;
  57. internal ScenePresenceStateMachine(ScenePresence sp)
  58. {
  59. m_sp = sp;
  60. m_state = ScenePresenceState.Running;
  61. }
  62. internal ScenePresenceState GetState()
  63. {
  64. return m_state;
  65. }
  66. /// <summary>
  67. /// Updates the state of an agent that is already in transit.
  68. /// </summary>
  69. /// <param name='id'></param>
  70. /// <param name='newState'></param>
  71. /// <returns></returns>
  72. /// <exception cref='Exception'>Illegal transitions will throw an Exception</exception>
  73. internal void SetState(ScenePresenceState newState)
  74. {
  75. bool transitionOkay = false;
  76. lock (this)
  77. {
  78. if (newState == m_state)
  79. return;
  80. else if (newState == ScenePresenceState.Running && m_state == ScenePresenceState.PreRemove)
  81. transitionOkay = true;
  82. else if (newState == ScenePresenceState.PreRemove && m_state == ScenePresenceState.Running)
  83. transitionOkay = true;
  84. else if (newState == ScenePresenceState.Removing)
  85. {
  86. if (m_state == ScenePresenceState.Running || m_state == ScenePresenceState.PreRemove)
  87. transitionOkay = true;
  88. }
  89. else if (newState == ScenePresenceState.Removed && m_state == ScenePresenceState.Removing)
  90. transitionOkay = true;
  91. }
  92. if (!transitionOkay)
  93. {
  94. throw new Exception(
  95. string.Format(
  96. "Scene presence {0} is not allowed to move from state {1} to new state {2} in {3}",
  97. m_sp.Name, m_state, newState, m_sp.Scene.Name));
  98. }
  99. else
  100. {
  101. m_state = newState;
  102. }
  103. }
  104. }
  105. }