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 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Copyright (c) 2006-2011 DMDirc Developers
  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;
  23. import com.dmdirc.config.Identity;
  24. import com.dmdirc.config.IdentityManager;
  25. import com.dmdirc.logger.ErrorLevel;
  26. import com.dmdirc.logger.Logger;
  27. import com.dmdirc.parser.common.ChannelJoinRequest;
  28. import java.net.URI;
  29. import java.net.URISyntaxException;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. import java.util.Set;
  33. import java.util.concurrent.CopyOnWriteArraySet;
  34. /**
  35. * The ServerManager maintains a list of all servers, and provides methods to
  36. * search or iterate over them.
  37. *
  38. * @author chris
  39. */
  40. public final class ServerManager {
  41. /** Singleton instance of ServerManager. */
  42. private static ServerManager me;
  43. /** All servers that currently exist. */
  44. private final Set<Server> servers = new CopyOnWriteArraySet<Server>();
  45. /**
  46. * Creates a new instance of ServerManager.
  47. */
  48. private ServerManager() {
  49. }
  50. /**
  51. * Returns the singleton instance of ServerManager.
  52. *
  53. * @return Instance of ServerManager
  54. */
  55. public static synchronized ServerManager getServerManager() {
  56. if (me == null) {
  57. me = new ServerManager();
  58. }
  59. return me;
  60. }
  61. /**
  62. * Registers a new server with the manager.
  63. *
  64. * @param server The server to be registered
  65. */
  66. public void registerServer(final Server server) {
  67. servers.add(server);
  68. }
  69. /**
  70. * Unregisters a server from the manager. The request is ignored if the
  71. * ServerManager is in the process of closing all servers.
  72. *
  73. * @param server The server to be unregistered
  74. */
  75. public void unregisterServer(final Server server) {
  76. servers.remove(server);
  77. }
  78. /**
  79. * Returns a list of all servers.
  80. *
  81. * @return A list of all servers
  82. */
  83. public List<Server> getServers() {
  84. return new ArrayList<Server>(servers);
  85. }
  86. /**
  87. * Makes all servers disconnected with the specified quit message.
  88. *
  89. * @param message The quit message to send to the IRC servers
  90. */
  91. public void disconnectAll(final String message) {
  92. for (Server server : servers) {
  93. server.disconnect(message);
  94. }
  95. }
  96. /**
  97. * Closes all servers with a default quit message.
  98. */
  99. public void closeAll() {
  100. for (Server server : servers) {
  101. server.disconnect();
  102. server.close();
  103. }
  104. }
  105. /**
  106. * Closes all servers with the specified quit message.
  107. *
  108. * @param message The quit message to send to the IRC servers
  109. */
  110. public void closeAll(final String message) {
  111. for (Server server : servers) {
  112. server.disconnect(message);
  113. server.close();
  114. }
  115. }
  116. /**
  117. * Returns the number of servers that are registered with the manager.
  118. *
  119. * @return number of registered servers
  120. */
  121. public int numServers() {
  122. return servers.size();
  123. }
  124. /**
  125. * Retrieves a list of servers connected to the specified network.
  126. *
  127. * @param network The network to search for
  128. * @return A list of servers connected to the network
  129. */
  130. public List<Server> getServersByNetwork(final String network) {
  131. final List<Server> res = new ArrayList<Server>();
  132. for (Server server : servers) {
  133. if (server.isNetwork(network)) {
  134. res.add(server);
  135. }
  136. }
  137. return res;
  138. }
  139. /**
  140. * Retrieves a list of servers connected to the specified address.
  141. *
  142. * @param address The address to search for
  143. * @return A list of servers connected to the network
  144. */
  145. public List<Server> getServersByAddress(final String address) {
  146. final List<Server> res = new ArrayList<Server>();
  147. for (Server server : servers) {
  148. if (server.getAddress().equalsIgnoreCase(address)) {
  149. res.add(server);
  150. }
  151. }
  152. return res;
  153. }
  154. /**
  155. * Creates a new server which will connect to the specified URI with the
  156. * default profile.
  157. *
  158. * @param uri The URI to connect to
  159. * @return The server which will be connecting
  160. * @since 0.6.3
  161. */
  162. public Server connectToAddress(final URI uri) {
  163. return connectToAddress(uri, IdentityManager.getCustomIdentities(
  164. "profile").get(0));
  165. }
  166. /**
  167. * Creates a new server which will connect to the specified URI with the
  168. * specified profile.
  169. *
  170. * @param uri The URI to connect to
  171. * @param profile The profile to use
  172. * @return The server which will be connecting
  173. * @since 0.6.3
  174. */
  175. public Server connectToAddress(final URI uri, final Identity profile) {
  176. Logger.assertTrue(profile.isProfile());
  177. Server server = null;
  178. for (Server loopServer : servers) {
  179. if (loopServer.compareURI(uri)) {
  180. server = loopServer;
  181. break;
  182. }
  183. }
  184. if (server == null) {
  185. server = new Server(uri, profile);
  186. server.connect();
  187. return server;
  188. }
  189. if (server.getState().isDisconnected()) {
  190. server.connect(uri, profile);
  191. } else {
  192. server.join(server.getParser().extractChannels(uri)
  193. .toArray(new ChannelJoinRequest[0]));
  194. }
  195. return server;
  196. }
  197. /**
  198. * Connects the user to Quakenet if neccessary and joins #DMDirc.
  199. */
  200. public void joinDevChat() {
  201. final List<Server> qnetServers = getServersByNetwork("Quakenet");
  202. Server connectedServer = null;
  203. for (Server server : qnetServers) {
  204. if (server.getState() == ServerState.CONNECTED) {
  205. connectedServer = server;
  206. if (server.hasChannel("#DMDirc")) {
  207. server.join(new ChannelJoinRequest("#DMDirc"));
  208. return;
  209. }
  210. }
  211. }
  212. if (connectedServer == null) {
  213. try {
  214. final Server server = new Server(new URI("irc://irc.quakenet.org/DMDirc"),
  215. IdentityManager.getCustomIdentities("profile").get(0));
  216. server.connect();
  217. } catch (URISyntaxException ex) {
  218. Logger.appError(ErrorLevel.MEDIUM, "Unable to construct new server", ex);
  219. }
  220. } else {
  221. connectedServer.join(new ChannelJoinRequest("#DMDirc"));
  222. }
  223. }
  224. }