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

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