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.

ServerStatus.java 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2006-2015 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 com.dmdirc.interfaces.Connection;
  24. import com.dmdirc.parser.interfaces.Parser;
  25. import com.dmdirc.util.collections.RollingList;
  26. import java.util.ArrayList;
  27. import java.util.List;
  28. /**
  29. * Describes the status of a server and manages transitions between different states.
  30. *
  31. * @since 0.6.3m1
  32. */
  33. public class ServerStatus {
  34. /** The connection to which this status belongs. */
  35. private final Connection connection;
  36. /** Object to notify when the state of the server changes. */
  37. private final Object notifier;
  38. /** The current state of the server. */
  39. private ServerState state = ServerState.DISCONNECTED;
  40. /** A history of transactions for debugging purposes. */
  41. private final RollingList<String> history = new RollingList<>(10);
  42. /** A list of known parser hashcodes. */
  43. private final List<Integer> parsers = new ArrayList<>();
  44. /**
  45. * Creates a new ServerStatus instance for the specified server.
  46. *
  47. * @param connection The connection to which this status belongs
  48. * @param notifier The object to notify when the state changes
  49. *
  50. * @since 0.6.3
  51. */
  52. public ServerStatus(final Connection connection, final Object notifier) {
  53. this.connection = connection;
  54. this.notifier = notifier;
  55. }
  56. /**
  57. * Transitions the status of this object to the specified state.
  58. *
  59. * @param newState The state to transition to
  60. *
  61. * @throws IllegalArgumentException If the specified transition is invalid
  62. */
  63. public synchronized void transition(final ServerState newState) {
  64. addHistoryEntry(state, newState);
  65. if (state.canTransitionTo(newState)) {
  66. state = newState;
  67. synchronized (notifier) {
  68. notifier.notifyAll();
  69. }
  70. } else {
  71. throw new IllegalArgumentException("Illegal server state "
  72. + "transition\n\n" + getTransitionHistory());
  73. }
  74. }
  75. /**
  76. * Retrieves the current state of this status object.
  77. *
  78. * @return This object's current state
  79. */
  80. public synchronized ServerState getState() {
  81. return state;
  82. }
  83. /**
  84. * Adds a history entry to this status object. The history entry contains the name of the states
  85. * being transitioned between, the details of the method (and class and line) which initiated
  86. * the transition, and the name of the thread in which the transition is occurring.
  87. *
  88. * @param fromState The state which is being transitioned from
  89. * @param toState The state which is being transitioned to
  90. */
  91. protected void addHistoryEntry(final ServerState fromState, final ServerState toState) {
  92. final StringBuilder builder = new StringBuilder();
  93. builder.append(fromState.name());
  94. builder.append('\u2192');
  95. builder.append(toState.name());
  96. builder.append(' ');
  97. builder.append(Thread.currentThread().getStackTrace()[3]);
  98. builder.append(" [");
  99. builder.append(Thread.currentThread().getName());
  100. builder.append("] (parser #");
  101. builder.append(getParserID(connection.getParser().orElse(null)));
  102. builder.append(')');
  103. history.add(builder.toString());
  104. }
  105. /**
  106. * Retrieves the transition history of this status object as a string.
  107. *
  108. * @see #addHistoryEntry(ServerState, ServerState)
  109. * @return A line feed ('\n') delimited string containing one entry for each of the entries in
  110. * this status's transition history.
  111. */
  112. public String getTransitionHistory() {
  113. final StringBuilder builder = new StringBuilder();
  114. for (String line : history.getList()) {
  115. if (builder.length() > 0) {
  116. builder.append('\n');
  117. }
  118. builder.append(line);
  119. }
  120. return builder.toString();
  121. }
  122. /**
  123. * Returns a unique, ID for the specified parser. Each parser that has been seen to be used by
  124. * this server is given a sequential id.
  125. *
  126. * @param parser The parser whose ID is being requested
  127. *
  128. * @return A unique ID for the specified parser, or 0 if the parser is null
  129. */
  130. public int getParserID(final Parser parser) {
  131. if (parser == null) {
  132. return 0;
  133. }
  134. final int hashcode = parser.hashCode();
  135. int offset;
  136. synchronized (parsers) {
  137. offset = parsers.indexOf(hashcode);
  138. if (offset == -1) {
  139. parsers.add(hashcode);
  140. offset = parsers.indexOf(hashcode);
  141. }
  142. }
  143. return 1 + offset;
  144. }
  145. }