Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ServerManager.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.events.UserErrorEvent;
  25. import com.dmdirc.interfaces.CommandController;
  26. import com.dmdirc.interfaces.Connection;
  27. import com.dmdirc.interfaces.ConnectionManager;
  28. import com.dmdirc.interfaces.config.ConfigProvider;
  29. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  30. import com.dmdirc.interfaces.config.IdentityController;
  31. import com.dmdirc.interfaces.config.IdentityFactory;
  32. import com.dmdirc.logger.ErrorLevel;
  33. import com.dmdirc.parser.common.ChannelJoinRequest;
  34. import com.dmdirc.ui.WindowManager;
  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 ConnectionManager {
  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 DMDircMBassador 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 DMDircMBassador 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. @Override
  130. public List<Connection> getConnections() {
  131. return new ArrayList<Connection>(servers);
  132. }
  133. @Override
  134. public void disconnectAll(final String message) {
  135. for (Server server : servers) {
  136. server.disconnect(message);
  137. }
  138. }
  139. @Override
  140. public void closeAll(final String message) {
  141. for (Server server : servers) {
  142. server.disconnect(message);
  143. server.close();
  144. }
  145. }
  146. @Override
  147. public int getConnectionCount() {
  148. return servers.size();
  149. }
  150. @Override
  151. public List<Connection> getConnectionsByNetwork(final String network) {
  152. final List<Connection> res = new ArrayList<>();
  153. for (Server server : servers) {
  154. if (server.isNetwork(network)) {
  155. res.add(server);
  156. }
  157. }
  158. return res;
  159. }
  160. @Override
  161. public Connection connectToAddress(final URI uri) {
  162. return connectToAddress(uri,
  163. identityController.getProvidersByType("profile").get(0));
  164. }
  165. @Override
  166. public Connection connectToAddress(final URI uri, final ConfigProvider profile) {
  167. checkArgument(profile.isProfile());
  168. Connection server = null;
  169. for (Server loopServer : servers) {
  170. if (loopServer.compareURI(uri)) {
  171. server = loopServer;
  172. break;
  173. }
  174. }
  175. if (server == null) {
  176. server = createServer(uri, profile);
  177. server.connect();
  178. return server;
  179. }
  180. if (server.getState().isDisconnected()) {
  181. server.connect(uri, profile);
  182. } else {
  183. final Collection<? extends ChannelJoinRequest> joinRequests =
  184. server.getParser().extractChannels(uri);
  185. server.join(joinRequests.toArray(new ChannelJoinRequest[joinRequests.size()]));
  186. }
  187. return server;
  188. }
  189. @Override
  190. public void joinDevChat() {
  191. final List<Connection> qnetServers = getConnectionsByNetwork("Quakenet");
  192. Connection connectedServer = null;
  193. for (Connection server : qnetServers) {
  194. if (server.getState() == ServerState.CONNECTED) {
  195. connectedServer = server;
  196. if (server.hasChannel("#DMDirc")) {
  197. server.join(new ChannelJoinRequest("#DMDirc"));
  198. return;
  199. }
  200. }
  201. }
  202. if (connectedServer == null) {
  203. try {
  204. connectToAddress(new URI("irc://irc.quakenet.org/DMDirc"));
  205. } catch (URISyntaxException ex) {
  206. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
  207. "Unable to construct new server", ""));
  208. }
  209. } else {
  210. connectedServer.join(new ChannelJoinRequest("#DMDirc"));
  211. }
  212. }
  213. }