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

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