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.

ServerManager.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2006-2007 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 uk.org.ownage.dmdirc;
  23. import java.util.ArrayList;
  24. import java.util.List;
  25. import javax.swing.JInternalFrame;
  26. import uk.org.ownage.dmdirc.ui.MainFrame;
  27. /**
  28. * The ServerManager maintains a list of all servers, and provides methods to
  29. * search or iterate over them.
  30. * @author chris
  31. */
  32. public final class ServerManager {
  33. /**
  34. * Singleton instance of ServerManager.
  35. */
  36. private static ServerManager me;
  37. /**
  38. * Indicates that the server manager is in the process of closing all
  39. * servers. Used to prevent concurrent access to the servers property.
  40. */
  41. private boolean closing;
  42. /**
  43. * All servers that currently exist.
  44. */
  45. private final List<Server> servers = new ArrayList<Server>();
  46. /** Creates a new instance of ServerManager. */
  47. private ServerManager() {
  48. }
  49. /**
  50. * Returns the singleton instance of ServerManager.
  51. * @return Instance of ServerManager
  52. */
  53. public static synchronized ServerManager getServerManager() {
  54. if (me == null) {
  55. me = new ServerManager();
  56. }
  57. return me;
  58. }
  59. /**
  60. * Registers a new server with the manager.
  61. * @param server The server to be registered
  62. */
  63. public void registerServer(final Server server) {
  64. servers.add(server);
  65. MainFrame.getMainFrame().getFrameManager().addServer(server);
  66. }
  67. /**
  68. * Unregisters a server from the manager. The request is ignored if the
  69. * ServerManager is in the process of closing all servers.
  70. * @param server The server to be unregistered
  71. */
  72. public void unregisterServer(final Server server) {
  73. if (!closing) {
  74. servers.remove(server);
  75. }
  76. MainFrame.getMainFrame().getFrameManager().delServer(server);
  77. }
  78. /**
  79. * Returns a list of all servers.
  80. * @return A list of all servers
  81. */
  82. public List<Server> getServers() {
  83. return servers;
  84. }
  85. /**
  86. * Makes all servers disconnected with the specified quit message.
  87. * @param message The quit message to send to the IRC servers
  88. */
  89. public void disconnectAll(final String message) {
  90. for (Server server : servers) {
  91. server.disconnect(message);
  92. }
  93. }
  94. /**
  95. * Closes all servers with the specified quit message.
  96. * @param message The quit message to send to the IRC servers
  97. */
  98. public void closeAll(final String message) {
  99. closing = true;
  100. for (Server server : servers) {
  101. server.close(message);
  102. }
  103. closing = false;
  104. servers.clear();
  105. }
  106. /**
  107. * Returns the number of servers that are registered with the manager.
  108. * @return number of registered servers
  109. */
  110. public int numServers() {
  111. return servers.size();
  112. }
  113. /**
  114. * Returns the server instance that owns the specified internal frame.
  115. * @param active The internal frame to check
  116. * @return The server associated with the internal frame
  117. */
  118. public Server getServerFromFrame(final JInternalFrame active) {
  119. for (Server server : servers) {
  120. if (server.ownsFrame(active)) {
  121. return server;
  122. }
  123. }
  124. return null;
  125. }
  126. }