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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc;
  18. import com.dmdirc.interfaces.Connection;
  19. import com.dmdirc.parser.interfaces.Parser;
  20. import com.dmdirc.util.collections.RollingList;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. /**
  24. * Describes the status of a server and manages transitions between different states.
  25. *
  26. * @since 0.6.3m1
  27. */
  28. public class ServerStatus {
  29. /** The connection to which this status belongs. */
  30. private final Connection connection;
  31. /** Object to notify when the state of the server changes. */
  32. private final Object notifier;
  33. /** The current state of the server. */
  34. private ServerState state = ServerState.DISCONNECTED;
  35. /** A history of transactions for debugging purposes. */
  36. private final RollingList<String> history = new RollingList<>(10);
  37. /** A list of known parser hashcodes. */
  38. private final List<Integer> parsers = new ArrayList<>();
  39. /**
  40. * Creates a new ServerStatus instance for the specified server.
  41. *
  42. * @param connection The connection to which this status belongs
  43. * @param notifier The object to notify when the state changes
  44. *
  45. * @since 0.6.3
  46. */
  47. public ServerStatus(final Connection connection, final Object notifier) {
  48. this.connection = connection;
  49. this.notifier = notifier;
  50. }
  51. /**
  52. * Transitions the status of this object to the specified state.
  53. *
  54. * @param newState The state to transition to
  55. *
  56. * @throws IllegalArgumentException If the specified transition is invalid
  57. */
  58. public synchronized void transition(final ServerState newState) {
  59. addHistoryEntry(state, newState);
  60. if (state.canTransitionTo(newState)) {
  61. state = newState;
  62. synchronized (notifier) {
  63. notifier.notifyAll();
  64. }
  65. } else {
  66. throw new IllegalArgumentException("Illegal server state "
  67. + "transition\n\n" + getTransitionHistory());
  68. }
  69. }
  70. /**
  71. * Retrieves the current state of this status object.
  72. *
  73. * @return This object's current state
  74. */
  75. public synchronized ServerState getState() {
  76. return state;
  77. }
  78. /**
  79. * Adds a history entry to this status object. The history entry contains the name of the states
  80. * being transitioned between, the details of the method (and class and line) which initiated
  81. * the transition, and the name of the thread in which the transition is occurring.
  82. *
  83. * @param fromState The state which is being transitioned from
  84. * @param toState The state which is being transitioned to
  85. */
  86. protected void addHistoryEntry(final ServerState fromState, final ServerState toState) {
  87. final StringBuilder builder = new StringBuilder();
  88. builder.append(fromState.name());
  89. builder.append('\u2192');
  90. builder.append(toState.name());
  91. builder.append(' ');
  92. builder.append(Thread.currentThread().getStackTrace()[3]);
  93. builder.append(" [");
  94. builder.append(Thread.currentThread().getName());
  95. builder.append("] (parser #");
  96. builder.append(getParserID(connection.getParser().orElse(null)));
  97. builder.append(')');
  98. history.add(builder.toString());
  99. }
  100. /**
  101. * Retrieves the transition history of this status object as a string.
  102. *
  103. * @see #addHistoryEntry(ServerState, ServerState)
  104. * @return A line feed ('\n') delimited string containing one entry for each of the entries in
  105. * this status's transition history.
  106. */
  107. public String getTransitionHistory() {
  108. final StringBuilder builder = new StringBuilder();
  109. for (String line : history.getList()) {
  110. if (builder.length() > 0) {
  111. builder.append('\n');
  112. }
  113. builder.append(line);
  114. }
  115. return builder.toString();
  116. }
  117. /**
  118. * Returns a unique, ID for the specified parser. Each parser that has been seen to be used by
  119. * this server is given a sequential id.
  120. *
  121. * @param parser The parser whose ID is being requested
  122. *
  123. * @return A unique ID for the specified parser, or 0 if the parser is null
  124. */
  125. public int getParserID(final Parser parser) {
  126. if (parser == null) {
  127. return 0;
  128. }
  129. final int hashcode = parser.hashCode();
  130. int offset;
  131. synchronized (parsers) {
  132. offset = parsers.indexOf(hashcode);
  133. if (offset == -1) {
  134. parsers.add(hashcode);
  135. offset = parsers.indexOf(hashcode);
  136. }
  137. }
  138. return 1 + offset;
  139. }
  140. }