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.

EventBusViewer.java 6.1KB

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.debug.commands;
  23. import com.dmdirc.ClientModule.GlobalConfig;
  24. import com.dmdirc.CustomWindow;
  25. import com.dmdirc.DMDircMBassador;
  26. import com.dmdirc.FrameContainer;
  27. import com.dmdirc.addons.debug.Debug;
  28. import com.dmdirc.addons.debug.DebugCommand;
  29. import com.dmdirc.commandparser.CommandArguments;
  30. import com.dmdirc.commandparser.commands.context.CommandContext;
  31. import com.dmdirc.events.ClientLineAddedEvent;
  32. import com.dmdirc.events.DMDircEvent;
  33. import com.dmdirc.events.FrameClosingEvent;
  34. import com.dmdirc.interfaces.config.AggregateConfigProvider;
  35. import com.dmdirc.ui.WindowManager;
  36. import com.dmdirc.ui.messages.BackBufferFactory;
  37. import com.dmdirc.ui.messages.Styliser;
  38. import com.dmdirc.util.URLBuilder;
  39. import java.lang.reflect.Method;
  40. import javax.annotation.Nonnull;
  41. import javax.inject.Inject;
  42. import javax.inject.Provider;
  43. import net.engio.mbassy.listener.Handler;
  44. import net.engio.mbassy.listener.Listener;
  45. import net.engio.mbassy.listener.References;
  46. /**
  47. * Displays events passed on an event bus.
  48. */
  49. public class EventBusViewer extends DebugCommand {
  50. private final URLBuilder urlBuilder;
  51. private final AggregateConfigProvider globalConfig;
  52. private final WindowManager windowManager;
  53. private final DMDircMBassador globalEventBus;
  54. private final BackBufferFactory backBufferFactory;
  55. /**
  56. * Creates a new instance of the command.
  57. */
  58. @Inject
  59. public EventBusViewer(
  60. final Provider<Debug> commandProvider,
  61. @GlobalConfig final AggregateConfigProvider globalConfig,
  62. final URLBuilder urlBuilder,
  63. final WindowManager windowManager,
  64. final DMDircMBassador globalEventBus,
  65. final BackBufferFactory backBufferFactory) {
  66. super(commandProvider);
  67. this.globalConfig = globalConfig;
  68. this.urlBuilder = urlBuilder;
  69. this.windowManager = windowManager;
  70. this.globalEventBus = globalEventBus;
  71. this.backBufferFactory = backBufferFactory;
  72. }
  73. @Override
  74. public String getName() {
  75. return "eventbus";
  76. }
  77. @Override
  78. public String getUsage() {
  79. return "[--global] - Shows events being sent on an event bus";
  80. }
  81. @Override
  82. public void execute(@Nonnull final FrameContainer origin,
  83. final CommandArguments args, final CommandContext context) {
  84. final boolean isGlobal = args.getArguments().length > 0
  85. && "--global".equals(args.getArguments()[0]);
  86. final CustomWindow window;
  87. if (isGlobal) {
  88. window = new CustomWindow("Event bus", "Event bus", globalConfig, urlBuilder,
  89. globalEventBus, backBufferFactory);
  90. windowManager.addWindow(window);
  91. } else {
  92. window = new CustomWindow("Event bus", "Event bus", origin, urlBuilder,
  93. backBufferFactory);
  94. windowManager.addWindow(origin, window);
  95. }
  96. final DMDircMBassador eventBus = isGlobal ? globalEventBus : origin.getEventBus();
  97. final WindowUpdater updater = new WindowUpdater(eventBus, window);
  98. eventBus.subscribe(updater);
  99. }
  100. /**
  101. * Updates a custom window with details of each event received on an event bus.
  102. */
  103. @Listener(references = References.Strong)
  104. private static class WindowUpdater {
  105. private final DMDircMBassador eventBus;
  106. private final FrameContainer target;
  107. WindowUpdater(final DMDircMBassador eventBus, final FrameContainer target) {
  108. this.eventBus = eventBus;
  109. this.target = target;
  110. }
  111. @Handler
  112. public void handleFrameClosing(final FrameClosingEvent event) {
  113. eventBus.unsubscribe(this);
  114. }
  115. @Handler
  116. public void handleEvent(final DMDircEvent event) {
  117. if (event instanceof ClientLineAddedEvent
  118. && ((ClientLineAddedEvent) event).getFrameContainer() == target) {
  119. // Don't add a line every time we add a line to our output window.
  120. // Things will explode otherwise.
  121. return;
  122. }
  123. final StringBuilder output = new StringBuilder();
  124. output.append(Styliser.CODE_BOLD)
  125. .append(event.getClass().getSimpleName())
  126. .append(Styliser.CODE_BOLD);
  127. for (Method method : event.getClass().getMethods()) {
  128. if (method.getName().startsWith("get") && method.getParameterTypes().length == 0) {
  129. try {
  130. output.append(' ')
  131. .append(Styliser.CODE_UNDERLINE)
  132. .append(method.getName().substring(3))
  133. .append(Styliser.CODE_UNDERLINE)
  134. .append('=')
  135. .append(method.invoke(event).toString());
  136. } catch (ReflectiveOperationException ex) {
  137. // Ignore.
  138. }
  139. }
  140. }
  141. target.addLine(FORMAT_OUTPUT, output.toString());
  142. }
  143. }
  144. }