Sfoglia il codice sorgente

Add a debug command to view events.

Change-Id: Id258151e854d13b1fdadf02eb26b6be21144e5a8
Reviewed-on: http://gerrit.dmdirc.com/3483
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
changes/83/3483/2
Chris Smith 10 anni fa
parent
commit
748c33322b

+ 6
- 0
src/com/dmdirc/addons/debug/DebugModule.java Vedi File

@@ -26,6 +26,7 @@ import com.dmdirc.ClientModule;
26 26
 import com.dmdirc.addons.debug.commands.Benchmark;
27 27
 import com.dmdirc.addons.debug.commands.ColourSpam;
28 28
 import com.dmdirc.addons.debug.commands.ConfigInfo;
29
+import com.dmdirc.addons.debug.commands.EventBusViewer;
29 30
 import com.dmdirc.addons.debug.commands.FakeError;
30 31
 import com.dmdirc.addons.debug.commands.FakeUpdates;
31 32
 import com.dmdirc.addons.debug.commands.FirstRun;
@@ -147,4 +148,9 @@ public class DebugModule {
147 148
         return command;
148 149
     }
149 150
 
151
+    @Provides(type = Provides.Type.SET)
152
+    public DebugCommand getCommand(final EventBusViewer command) {
153
+        return command;
154
+    }
155
+
150 156
 }

+ 165
- 0
src/com/dmdirc/addons/debug/commands/EventBusViewer.java Vedi File

@@ -0,0 +1,165 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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
+
23
+package com.dmdirc.addons.debug.commands;
24
+
25
+import com.dmdirc.ClientModule.GlobalConfig;
26
+import com.dmdirc.CustomWindow;
27
+import com.dmdirc.FrameContainer;
28
+import com.dmdirc.addons.debug.Debug;
29
+import com.dmdirc.addons.debug.DebugCommand;
30
+import com.dmdirc.commandparser.CommandArguments;
31
+import com.dmdirc.commandparser.commands.context.CommandContext;
32
+import com.dmdirc.events.ClientLineAddedEvent;
33
+import com.dmdirc.events.DMDircEvent;
34
+import com.dmdirc.interfaces.FrameCloseListener;
35
+import com.dmdirc.interfaces.config.AggregateConfigProvider;
36
+import com.dmdirc.ui.WindowManager;
37
+import com.dmdirc.ui.messages.Styliser;
38
+import com.dmdirc.util.URLBuilder;
39
+
40
+import com.google.common.eventbus.EventBus;
41
+import com.google.common.eventbus.Subscribe;
42
+
43
+import java.lang.reflect.Method;
44
+
45
+import javax.inject.Inject;
46
+import javax.inject.Provider;
47
+
48
+/**
49
+ * Displays events passed on an event bus.
50
+ */
51
+public class EventBusViewer extends DebugCommand {
52
+
53
+    private final URLBuilder urlBuilder;
54
+    private final AggregateConfigProvider globalConfig;
55
+    private final WindowManager windowManager;
56
+    private final EventBus globalEventBus;
57
+
58
+    /**
59
+     * Creates a new instance of the command.
60
+     *
61
+     * @param commandProvider The provider to use to access the main debug command.
62
+     * @param globalConfig    The global configuration to use for new windows.
63
+     * @param urlBuilder      The URL builder to use in new windows.
64
+     * @param windowManager   The manager to register new windows with.
65
+     * @param globalEventBus  The bus to use when the user specifies the --global flag.
66
+     */
67
+    @Inject
68
+    public EventBusViewer(
69
+            final Provider<Debug> commandProvider,
70
+            @GlobalConfig final AggregateConfigProvider globalConfig,
71
+            final URLBuilder urlBuilder,
72
+            final WindowManager windowManager,
73
+            final EventBus globalEventBus) {
74
+        super(commandProvider);
75
+        this.globalConfig = globalConfig;
76
+        this.urlBuilder = urlBuilder;
77
+        this.windowManager = windowManager;
78
+        this.globalEventBus = globalEventBus;
79
+    }
80
+
81
+    @Override
82
+    public String getName() {
83
+        return "eventbus";
84
+    }
85
+
86
+    @Override
87
+    public String getUsage() {
88
+        return "[--global] - Shows events being sent on an event bus";
89
+    }
90
+
91
+    @Override
92
+    public void execute(final FrameContainer origin,
93
+            final CommandArguments args, final CommandContext context) {
94
+        final boolean isGlobal = args.getArguments().length > 0
95
+                && "--global".equals(args.getArguments()[0]);
96
+
97
+        final CustomWindow window;
98
+        if (isGlobal) {
99
+            window = new CustomWindow("Event bus", "Event bus", globalConfig,
100
+                    urlBuilder, globalEventBus);
101
+            windowManager.addWindow(window);
102
+        } else {
103
+            window = new CustomWindow("Event bus", "Event bus", origin,
104
+                    urlBuilder);
105
+            windowManager.addWindow(origin, window);
106
+        }
107
+
108
+        final EventBus eventBus = isGlobal ? globalEventBus : origin.getEventBus();
109
+        final WindowUpdater updater = new WindowUpdater(eventBus, window);
110
+        eventBus.register(updater);
111
+        window.addCloseListener(updater);
112
+    }
113
+
114
+    /**
115
+     * Updates a custom window with details of each event received on an event bus.
116
+     */
117
+    private static class WindowUpdater implements FrameCloseListener {
118
+
119
+        private final EventBus eventBus;
120
+        private final FrameContainer target;
121
+
122
+        WindowUpdater(final EventBus eventBus, final FrameContainer target) {
123
+            this.eventBus = eventBus;
124
+            this.target = target;
125
+        }
126
+
127
+        @Subscribe
128
+        public void handleEvent(final DMDircEvent event) {
129
+            if (event instanceof ClientLineAddedEvent
130
+                    && ((ClientLineAddedEvent) event).getFrameContainer() == target) {
131
+                // Don't add a line every time we add a line to our output window.
132
+                // Things will explode otherwise.
133
+                return;
134
+            }
135
+
136
+            final StringBuffer output = new StringBuffer();
137
+            output.append(Styliser.CODE_BOLD)
138
+                    .append(event.getClass().getSimpleName())
139
+                    .append(Styliser.CODE_BOLD);
140
+
141
+            for (Method method : event.getClass().getDeclaredMethods()) {
142
+                if (method.getName().startsWith("get") && method.getParameterCount() == 0) {
143
+                    try {
144
+                        output.append(' ')
145
+                                .append(Styliser.CODE_UNDERLINE)
146
+                                .append(method.getName().substring(3))
147
+                                .append(Styliser.CODE_UNDERLINE)
148
+                                .append('=')
149
+                                .append(method.invoke(event).toString());
150
+                    } catch (ReflectiveOperationException ex) {
151
+                        // Ignore.
152
+                    }
153
+                }
154
+            }
155
+
156
+            target.addLine(FORMAT_OUTPUT, output.toString());
157
+        }
158
+
159
+        @Override
160
+        public void windowClosing(final FrameContainer window) {
161
+            eventBus.unregister(this);
162
+        }
163
+
164
+    }
165
+}

Loading…
Annulla
Salva