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

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