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

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