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

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