ScenePresenceStateMachine.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 => Removing
  39. /// Removing => Removed
  40. ///
  41. /// All other methods should only see the scene presence in running state - this is the normal operational state
  42. /// Removed state occurs when the presence has been removed. This is the end state with no exit.
  43. /// </remarks>
  44. public enum ScenePresenceState
  45. {
  46. Running, // Normal operation state. The scene presence is available.
  47. Removing, // The presence is in the process of being removed from the scene via Scene.RemoveClient.
  48. Removed, // The presence has been removed from the scene and is effectively dead.
  49. // There is no exit from this state.
  50. }
  51. internal class ScenePresenceStateMachine
  52. {
  53. private ScenePresence m_sp;
  54. private ScenePresenceState m_state;
  55. internal ScenePresenceStateMachine(ScenePresence sp)
  56. {
  57. m_sp = sp;
  58. m_state = ScenePresenceState.Running;
  59. }
  60. internal ScenePresenceState GetState()
  61. {
  62. return m_state;
  63. }
  64. /// <summary>
  65. /// Updates the state of an agent that is already in transit.
  66. /// </summary>
  67. /// <param name='id'></param>
  68. /// <param name='newState'></param>
  69. /// <returns></returns>
  70. /// <exception cref='Exception'>Illegal transitions will throw an Exception</exception>
  71. internal void SetState(ScenePresenceState newState)
  72. {
  73. bool transitionOkay = false;
  74. lock (this)
  75. {
  76. if (newState == ScenePresenceState.Removing && m_state == ScenePresenceState.Running)
  77. transitionOkay = true;
  78. else if (newState == ScenePresenceState.Removed && m_state == ScenePresenceState.Removing)
  79. transitionOkay = true;
  80. }
  81. if (!transitionOkay)
  82. {
  83. throw new Exception(
  84. string.Format(
  85. "Scene presence {0} is not allowed to move from state {1} to new state {2} in {3}",
  86. m_sp.Name, m_state, newState, m_sp.Scene.Name));
  87. }
  88. else
  89. {
  90. m_state = newState;
  91. }
  92. }
  93. }
  94. }