Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

ServerManager.java 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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;
  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.ui.interfaces.Window;
  28. import java.net.URI;
  29. import java.net.URISyntaxException;
  30. import java.util.ArrayList;
  31. import java.util.List;
  32. /**
  33. * The ServerManager maintains a list of all servers, and provides methods to
  34. * search or iterate over them.
  35. *
  36. * @author chris
  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 List<Server> servers = new ArrayList<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. synchronized (servers) {
  66. servers.add(server);
  67. }
  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. synchronized (servers) {
  77. servers.remove(server);
  78. }
  79. }
  80. /**
  81. * Returns a list of all servers.
  82. *
  83. * @return A list of all servers
  84. */
  85. public List<Server> getServers() {
  86. return new ArrayList<Server>(servers);
  87. }
  88. /**
  89. * Makes all servers disconnected with the specified quit message.
  90. *
  91. * @param message The quit message to send to the IRC servers
  92. */
  93. public void disconnectAll(final String message) {
  94. synchronized (servers) {
  95. for (Server server : servers) {
  96. server.disconnect(message);
  97. }
  98. }
  99. }
  100. /**
  101. * Closes all servers with a default quit message.
  102. */
  103. public void closeAll() {
  104. synchronized (servers) {
  105. for (Server server : servers) {
  106. server.disconnect();
  107. server.close();
  108. }
  109. }
  110. }
  111. /**
  112. * Closes all servers with the specified quit message.
  113. *
  114. * @param message The quit message to send to the IRC servers
  115. */
  116. public void closeAll(final String message) {
  117. synchronized (servers) {
  118. for (Server server : servers) {
  119. server.disconnect(message);
  120. server.close();
  121. }
  122. }
  123. }
  124. /**
  125. * Returns the number of servers that are registered with the manager.
  126. *
  127. * @return number of registered servers
  128. */
  129. public int numServers() {
  130. return servers.size();
  131. }
  132. /**
  133. * Returns the server instance that owns the specified internal frame.
  134. *
  135. * @param active The internal frame to check
  136. * @return The server associated with the internal frame
  137. */
  138. public Server getServerFromFrame(final Window active) {
  139. synchronized (servers) {
  140. for (Server server : servers) {
  141. if (server.ownsFrame(active)) {
  142. return server;
  143. }
  144. }
  145. }
  146. return null;
  147. }
  148. /**
  149. * Retrieves a list of servers connected to the specified network.
  150. *
  151. * @param network The network to search for
  152. * @return A list of servers connected to the network
  153. */
  154. public List<Server> getServersByNetwork(final String network) {
  155. final List<Server> res = new ArrayList<Server>();
  156. synchronized (servers) {
  157. for (Server server : servers) {
  158. if (server.isNetwork(network)) {
  159. res.add(server);
  160. }
  161. }
  162. }
  163. return res;
  164. }
  165. /**
  166. * Retrieves a list of servers connected to the specified address.
  167. *
  168. * @param address The address to search for
  169. * @return A list of servers connected to the network
  170. */
  171. public List<Server> getServersByAddress(final String address) {
  172. final List<Server> res = new ArrayList<Server>();
  173. synchronized (servers) {
  174. for (Server server : servers) {
  175. if (server.getName().equalsIgnoreCase(address)) {
  176. res.add(server);
  177. }
  178. }
  179. }
  180. return res;
  181. }
  182. /**
  183. * Creates a new server which will connect to the specified URI with the
  184. * default profile.
  185. *
  186. * @param uri The URI to connect to
  187. * @return The server which will be connecting
  188. * @since 0.6.4
  189. */
  190. public Server connectToAddress(final URI uri) {
  191. return connectToAddress(uri, IdentityManager.getProfiles().get(0));
  192. }
  193. /**
  194. * Creates a new server which will connect to the specified URI with the
  195. * specified profile.
  196. *
  197. * @param uri The URI to connect to
  198. * @param profile The profile to use
  199. * @return The server which will be connecting
  200. * @since 0.6.4
  201. */
  202. public Server connectToAddress(final URI uri, final Identity profile) {
  203. Logger.assertTrue(profile.isProfile());
  204. Server server = null;
  205. synchronized (servers) {
  206. for (Server loopServer : servers) {
  207. if (loopServer.compareURI(uri)) {
  208. server = loopServer;
  209. break;
  210. }
  211. }
  212. }
  213. if (server == null) {
  214. server = new Server(uri, profile);
  215. server.connect();
  216. return server;
  217. }
  218. if (server.getState().isDisconnected()) {
  219. server.connect(uri, profile );
  220. }
  221. server.activateFrame();
  222. return server;
  223. }
  224. /**
  225. * Connects the user to Quakenet if neccessary and joins #DMDirc.
  226. */
  227. public void joinDevChat() {
  228. final List<Server> qnetServers = getServersByNetwork("Quakenet");
  229. Server connectedServer = null;
  230. for (Server server : qnetServers) {
  231. if (server.getState() == ServerState.CONNECTED) {
  232. connectedServer = server;
  233. if (server.hasChannel("#DMDirc")) {
  234. server.join("#DMDirc");
  235. return;
  236. }
  237. }
  238. }
  239. if (connectedServer == null) {
  240. try {
  241. final Server server = new Server(new URI("irc://irc.quakenet.org/DMDirc"),
  242. IdentityManager.getProfiles().get(0));
  243. server.connect();
  244. } catch (URISyntaxException ex) {
  245. Logger.appError(ErrorLevel.MEDIUM, "Unable to construct new server", ex);
  246. }
  247. } else {
  248. connectedServer.join("#DMDirc");
  249. }
  250. }
  251. }