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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2006-2014 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.commandparser.parsers.ServerCommandParser;
  24. import com.dmdirc.interfaces.CommandController;
  25. import com.dmdirc.interfaces.ServerFactory;
  26. import com.dmdirc.interfaces.config.ConfigProvider;
  27. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  28. import com.dmdirc.interfaces.config.IdentityController;
  29. import com.dmdirc.interfaces.config.IdentityFactory;
  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. import static com.google.common.base.Preconditions.checkArgument;
  44. /**
  45. * The ServerManager maintains a list of all servers, and provides methods to
  46. * search or iterate over them.
  47. */
  48. @Singleton
  49. public class ServerManager implements ServerFactory {
  50. /** All servers that currently exist. */
  51. private final Set<Server> servers = new CopyOnWriteArraySet<>();
  52. /** The identity controller to use to find profiles. */
  53. private final IdentityController identityController;
  54. /** A provider of {@link CommandController}s to pass to servers. */
  55. private final Provider<CommandController> commandController;
  56. /** The identity factory to give to servers. */
  57. private final IdentityFactory identityFactory;
  58. /** Window manager to add new servers to. */
  59. private final WindowManager windowManager;
  60. /** Concrete server factory to use. */
  61. private final ServerFactoryImpl serverFactoryImpl;
  62. /**
  63. * Creates a new instance of ServerManager.
  64. *
  65. * @param identityController The identity controller to use to find profiles.
  66. * @param identityFactory The factory to use to create new identities.
  67. * @param commandController A provider of {@link CommandController}s to pass to servers.
  68. * @param windowManager Window manager to add new servers to.
  69. * @param serverFactory The factory to use to create servers.
  70. */
  71. @Inject
  72. public ServerManager(
  73. final IdentityController identityController,
  74. final IdentityFactory identityFactory,
  75. final Provider<CommandController> commandController,
  76. final WindowManager windowManager,
  77. final ServerFactoryImpl serverFactory) {
  78. this.identityController = identityController;
  79. this.identityFactory = identityFactory;
  80. this.commandController = commandController;
  81. this.windowManager = windowManager;
  82. this.serverFactoryImpl = serverFactory;
  83. }
  84. /** {@inheritDoc} */
  85. @Override
  86. public Server createServer(final URI uri, final ConfigProvider profile) {
  87. final ConfigProviderMigrator configProvider =
  88. identityFactory.createMigratableConfig(uri.getScheme(), "", "", uri.getHost());
  89. final Server server = serverFactoryImpl.getServer(
  90. configProvider,
  91. new ServerCommandParser(configProvider.getConfigProvider(), commandController.get()),
  92. uri,
  93. profile);
  94. registerServer(server);
  95. windowManager.addWindow(server);
  96. if (configProvider.getConfigProvider().getOptionBool("general", "showrawwindow")) {
  97. server.addRaw();
  98. }
  99. return server;
  100. }
  101. /**
  102. * Registers a new server with the manager.
  103. *
  104. * @param server The server to be registered
  105. */
  106. public void registerServer(final Server server) {
  107. servers.add(server);
  108. }
  109. /**
  110. * Unregisters a server from the manager. The request is ignored if the
  111. * ServerManager is in the process of closing all servers.
  112. *
  113. * @param server The server to be unregistered
  114. */
  115. public void unregisterServer(final Server server) {
  116. servers.remove(server);
  117. }
  118. /**
  119. * Returns a list of all servers.
  120. *
  121. * @return A list of all servers
  122. */
  123. public List<Server> getServers() {
  124. return new ArrayList<>(servers);
  125. }
  126. /**
  127. * Makes all servers disconnected with the specified quit message.
  128. *
  129. * @param message The quit message to send to the IRC servers
  130. */
  131. public void disconnectAll(final String message) {
  132. for (Server server : servers) {
  133. server.disconnect(message);
  134. }
  135. }
  136. /**
  137. * Closes all servers with the specified quit message.
  138. *
  139. * @param message The quit message to send to the IRC servers
  140. */
  141. public void closeAll(final String message) {
  142. for (Server server : servers) {
  143. server.disconnect(message);
  144. server.close();
  145. }
  146. }
  147. /**
  148. * Returns the number of servers that are registered with the manager.
  149. *
  150. * @return number of registered servers
  151. */
  152. public int numServers() {
  153. return servers.size();
  154. }
  155. /**
  156. * Retrieves a list of servers connected to the specified network.
  157. *
  158. * @param network The network to search for
  159. * @return A list of servers connected to the network
  160. */
  161. public List<Server> getServersByNetwork(final String network) {
  162. final List<Server> res = new ArrayList<>();
  163. for (Server server : servers) {
  164. if (server.isNetwork(network)) {
  165. res.add(server);
  166. }
  167. }
  168. return res;
  169. }
  170. /**
  171. * Creates a new server which will connect to the specified URI with the
  172. * default profile.
  173. *
  174. * @param uri The URI to connect to
  175. * @return The server which will be connecting
  176. * @since 0.6.3
  177. */
  178. public Server connectToAddress(final URI uri) {
  179. return connectToAddress(uri,
  180. identityController.getProvidersByType("profile").get(0));
  181. }
  182. /**
  183. * Creates a new server which will connect to the specified URI with the
  184. * specified profile.
  185. *
  186. * @param uri The URI to connect to
  187. * @param profile The profile to use
  188. * @return The server which will be connecting
  189. * @since 0.6.3
  190. */
  191. public Server connectToAddress(final URI uri, final ConfigProvider profile) {
  192. checkArgument(profile.isProfile());
  193. Server server = null;
  194. for (Server loopServer : servers) {
  195. if (loopServer.compareURI(uri)) {
  196. server = loopServer;
  197. break;
  198. }
  199. }
  200. if (server == null) {
  201. server = createServer(uri, profile);
  202. server.connect();
  203. return server;
  204. }
  205. if (server.getState().isDisconnected()) {
  206. server.connect(uri, profile);
  207. } else {
  208. server.join(server.getParser().extractChannels(uri)
  209. .toArray(new ChannelJoinRequest[0]));
  210. }
  211. return server;
  212. }
  213. /**
  214. * Connects the user to Quakenet if neccessary and joins #DMDirc.
  215. */
  216. public void joinDevChat() {
  217. final List<Server> qnetServers = getServersByNetwork("Quakenet");
  218. Server connectedServer = null;
  219. for (Server server : qnetServers) {
  220. if (server.getState() == ServerState.CONNECTED) {
  221. connectedServer = server;
  222. if (server.hasChannel("#DMDirc")) {
  223. server.join(new ChannelJoinRequest("#DMDirc"));
  224. return;
  225. }
  226. }
  227. }
  228. if (connectedServer == null) {
  229. try {
  230. connectToAddress(new URI("irc://irc.quakenet.org/DMDirc"));
  231. } catch (URISyntaxException ex) {
  232. Logger.appError(ErrorLevel.MEDIUM, "Unable to construct new server", ex);
  233. }
  234. } else {
  235. connectedServer.join(new ChannelJoinRequest("#DMDirc"));
  236. }
  237. }
  238. }