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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.RollingList;
  25. import java.util.ArrayList;
  26. import java.util.List;
  27. /**
  28. * Describes the status of a server and manages transitions between different
  29. * states.
  30. *
  31. * @since 0.6.3m1
  32. * @author chris
  33. */
  34. public class ServerStatus {
  35. /** The server to which this status belongs. */
  36. protected final Server server;
  37. /** Object to notify when the state of the server changes. */
  38. protected final Object notifier;
  39. /** The current state of the server. */
  40. protected ServerState state = ServerState.DISCONNECTED;
  41. /** A history of transactions for debugging purposes. */
  42. protected RollingList<String> history = new RollingList<String>(10);
  43. /** A list of known parser hashcodes. */
  44. protected final List<Integer> parsers = new ArrayList<Integer>();
  45. /**
  46. * Creates a new ServerStatus instance for the specified server.
  47. *
  48. * @param server The server to which this status belongs
  49. * @param notifier The object to notify when the state changes
  50. * @since 0.6.3
  51. */
  52. public ServerStatus(final Server server, final Object notifier) {
  53. this.server = server;
  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. * @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
  84. * the name of the states being transitioned between, the details of the
  85. * method (and class and line) which initiated the transition, and the
  86. * name of the thread in which the transition is occuring.
  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('→');
  95. builder.append(toState.name());
  96. builder.append(' ');
  97. builder.append(Thread.currentThread().getStackTrace()[3].toString());
  98. builder.append(" [");
  99. builder.append(Thread.currentThread().getName());
  100. builder.append("] (parser #");
  101. builder.append(getParserID(server.getParser()));
  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(com.dmdirc.ServerState, com.dmdirc.ServerState)
  109. * @return A line feed ('\n') delimited string containing one entry for
  110. * each of the entries in 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
  124. * seen to be used by this server is given a sequential id.
  125. *
  126. * @param parser The parser whose ID is being requested
  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. }