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.

LagDisplayPlugin.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.lagdisplay;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.ServerState;
  26. import com.dmdirc.actions.ActionManager;
  27. import com.dmdirc.actions.CoreActionType;
  28. import com.dmdirc.actions.interfaces.ActionType;
  29. import com.dmdirc.addons.ui_swing.SwingController;
  30. import com.dmdirc.config.ConfigManager;
  31. import com.dmdirc.config.IdentityManager;
  32. import com.dmdirc.config.prefs.PluginPreferencesCategory;
  33. import com.dmdirc.config.prefs.PreferencesCategory;
  34. import com.dmdirc.config.prefs.PreferencesDialogModel;
  35. import com.dmdirc.config.prefs.PreferencesSetting;
  36. import com.dmdirc.config.prefs.PreferencesType;
  37. import com.dmdirc.interfaces.ActionListener;
  38. import com.dmdirc.interfaces.ConfigChangeListener;
  39. import com.dmdirc.plugins.Plugin;
  40. import com.dmdirc.plugins.PluginManager;
  41. import com.dmdirc.ui.WindowManager;
  42. import com.dmdirc.util.RollingList;
  43. import java.util.Date;
  44. import java.util.HashMap;
  45. import java.util.Map;
  46. import java.util.WeakHashMap;
  47. /**
  48. * Displays the current server's lag in the status bar.
  49. * @author chris
  50. */
  51. public final class LagDisplayPlugin extends Plugin implements ActionListener, ConfigChangeListener {
  52. /** The panel we use in the status bar. */
  53. private final LagDisplayPanel panel = new LagDisplayPanel(this);
  54. /** A cache of ping times. */
  55. private final Map<Server, String> pings = new WeakHashMap<Server, String>();
  56. /** Ping history. */
  57. private final Map<Server, RollingList<Long>> history
  58. = new HashMap<Server, RollingList<Long>>();
  59. /** Whether or not to show a graph in the info popup. */
  60. private boolean showGraph = true;
  61. /** Whether or not to show labels on that graph. */
  62. private boolean showLabels = true;
  63. /** The length of history to keep per-server. */
  64. private int historySize = 100;
  65. /** Creates a new instance of LagDisplayPlugin. */
  66. public LagDisplayPlugin() {
  67. super();
  68. }
  69. /** {@inheritDoc} */
  70. @Override
  71. public void onLoad() {
  72. ((SwingController) PluginManager.getPluginManager()
  73. .getPluginInfoByName("ui_swing").getPlugin())
  74. .getSwingStatusBar().addComponent(panel);
  75. IdentityManager.getGlobalConfig().addChangeListener(getDomain(), this);
  76. readConfig();
  77. ActionManager.addListener(this, CoreActionType.SERVER_GOTPING,
  78. CoreActionType.SERVER_NOPING, CoreActionType.CLIENT_FRAME_CHANGED,
  79. CoreActionType.SERVER_DISCONNECTED, CoreActionType.SERVER_PINGSENT,
  80. CoreActionType.SERVER_NUMERIC);
  81. }
  82. /**
  83. * Reads the plugin's global configuration settings.
  84. */
  85. protected void readConfig() {
  86. final ConfigManager manager = IdentityManager.getGlobalConfig();
  87. showGraph = manager.getOptionBool(getDomain(), "graph");
  88. showLabels = manager.getOptionBool(getDomain(), "labels");
  89. historySize = manager.getOptionInt(getDomain(), "history");
  90. }
  91. /**
  92. * Retrieves the history of the specified server. If there is no history,
  93. * a new list is added to the history map and returned.
  94. *
  95. * @param server The server whose history is being requested
  96. * @return The history for the specified server
  97. */
  98. protected RollingList<Long> getHistory(final Server server) {
  99. if (!history.containsKey(server)) {
  100. history.put(server, new RollingList<Long>(historySize));
  101. }
  102. return history.get(server);
  103. }
  104. /**
  105. * Determines if the {@link ServerInfoDialog} should show a graph of the
  106. * ping time for the current server.
  107. *
  108. * @return True if a graph should be shown, false otherwise
  109. */
  110. public boolean shouldShowGraph() {
  111. return showGraph;
  112. }
  113. /**
  114. * Determines if the {@link PingHistoryPanel} should show labels on selected
  115. * points.
  116. *
  117. * @return True if labels should be shown, false otherwise
  118. */
  119. public boolean shouldShowLabels() {
  120. return showLabels;
  121. }
  122. /** {@inheritDoc} */
  123. @Override
  124. public void onUnload() {
  125. ((SwingController) PluginManager.getPluginManager()
  126. .getPluginInfoByName("ui_swing").getPlugin())
  127. .getSwingStatusBar().removeComponent(panel);
  128. IdentityManager.getConfigIdentity().removeListener(this);
  129. ActionManager.removeListener(this);
  130. }
  131. /** {@inheritDoc} */
  132. @Override
  133. public void processEvent(final ActionType type, final StringBuffer format,
  134. final Object... arguments) {
  135. boolean useAlternate = false;
  136. for (Object obj : arguments) {
  137. if (obj instanceof FrameContainer<?>
  138. && ((FrameContainer<?>) obj).getConfigManager() != null) {
  139. useAlternate = ((FrameContainer<?>) obj).getConfigManager()
  140. .getOptionBool(getDomain(), "usealternate");
  141. break;
  142. }
  143. }
  144. if (!useAlternate && type.equals(CoreActionType.SERVER_GOTPING)) {
  145. final FrameContainer<?> active = WindowManager.getActiveWindow();
  146. final String value = formatTime(arguments[1]);
  147. getHistory(((Server) arguments[0])).add((Long) arguments[1]);
  148. pings.put(((Server) arguments[0]), value);
  149. if (((Server) arguments[0]).isChild(active) || arguments[0] == active) {
  150. panel.setText(value);
  151. }
  152. panel.refreshDialog();
  153. } else if (!useAlternate && type.equals(CoreActionType.SERVER_NOPING)) {
  154. final FrameContainer<?> active = WindowManager.getActiveWindow();
  155. final String value = formatTime(arguments[1]) + "+";
  156. pings.put(((Server) arguments[0]), value);
  157. if (((Server) arguments[0]).isChild(active) || arguments[0] == active) {
  158. panel.setText(value);
  159. }
  160. panel.refreshDialog();
  161. } else if (type.equals(CoreActionType.SERVER_DISCONNECTED)) {
  162. final FrameContainer<?> active = WindowManager.getActiveWindow();
  163. if (((Server) arguments[0]).isChild(active) || arguments[0] == active) {
  164. panel.setText("Not connected");
  165. pings.remove((Server) arguments[0]);
  166. }
  167. panel.refreshDialog();
  168. } else if (type.equals(CoreActionType.CLIENT_FRAME_CHANGED)) {
  169. final FrameContainer<?> source = (FrameContainer<?>) arguments[0];
  170. if (source == null || source.getServer() == null) {
  171. panel.setText("Unknown");
  172. } else if (source.getServer().getState() != ServerState.CONNECTED) {
  173. panel.setText("Not connected");
  174. } else {
  175. panel.setText(getTime(source.getServer()));
  176. }
  177. panel.refreshDialog();
  178. } else if (useAlternate && type.equals(CoreActionType.SERVER_PINGSENT)) {
  179. ((Server) arguments[0]).getParser().sendRawMessage("LAGCHECK_" + new Date().getTime());
  180. } else if (useAlternate && type.equals(CoreActionType.SERVER_NUMERIC)
  181. && ((Integer) arguments[1]).intValue() == 421
  182. && ((String[]) arguments[2])[3].startsWith("LAGCHECK_")) {
  183. try {
  184. final long sent = Long.parseLong(((String[]) arguments[2])[3].substring(9));
  185. final Long duration = Long.valueOf(new Date().getTime() - sent);
  186. final String value = formatTime(duration);
  187. final FrameContainer<?> active = WindowManager.getActiveWindow();
  188. pings.put((Server) arguments[0], value);
  189. getHistory(((Server) arguments[0])).add(duration);
  190. if (((Server) arguments[0]).isChild(active) || arguments[0] == active) {
  191. panel.setText(value);
  192. }
  193. } catch (NumberFormatException ex) {
  194. pings.remove((Server) arguments[0]);
  195. }
  196. if (format != null) {
  197. format.delete(0, format.length());
  198. }
  199. panel.refreshDialog();
  200. }
  201. }
  202. /**
  203. * Retrieves the ping time for the specified server.
  204. *
  205. * @param server The server whose ping time is being requested
  206. * @return A String representation of the current lag, or "Unknown"
  207. */
  208. public String getTime(final Server server) {
  209. return pings.get(server) == null ? "Unknown" : pings.get(server);
  210. }
  211. /**
  212. * Formats the specified time so it's a nice size to display in the label.
  213. * @param object An uncast Long representing the time to be formatted
  214. * @return Formatted time string
  215. */
  216. protected String formatTime(final Object object) {
  217. final Long time = (Long) object;
  218. if (time >= 10000) {
  219. return Math.round(time / 1000.0) + "s";
  220. } else {
  221. return time + "ms";
  222. }
  223. }
  224. /** {@inheritDoc} */
  225. @Override
  226. public void showConfig(final PreferencesDialogModel manager) {
  227. final PreferencesCategory cat = new PluginPreferencesCategory(
  228. getPluginInfo(), "Lag display plugin", "");
  229. cat.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  230. getDomain(), "usealternate",
  231. "Alternate method", "Use an alternate method of determining "
  232. + "lag which bypasses bouncers or proxies that may reply?"));
  233. cat.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  234. getDomain(), "graph", "Show graph", "Show a graph of ping times " +
  235. "for the current server in the information popup?"));
  236. cat.addSetting(new PreferencesSetting(PreferencesType.BOOLEAN,
  237. getDomain(), "labels", "Show labels", "Show labels on selected " +
  238. "points on the ping graph?"));
  239. cat.addSetting(new PreferencesSetting(PreferencesType.INTEGER,
  240. getDomain(), "history", "Graph points", "Number of data points " +
  241. "to plot on the graph, if enabled."));
  242. manager.getCategory("Plugins").addSubCategory(cat);
  243. }
  244. /** {@inheritDoc} */
  245. @Override
  246. public void configChanged(final String domain, final String key) {
  247. readConfig();
  248. }
  249. }