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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (c) 2006-2012 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. public final class ServerManager {
  39. /** Singleton instance of ServerManager. */
  40. private static ServerManager me;
  41. /** All servers that currently exist. */
  42. private final Set<Server> servers = new CopyOnWriteArraySet<Server>();
  43. /**
  44. * Creates a new instance of ServerManager.
  45. */
  46. private ServerManager() {
  47. }
  48. /**
  49. * Returns the singleton instance of ServerManager.
  50. *
  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. *
  62. * @param server The server to be registered
  63. */
  64. public void registerServer(final Server server) {
  65. servers.add(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. *
  71. * @param server The server to be unregistered
  72. */
  73. public void unregisterServer(final Server server) {
  74. servers.remove(server);
  75. }
  76. /**
  77. * Returns a list of all servers.
  78. *
  79. * @return A list of all servers
  80. */
  81. public List<Server> getServers() {
  82. return new ArrayList<Server>(servers);
  83. }
  84. /**
  85. * Makes all servers disconnected with the specified quit message.
  86. *
  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 a default quit message.
  96. */
  97. public void closeAll() {
  98. for (Server server : servers) {
  99. server.disconnect();
  100. server.close();
  101. }
  102. }
  103. /**
  104. * Closes all servers with the specified quit message.
  105. *
  106. * @param message The quit message to send to the IRC servers
  107. */
  108. public void closeAll(final String message) {
  109. for (Server server : servers) {
  110. server.disconnect(message);
  111. server.close();
  112. }
  113. }
  114. /**
  115. * Returns the number of servers that are registered with the manager.
  116. *
  117. * @return number of registered servers
  118. */
  119. public int numServers() {
  120. return servers.size();
  121. }
  122. /**
  123. * Retrieves a list of servers connected to the specified network.
  124. *
  125. * @param network The network to search for
  126. * @return A list of servers connected to the network
  127. */
  128. public List<Server> getServersByNetwork(final String network) {
  129. final List<Server> res = new ArrayList<Server>();
  130. for (Server server : servers) {
  131. if (server.isNetwork(network)) {
  132. res.add(server);
  133. }
  134. }
  135. return res;
  136. }
  137. /**
  138. * Retrieves a list of servers connected to the specified address.
  139. *
  140. * @param address The address to search for
  141. * @return A list of servers connected to the network
  142. */
  143. public List<Server> getServersByAddress(final String address) {
  144. final List<Server> res = new ArrayList<Server>();
  145. for (Server server : servers) {
  146. if (server.getAddress().equalsIgnoreCase(address)) {
  147. res.add(server);
  148. }
  149. }
  150. return res;
  151. }
  152. /**
  153. * Creates a new server which will connect to the specified URI with the
  154. * default profile.
  155. *
  156. * @param uri The URI to connect to
  157. * @return The server which will be connecting
  158. * @since 0.6.3
  159. */
  160. public Server connectToAddress(final URI uri) {
  161. return connectToAddress(uri, IdentityManager.getIdentityManager()
  162. .getIdentitiesByType("profile").get(0));
  163. }
  164. /**
  165. * Creates a new server which will connect to the specified URI with the
  166. * specified profile.
  167. *
  168. * @param uri The URI to connect to
  169. * @param profile The profile to use
  170. * @return The server which will be connecting
  171. * @since 0.6.3
  172. */
  173. public Server connectToAddress(final URI uri, final Identity profile) {
  174. Logger.assertTrue(profile.isProfile());
  175. Server server = null;
  176. for (Server loopServer : servers) {
  177. if (loopServer.compareURI(uri)) {
  178. server = loopServer;
  179. break;
  180. }
  181. }
  182. if (server == null) {
  183. server = new Server(uri, profile);
  184. server.connect();
  185. return server;
  186. }
  187. if (server.getState().isDisconnected()) {
  188. server.connect(uri, profile);
  189. } else {
  190. server.join(server.getParser().extractChannels(uri)
  191. .toArray(new ChannelJoinRequest[0]));
  192. }
  193. return server;
  194. }
  195. /**
  196. * Connects the user to Quakenet if neccessary and joins #DMDirc.
  197. */
  198. public void joinDevChat() {
  199. final List<Server> qnetServers = getServersByNetwork("Quakenet");
  200. Server connectedServer = null;
  201. for (Server server : qnetServers) {
  202. if (server.getState() == ServerState.CONNECTED) {
  203. connectedServer = server;
  204. if (server.hasChannel("#DMDirc")) {
  205. server.join(new ChannelJoinRequest("#DMDirc"));
  206. return;
  207. }
  208. }
  209. }
  210. if (connectedServer == null) {
  211. try {
  212. final Server server = new Server(new URI("irc://irc.quakenet.org/DMDirc"),
  213. IdentityManager.getIdentityManager()
  214. .getIdentitiesByType("profile").get(0));
  215. server.connect();
  216. } catch (URISyntaxException ex) {
  217. Logger.appError(ErrorLevel.MEDIUM, "Unable to construct new server", ex);
  218. }
  219. } else {
  220. connectedServer.join(new ChannelJoinRequest("#DMDirc"));
  221. }
  222. }
  223. }