|
@@ -22,263 +22,53 @@
|
22
|
22
|
|
23
|
23
|
package com.dmdirc.addons.lagdisplay;
|
24
|
24
|
|
25
|
|
-import com.dmdirc.FrameContainer;
|
26
|
|
-import com.dmdirc.ServerState;
|
27
|
|
-import com.dmdirc.actions.ActionManager;
|
28
|
|
-import com.dmdirc.actions.CoreActionType;
|
29
|
|
-import com.dmdirc.addons.ui_swing.SelectionListener;
|
30
|
|
-import com.dmdirc.addons.ui_swing.SwingController;
|
31
|
|
-import com.dmdirc.addons.ui_swing.components.frames.TextFrame;
|
32
|
25
|
import com.dmdirc.config.prefs.PluginPreferencesCategory;
|
33
|
26
|
import com.dmdirc.config.prefs.PreferencesCategory;
|
34
|
27
|
import com.dmdirc.config.prefs.PreferencesDialogModel;
|
35
|
28
|
import com.dmdirc.config.prefs.PreferencesSetting;
|
36
|
29
|
import com.dmdirc.config.prefs.PreferencesType;
|
37
|
|
-import com.dmdirc.interfaces.ActionListener;
|
38
|
|
-import com.dmdirc.interfaces.Connection;
|
39
|
|
-import com.dmdirc.interfaces.actions.ActionType;
|
40
|
|
-import com.dmdirc.interfaces.config.AggregateConfigProvider;
|
41
|
|
-import com.dmdirc.interfaces.config.ConfigChangeListener;
|
42
|
30
|
import com.dmdirc.plugins.PluginInfo;
|
43
|
31
|
import com.dmdirc.plugins.implementations.BasePlugin;
|
44
|
|
-import com.dmdirc.util.collections.RollingList;
|
45
|
|
-
|
46
|
|
-import java.util.Date;
|
47
|
|
-import java.util.HashMap;
|
48
|
|
-import java.util.Map;
|
49
|
|
-import java.util.WeakHashMap;
|
50
|
32
|
|
51
|
33
|
import dagger.ObjectGraph;
|
52
|
34
|
|
53
|
35
|
/**
|
54
|
36
|
* Displays the current server's lag in the status bar.
|
55
|
37
|
*/
|
56
|
|
-public final class LagDisplayPlugin extends BasePlugin implements
|
57
|
|
- ActionListener, ConfigChangeListener, SelectionListener {
|
|
38
|
+public final class LagDisplayPlugin extends BasePlugin {
|
58
|
39
|
|
59
|
|
- /** The panel we use in the status bar. */
|
60
|
|
- private LagDisplayPanel panel;
|
61
|
|
- /** A cache of ping times. */
|
62
|
|
- private final Map<Connection, String> pings = new WeakHashMap<>();
|
63
|
|
- /** Ping history. */
|
64
|
|
- private final Map<Connection, RollingList<Long>> history = new HashMap<>();
|
65
|
|
- /** Parent Swing UI. */
|
66
|
|
- private final SwingController controller;
|
67
|
|
- /** Whether or not to show a graph in the info popup. */
|
68
|
|
- private boolean showGraph = true;
|
69
|
|
- /** Whether or not to show labels on that graph. */
|
70
|
|
- private boolean showLabels = true;
|
71
|
|
- /** The length of history to keep per-server. */
|
72
|
|
- private int historySize = 100;
|
73
|
40
|
/** This plugin's plugin info. */
|
74
|
41
|
private final PluginInfo pluginInfo;
|
75
|
|
- /** Global config. */
|
76
|
|
- private AggregateConfigProvider config;
|
|
42
|
+ /** The manager currently in use. */
|
|
43
|
+ private LagDisplayManager manager;
|
77
|
44
|
|
78
|
45
|
/**
|
79
|
46
|
* Creates a new LagDisplayPlugin.
|
80
|
47
|
*
|
81
|
|
- * @param controller The controller to add components to
|
82
|
48
|
* @param pluginInfo This plugin's plugin info
|
83
|
49
|
*/
|
84
|
|
- public LagDisplayPlugin(final SwingController controller,
|
85
|
|
- final PluginInfo pluginInfo) {
|
86
|
|
- super();
|
87
|
|
- this.controller = controller;
|
|
50
|
+ public LagDisplayPlugin(final PluginInfo pluginInfo) {
|
88
|
51
|
this.pluginInfo = pluginInfo;
|
89
|
|
- config = controller.getGlobalConfig();
|
90
|
52
|
}
|
91
|
53
|
|
92
|
54
|
@Override
|
93
|
55
|
public void load(final PluginInfo pluginInfo, final ObjectGraph graph) {
|
94
|
56
|
super.load(pluginInfo, graph);
|
95
|
57
|
|
96
|
|
- setObjectGraph(graph.plus(new LagDisplayModule(this)));
|
97
|
|
- panel = getObjectGraph().get(LagDisplayPanel.class);
|
|
58
|
+ setObjectGraph(graph.plus(new LagDisplayModule(pluginInfo.getDomain())));
|
|
59
|
+ manager = getObjectGraph().get(LagDisplayManager.class);
|
98
|
60
|
}
|
99
|
61
|
|
100
|
62
|
/** {@inheritDoc} */
|
101
|
63
|
@Override
|
102
|
64
|
public void onLoad() {
|
103
|
|
- controller.getSwingStatusBar().addComponent(panel);
|
104
|
|
- controller.getMainFrame().addSelectionListener(this);
|
105
|
|
- config.addChangeListener(getDomain(), this);
|
106
|
|
- readConfig();
|
107
|
|
- ActionManager.getActionManager().registerListener(this,
|
108
|
|
- CoreActionType.SERVER_GOTPING, CoreActionType.SERVER_NOPING,
|
109
|
|
- CoreActionType.SERVER_DISCONNECTED,
|
110
|
|
- CoreActionType.SERVER_PINGSENT, CoreActionType.SERVER_NUMERIC);
|
|
65
|
+ manager.load();
|
111
|
66
|
}
|
112
|
67
|
|
113
|
68
|
/** {@inheritDoc} */
|
114
|
69
|
@Override
|
115
|
70
|
public void onUnload() {
|
116
|
|
- controller.getMainFrame().removeSelectionListener(this);
|
117
|
|
- controller.getSwingStatusBar().removeComponent(panel);
|
118
|
|
- config.removeListener(this);
|
119
|
|
- ActionManager.getActionManager().unregisterListener(this);
|
120
|
|
- }
|
121
|
|
-
|
122
|
|
- /** Reads the plugin's global configuration settings. */
|
123
|
|
- private void readConfig() {
|
124
|
|
- showGraph = config.getOptionBool(getDomain(), "graph");
|
125
|
|
- showLabels = config.getOptionBool(getDomain(), "labels");
|
126
|
|
- historySize = config.getOptionInt(getDomain(), "history");
|
127
|
|
- }
|
128
|
|
-
|
129
|
|
- /**
|
130
|
|
- * Retrieves the history of the specified server. If there is no history, a new list is added to
|
131
|
|
- * the history map and returned.
|
132
|
|
- *
|
133
|
|
- * @param connection The connection whose history is being requested
|
134
|
|
- *
|
135
|
|
- * @return The history for the specified server
|
136
|
|
- */
|
137
|
|
- protected RollingList<Long> getHistory(final Connection connection) {
|
138
|
|
- if (!history.containsKey(connection)) {
|
139
|
|
- history.put(connection, new RollingList<Long>(historySize));
|
140
|
|
- }
|
141
|
|
- return history.get(connection);
|
142
|
|
- }
|
143
|
|
-
|
144
|
|
- /**
|
145
|
|
- * Determines if the {@link ServerInfoDialog} should show a graph of the ping time for the
|
146
|
|
- * current server.
|
147
|
|
- *
|
148
|
|
- * @return True if a graph should be shown, false otherwise
|
149
|
|
- */
|
150
|
|
- public boolean shouldShowGraph() {
|
151
|
|
- return showGraph;
|
152
|
|
- }
|
153
|
|
-
|
154
|
|
- /**
|
155
|
|
- * Determines if the {@link PingHistoryPanel} should show labels on selected points.
|
156
|
|
- *
|
157
|
|
- * @return True if labels should be shown, false otherwise
|
158
|
|
- */
|
159
|
|
- public boolean shouldShowLabels() {
|
160
|
|
- return showLabels;
|
161
|
|
- }
|
162
|
|
-
|
163
|
|
- /** {@inheritDoc} */
|
164
|
|
- @Override
|
165
|
|
- public void selectionChanged(final TextFrame window) {
|
166
|
|
- final FrameContainer source = window.getContainer();
|
167
|
|
- if (source == null || source.getConnection() == null) {
|
168
|
|
- panel.getComponent().setText("Unknown");
|
169
|
|
- } else if (source.getConnection().getState() != ServerState.CONNECTED) {
|
170
|
|
- panel.getComponent().setText("Not connected");
|
171
|
|
- } else {
|
172
|
|
- panel.getComponent().setText(getTime(source.getConnection()));
|
173
|
|
- }
|
174
|
|
- panel.refreshDialog();
|
175
|
|
- }
|
176
|
|
-
|
177
|
|
- /** {@inheritDoc} */
|
178
|
|
- @Override
|
179
|
|
- public void processEvent(final ActionType type, final StringBuffer format,
|
180
|
|
- final Object... arguments) {
|
181
|
|
- boolean useAlternate = false;
|
182
|
|
-
|
183
|
|
- for (Object obj : arguments) {
|
184
|
|
- if (obj instanceof FrameContainer
|
185
|
|
- && ((FrameContainer) obj).getConfigManager() != null) {
|
186
|
|
- useAlternate = ((FrameContainer) obj).getConfigManager()
|
187
|
|
- .getOptionBool(getDomain(), "usealternate");
|
188
|
|
- break;
|
189
|
|
- }
|
190
|
|
- }
|
191
|
|
-
|
192
|
|
- final TextFrame activeFrame = controller.getMainFrame().getActiveFrame();
|
193
|
|
- final FrameContainer active = activeFrame == null ? null
|
194
|
|
- : activeFrame.getContainer();
|
195
|
|
- final boolean isActive = active != null
|
196
|
|
- && arguments[0] instanceof Connection
|
197
|
|
- && ((Connection) arguments[0]).equals(active.getConnection());
|
198
|
|
-
|
199
|
|
- if (!useAlternate && type.equals(CoreActionType.SERVER_GOTPING)) {
|
200
|
|
- final String value = formatTime(arguments[1]);
|
201
|
|
-
|
202
|
|
- getHistory(((Connection) arguments[0])).add((Long) arguments[1]);
|
203
|
|
- pings.put(((Connection) arguments[0]), value);
|
204
|
|
-
|
205
|
|
- if (isActive) {
|
206
|
|
- panel.getComponent().setText(value);
|
207
|
|
- }
|
208
|
|
-
|
209
|
|
- panel.refreshDialog();
|
210
|
|
- } else if (!useAlternate && type.equals(CoreActionType.SERVER_NOPING)) {
|
211
|
|
- final String value = formatTime(arguments[1]) + "+";
|
212
|
|
-
|
213
|
|
- pings.put(((Connection) arguments[0]), value);
|
214
|
|
-
|
215
|
|
- if (isActive) {
|
216
|
|
- panel.getComponent().setText(value);
|
217
|
|
- }
|
218
|
|
-
|
219
|
|
- panel.refreshDialog();
|
220
|
|
- } else if (type.equals(CoreActionType.SERVER_DISCONNECTED)) {
|
221
|
|
- if (isActive) {
|
222
|
|
- panel.getComponent().setText("Not connected");
|
223
|
|
- pings.remove(arguments[0]);
|
224
|
|
- }
|
225
|
|
-
|
226
|
|
- panel.refreshDialog();
|
227
|
|
- } else if (useAlternate && type.equals(CoreActionType.SERVER_PINGSENT)) {
|
228
|
|
- ((Connection) arguments[0]).getParser().sendRawMessage("LAGCHECK_" + new Date().
|
229
|
|
- getTime());
|
230
|
|
- } else if (useAlternate && type.equals(CoreActionType.SERVER_NUMERIC)
|
231
|
|
- && ((Integer) arguments[1]) == 421
|
232
|
|
- && ((String[]) arguments[2])[3].startsWith("LAGCHECK_")) {
|
233
|
|
- try {
|
234
|
|
- final long sent = Long.parseLong(((String[]) arguments[2])[3].substring(9));
|
235
|
|
- final Long duration = new Date().getTime() - sent;
|
236
|
|
- final String value = formatTime(duration);
|
237
|
|
-
|
238
|
|
- pings.put((Connection) arguments[0], value);
|
239
|
|
- getHistory(((Connection) arguments[0])).add(duration);
|
240
|
|
-
|
241
|
|
- if (isActive) {
|
242
|
|
- panel.getComponent().setText(value);
|
243
|
|
- }
|
244
|
|
- } catch (NumberFormatException ex) {
|
245
|
|
- pings.remove(arguments[0]);
|
246
|
|
- }
|
247
|
|
-
|
248
|
|
- if (format != null) {
|
249
|
|
- format.delete(0, format.length());
|
250
|
|
- }
|
251
|
|
-
|
252
|
|
- panel.refreshDialog();
|
253
|
|
- }
|
254
|
|
- }
|
255
|
|
-
|
256
|
|
- /**
|
257
|
|
- * Retrieves the ping time for the specified connection.
|
258
|
|
- *
|
259
|
|
- * @param connection The connection whose ping time is being requested
|
260
|
|
- *
|
261
|
|
- * @return A String representation of the current lag, or "Unknown"
|
262
|
|
- */
|
263
|
|
- public String getTime(final Connection connection) {
|
264
|
|
- return pings.get(connection) == null ? "Unknown" : pings.get(connection);
|
265
|
|
- }
|
266
|
|
-
|
267
|
|
- /**
|
268
|
|
- * Formats the specified time so it's a nice size to display in the label.
|
269
|
|
- *
|
270
|
|
- * @param object An uncast Long representing the time to be formatted
|
271
|
|
- *
|
272
|
|
- * @return Formatted time string
|
273
|
|
- */
|
274
|
|
- protected String formatTime(final Object object) {
|
275
|
|
- final Long time = (Long) object;
|
276
|
|
-
|
277
|
|
- if (time >= 10000) {
|
278
|
|
- return Math.round(time / 1000.0) + "s";
|
279
|
|
- } else {
|
280
|
|
- return time + "ms";
|
281
|
|
- }
|
|
71
|
+ manager.unload();
|
282
|
72
|
}
|
283
|
73
|
|
284
|
74
|
/** {@inheritDoc} */
|
|
@@ -287,29 +77,23 @@ public final class LagDisplayPlugin extends BasePlugin implements
|
287
|
77
|
final PreferencesCategory cat = new PluginPreferencesCategory(
|
288
|
78
|
pluginInfo, "Lag display plugin", "");
|
289
|
79
|
cat.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
290
|
|
- getDomain(), "usealternate",
|
|
80
|
+ pluginInfo.getDomain(), "usealternate",
|
291
|
81
|
"Alternate method", "Use an alternate method of determining "
|
292
|
82
|
+ "lag which bypasses bouncers or proxies that may reply?",
|
293
|
83
|
manager.getConfigManager(), manager.getIdentity()));
|
294
|
84
|
cat.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
295
|
|
- getDomain(), "graph", "Show graph", "Show a graph of ping times "
|
|
85
|
+ pluginInfo.getDomain(), "graph", "Show graph", "Show a graph of ping times "
|
296
|
86
|
+ "for the current server in the information popup?",
|
297
|
87
|
manager.getConfigManager(), manager.getIdentity()));
|
298
|
88
|
cat.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
|
299
|
|
- getDomain(), "labels", "Show labels", "Show labels on selected "
|
|
89
|
+ pluginInfo.getDomain(), "labels", "Show labels", "Show labels on selected "
|
300
|
90
|
+ "points on the ping graph?",
|
301
|
91
|
manager.getConfigManager(), manager.getIdentity()));
|
302
|
92
|
cat.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
|
303
|
|
- getDomain(), "history", "Graph points", "Number of data points "
|
|
93
|
+ pluginInfo.getDomain(), "history", "Graph points", "Number of data points "
|
304
|
94
|
+ "to plot on the graph, if enabled.",
|
305
|
95
|
manager.getConfigManager(), manager.getIdentity()));
|
306
|
96
|
manager.getCategory("Plugins").addSubCategory(cat);
|
307
|
97
|
}
|
308
|
98
|
|
309
|
|
- /** {@inheritDoc} */
|
310
|
|
- @Override
|
311
|
|
- public void configChanged(final String domain, final String key) {
|
312
|
|
- readConfig();
|
313
|
|
- }
|
314
|
|
-
|
315
|
99
|
}
|