您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ServerStatus.java 5.6KB

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