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.

ParserDebugManager.java 5.8KB

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.addons.parserdebug;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.dmdirc.events.ServerDisconnectedEvent;
  25. import com.dmdirc.interfaces.Connection;
  26. import com.dmdirc.parser.common.CallbackNotFoundException;
  27. import com.dmdirc.parser.interfaces.Parser;
  28. import com.dmdirc.parser.interfaces.callbacks.DebugInfoListener;
  29. import com.dmdirc.ui.WindowManager;
  30. import com.dmdirc.ui.messages.BackBufferFactory;
  31. import com.dmdirc.util.URLBuilder;
  32. import java.util.Date;
  33. import java.util.HashMap;
  34. import java.util.Map;
  35. import javax.inject.Inject;
  36. import net.engio.mbassy.listener.Handler;
  37. public class ParserDebugManager implements DebugInfoListener {
  38. /** Event bus to subscribe to events on. */
  39. private final DMDircMBassador eventBus;
  40. /** Map of parsers registered. */
  41. protected final Map<Parser, DebugWindow> registeredParsers;
  42. /** Window manager. */
  43. private final WindowManager windowManager;
  44. private final BackBufferFactory backBufferFactory;
  45. @Inject
  46. public ParserDebugManager(
  47. final URLBuilder urlBuilder,
  48. final WindowManager windowManager,
  49. final DMDircMBassador eventBus,
  50. final BackBufferFactory backBufferFactory) {
  51. this.windowManager = windowManager;
  52. this.eventBus = eventBus;
  53. this.backBufferFactory = backBufferFactory;
  54. registeredParsers = new HashMap<>();
  55. }
  56. /**
  57. * Adds action listener.
  58. */
  59. public void addActionListener() {
  60. eventBus.subscribe(this);
  61. }
  62. /**
  63. * Remove action listener.
  64. */
  65. public void removeActionListener() {
  66. eventBus.unsubscribe(this);
  67. }
  68. /**
  69. * Are we listener on the specified parser?
  70. *
  71. * @param parser Parser to check
  72. *
  73. * @return true if we're listening
  74. */
  75. public boolean containsParser(final Parser parser) {
  76. return registeredParsers.containsKey(parser);
  77. }
  78. /**
  79. * Adds a parser to this manager, adding any required call backs.
  80. *
  81. * @param parser Parser to add
  82. * @param connection The connection associated with the parser
  83. *
  84. * @return Whether we added the parser without error
  85. */
  86. public boolean addParser(final Parser parser, final Connection connection) {
  87. try {
  88. parser.getCallbackManager().addCallback(DebugInfoListener.class, this);
  89. final DebugWindow window = new DebugWindow(this, "Parser Debug", parser,
  90. connection, eventBus, backBufferFactory);
  91. windowManager.addWindow(connection.getWindowModel(), window);
  92. registeredParsers.put(parser, window);
  93. window.addLine("======================", new Date());
  94. window.addLine("Started Monitoring: " + parser, new Date());
  95. window.addLine("======================", new Date());
  96. return true;
  97. } catch (CallbackNotFoundException ex) {
  98. return false;
  99. }
  100. }
  101. /**
  102. * Removes the parser from this manager, removing any call backs as required.
  103. *
  104. * @param parser Parser to add
  105. * @param close Close debug window?
  106. *
  107. * @return Whether removed the parser without error
  108. */
  109. public boolean removeParser(final Parser parser, final boolean close) {
  110. try {
  111. parser.getCallbackManager().delCallback(DebugInfoListener.class, this);
  112. final DebugWindow window = registeredParsers.get(parser);
  113. window.addLine("======================", new Date());
  114. window.addLine("No Longer Monitoring: " + parser + " (User Requested)", new Date());
  115. window.addLine("======================", new Date());
  116. if (close) {
  117. window.close();
  118. }
  119. registeredParsers.remove(parser);
  120. return true;
  121. } catch (CallbackNotFoundException ex) {
  122. return false;
  123. }
  124. }
  125. /**
  126. * Removes all parser listeners and closes.
  127. *
  128. * @param close Close debug windows?
  129. */
  130. public void removeAllParserListeners(final boolean close) {
  131. for (Parser parser : registeredParsers.keySet()) {
  132. removeParser(parser, close);
  133. }
  134. }
  135. @Handler
  136. public void handleServerDisconnected(final ServerDisconnectedEvent event) {
  137. final Parser parser = event.getConnection().getParser().get();
  138. if (registeredParsers.containsKey(parser)) {
  139. removeParser(parser, false);
  140. }
  141. }
  142. @Override
  143. public void onDebugInfo(final Parser parser, final Date date, final int level, final String data) {
  144. final DebugWindow window = registeredParsers.get(parser);
  145. if (window != null) {
  146. window.addLine(String.format("[%d] %s%n", level, data), new Date());
  147. }
  148. }
  149. }