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

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