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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /*
  2. * Copyright (c) 2006-2013 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.actions.wrappers.AliasWrapper;
  24. import com.dmdirc.commandparser.parsers.ServerCommandParser;
  25. import com.dmdirc.config.ConfigManager;
  26. import com.dmdirc.config.Identity;
  27. import com.dmdirc.interfaces.CommandController;
  28. import com.dmdirc.interfaces.IdentityController;
  29. import com.dmdirc.interfaces.ServerFactory;
  30. import com.dmdirc.logger.ErrorLevel;
  31. import com.dmdirc.logger.Logger;
  32. import com.dmdirc.parser.common.ChannelJoinRequest;
  33. import com.dmdirc.ui.WindowManager;
  34. import java.net.URI;
  35. import java.net.URISyntaxException;
  36. import java.util.ArrayList;
  37. import java.util.List;
  38. import java.util.Set;
  39. import java.util.concurrent.CopyOnWriteArraySet;
  40. import javax.inject.Inject;
  41. import javax.inject.Provider;
  42. import javax.inject.Singleton;
  43. /**
  44. * The ServerManager maintains a list of all servers, and provides methods to
  45. * search or iterate over them.
  46. */
  47. @Singleton
  48. public class ServerManager implements ServerFactory {
  49. /** All servers that currently exist. */
  50. private final Set<Server> servers = new CopyOnWriteArraySet<>();
  51. /** Provider of {@link ParserFactory}s for servers. */
  52. private final Provider<ParserFactory> parserFactoryProvider;
  53. /** The identity controller to use to find profiles. */
  54. private final IdentityController identityController;
  55. /** A provider of {@link CommandController}s to pass to servers. */
  56. private final Provider<CommandController> commandController;
  57. /**
  58. * Creates a new instance of ServerManager.
  59. *
  60. * @param parserFactoryProvider The provider of {@link ParserFactory}s to give to servers.
  61. * @param identityController The identity controller to use to find profiles.
  62. * @param commandController A provider of {@link CommandController}s to pass to servers.
  63. */
  64. @Inject
  65. public ServerManager(
  66. final Provider<ParserFactory> parserFactoryProvider,
  67. final IdentityController identityController,
  68. final Provider<CommandController> commandController) {
  69. this.parserFactoryProvider = parserFactoryProvider;
  70. this.identityController = identityController;
  71. this.commandController = commandController;
  72. }
  73. /** {@inheritDoc} */
  74. @Override
  75. public Server createServer(final URI uri, final Identity profile) {
  76. final ConfigManager configManager = new ConfigManager(uri.getScheme(), "", "", uri.getHost());
  77. return new Server(
  78. this,
  79. configManager,
  80. new ServerCommandParser(configManager),
  81. parserFactoryProvider.get(),
  82. WindowManager.getWindowManager(),
  83. AliasWrapper.getAliasWrapper(),
  84. commandController.get(),
  85. uri,
  86. profile);
  87. }
  88. /**
  89. * Registers a new server with the manager.
  90. *
  91. * @param server The server to be registered
  92. */
  93. public void registerServer(final Server server) {
  94. servers.add(server);
  95. }
  96. /**
  97. * Unregisters a server from the manager. The request is ignored if the
  98. * ServerManager is in the process of closing all servers.
  99. *
  100. * @param server The server to be unregistered
  101. */
  102. public void unregisterServer(final Server server) {
  103. servers.remove(server);
  104. }
  105. /**
  106. * Returns a list of all servers.
  107. *
  108. * @return A list of all servers
  109. */
  110. public List<Server> getServers() {
  111. return new ArrayList<>(servers);
  112. }
  113. /**
  114. * Makes all servers disconnected with the specified quit message.
  115. *
  116. * @param message The quit message to send to the IRC servers
  117. */
  118. public void disconnectAll(final String message) {
  119. for (Server server : servers) {
  120. server.disconnect(message);
  121. }
  122. }
  123. /**
  124. * Closes all servers with a default quit message.
  125. */
  126. public void closeAll() {
  127. for (Server server : servers) {
  128. server.disconnect();
  129. server.close();
  130. }
  131. }
  132. /**
  133. * Closes all servers with the specified quit message.
  134. *
  135. * @param message The quit message to send to the IRC servers
  136. */
  137. public void closeAll(final String message) {
  138. for (Server server : servers) {
  139. server.disconnect(message);
  140. server.close();
  141. }
  142. }
  143. /**
  144. * Returns the number of servers that are registered with the manager.
  145. *
  146. * @return number of registered servers
  147. */
  148. public int numServers() {
  149. return servers.size();
  150. }
  151. /**
  152. * Retrieves a list of servers connected to the specified network.
  153. *
  154. * @param network The network to search for
  155. * @return A list of servers connected to the network
  156. */
  157. public List<Server> getServersByNetwork(final String network) {
  158. final List<Server> res = new ArrayList<>();
  159. for (Server server : servers) {
  160. if (server.isNetwork(network)) {
  161. res.add(server);
  162. }
  163. }
  164. return res;
  165. }
  166. /**
  167. * Retrieves a list of servers connected to the specified address.
  168. *
  169. * @param address The address to search for
  170. * @return A list of servers connected to the network
  171. */
  172. public List<Server> getServersByAddress(final String address) {
  173. final List<Server> res = new ArrayList<>();
  174. for (Server server : servers) {
  175. if (server.getAddress().equalsIgnoreCase(address)) {
  176. res.add(server);
  177. }
  178. }
  179. return res;
  180. }
  181. /**
  182. * Creates a new server which will connect to the specified URI with the
  183. * default profile.
  184. *
  185. * @param uri The URI to connect to
  186. * @return The server which will be connecting
  187. * @since 0.6.3
  188. */
  189. public Server connectToAddress(final URI uri) {
  190. return connectToAddress(uri,
  191. identityController.getIdentitiesByType("profile").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.3
  201. */
  202. public Server connectToAddress(final URI uri, final Identity profile) {
  203. Logger.assertTrue(profile.isProfile());
  204. Server server = null;
  205. for (Server loopServer : servers) {
  206. if (loopServer.compareURI(uri)) {
  207. server = loopServer;
  208. break;
  209. }
  210. }
  211. if (server == null) {
  212. server = createServer(uri, profile);
  213. server.connect();
  214. return server;
  215. }
  216. if (server.getState().isDisconnected()) {
  217. server.connect(uri, profile);
  218. } else {
  219. server.join(server.getParser().extractChannels(uri)
  220. .toArray(new ChannelJoinRequest[0]));
  221. }
  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(new ChannelJoinRequest("#DMDirc"));
  235. return;
  236. }
  237. }
  238. }
  239. if (connectedServer == null) {
  240. try {
  241. connectToAddress(new URI("irc://irc.quakenet.org/DMDirc"));
  242. } catch (URISyntaxException ex) {
  243. Logger.appError(ErrorLevel.MEDIUM, "Unable to construct new server", ex);
  244. }
  245. } else {
  246. connectedServer.join(new ChannelJoinRequest("#DMDirc"));
  247. }
  248. }
  249. }