You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ServerState.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc;
  23. import java.util.Arrays;
  24. import java.util.List;
  25. /**
  26. * An enumeration of possible states for servers.
  27. */
  28. public enum ServerState {
  29. /** Indicates the client is in the process of connecting. */
  30. CONNECTING(
  31. "CONNECTED", // Connection attempt succeeded
  32. "TRANSIENTLY_DISCONNECTED", // Connection attempt failed
  33. "DISCONNECTING", // User ordered a disconnection
  34. "CLOSING" // DMDirc is closing
  35. ),
  36. /** Indicates the client has connected to the server. */
  37. CONNECTED(
  38. "DISCONNECTING", // User ordered a disconnection
  39. "TRANSIENTLY_DISCONNECTED", // Server caused a disconnection
  40. "CLOSING" // DMDirc is closing
  41. ),
  42. /** Indicates that we've been temporarily disconnected. */
  43. TRANSIENTLY_DISCONNECTED(
  44. "CONNECTING", // User forced a connect attempt
  45. "RECONNECT_WAIT", // Waiting for auto-reconnect
  46. "CLOSING" // DMDirc is closing
  47. ),
  48. /** Indicates that the user has told us to disconnect. */
  49. DISCONNECTED(
  50. "CONNECTING", // User forced a connect attempt
  51. "CLOSING" // DMDirc is closing
  52. ),
  53. /** In the process of disconnecting. */
  54. DISCONNECTING(
  55. "DISCONNECTED", // Socket closed
  56. "CLOSING" // DMDirc is closing
  57. ),
  58. /** Indicates we're waiting for the auto-reconnect timer to fire. */
  59. RECONNECT_WAIT(
  60. "CONNECTING", // User forced a connect attempt
  61. "TRANSIENTLY_DISCONNECTED", // Reconnect timer expired
  62. "DISCONNECTED", // User forced a disconnect
  63. "CLOSING" // DMDirc is closing
  64. ),
  65. /** Indicates that the server frame and its children are closing. */
  66. CLOSING;
  67. /** The allowed transitions from this state. */
  68. private final List<String> transitions;
  69. /**
  70. * Creates a new instance of ServerState.
  71. *
  72. * @since 0.6.3m1
  73. * @param transitions The names of the states to which a transition is allowed from this state
  74. */
  75. ServerState(final String... transitions) {
  76. this.transitions = Arrays.asList(transitions);
  77. }
  78. /**
  79. * Determines whether a transition from this state to the specified state would be legal.
  80. *
  81. * @since 0.6.3m1
  82. * @param state The state that is being transitioned to
  83. *
  84. * @return True if the transition is allowed, false otherwise.
  85. */
  86. public boolean canTransitionTo(final ServerState state) {
  87. return transitions.contains(state.name());
  88. }
  89. /**
  90. * Determines where the current state is a disconnected one.
  91. *
  92. * @return True if the state is one of the disconnected states, false otherwise
  93. *
  94. * @since 0.6.3m1
  95. */
  96. public boolean isDisconnected() {
  97. return this == ServerState.DISCONNECTED || this == ServerState.TRANSIENTLY_DISCONNECTED;
  98. }
  99. }