Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ServerManager.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (c) 2006-2015 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.config.profiles.Profile;
  25. import com.dmdirc.config.profiles.ProfileManager;
  26. import com.dmdirc.events.UserErrorEvent;
  27. import com.dmdirc.interfaces.CommandController;
  28. import com.dmdirc.interfaces.Connection;
  29. import com.dmdirc.interfaces.ConnectionManager;
  30. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  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 java.util.stream.Collectors;
  45. import javax.inject.Inject;
  46. import javax.inject.Provider;
  47. import javax.inject.Singleton;
  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 manager to use to find profiles. */
  57. private final ProfileManager profileManager;
  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 profileManager The manager 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 ProfileManager profileManager,
  81. final IdentityFactory identityFactory,
  82. final Provider<CommandController> commandController,
  83. final WindowManager windowManager,
  84. final ServerFactoryImpl serverFactory,
  85. final DMDircMBassador eventBus) {
  86. this.profileManager = profileManager;
  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 Profile 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. return server;
  108. }
  109. /**
  110. * Registers a new server with the manager.
  111. *
  112. * @param server The server to be registered
  113. */
  114. public void registerServer(final Server server) {
  115. servers.add(server);
  116. }
  117. /**
  118. * Unregisters a server from the manager. The request is ignored if the ServerManager is in the
  119. * process of closing all servers.
  120. *
  121. * @param server The server to be unregistered
  122. */
  123. public void unregisterServer(final Server server) {
  124. servers.remove(server);
  125. }
  126. @Override
  127. public List<Connection> getConnections() {
  128. return new ArrayList<>(servers);
  129. }
  130. @Override
  131. public void disconnectAll(final String message) {
  132. for (Server server : servers) {
  133. server.disconnect(message);
  134. }
  135. }
  136. @Override
  137. public void closeAll(final String message) {
  138. for (Server server : servers) {
  139. server.disconnect(message);
  140. server.close();
  141. }
  142. }
  143. @Override
  144. public int getConnectionCount() {
  145. return servers.size();
  146. }
  147. @Override
  148. public List<Connection> getConnectionsByNetwork(final String network) {
  149. return servers.stream()
  150. .filter(server -> server.isNetwork(network))
  151. .collect(Collectors.toList());
  152. }
  153. @Override
  154. public Connection connectToAddress(final URI uri) {
  155. return connectToAddress(uri, profileManager.getDefault());
  156. }
  157. @Override
  158. public Connection connectToAddress(final URI uri, final Profile profile) {
  159. final Server server = servers.stream()
  160. .filter(s -> s.compareURI(uri)).findAny()
  161. .orElse(createServer(uri, profile));
  162. if (server.getState().isDisconnected()) {
  163. server.connect(uri, profile);
  164. } else {
  165. final Collection<? extends ChannelJoinRequest> joinRequests =
  166. server.getParser().get().extractChannels(uri);
  167. server.join(joinRequests.toArray(new ChannelJoinRequest[joinRequests.size()]));
  168. }
  169. return server;
  170. }
  171. @Override
  172. public void joinDevChat() {
  173. final List<Connection> qnetServers = getConnectionsByNetwork("Quakenet");
  174. Connection connectedServer = null;
  175. for (Connection server : qnetServers) {
  176. if (server.getState() == ServerState.CONNECTED) {
  177. connectedServer = server;
  178. if (server.getChannel("#DMDirc").isPresent()) {
  179. server.join(new ChannelJoinRequest("#DMDirc"));
  180. return;
  181. }
  182. }
  183. }
  184. if (connectedServer == null) {
  185. try {
  186. connectToAddress(new URI("irc://irc.quakenet.org/DMDirc"));
  187. } catch (URISyntaxException ex) {
  188. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
  189. "Unable to construct new server", ""));
  190. }
  191. } else {
  192. connectedServer.join(new ChannelJoinRequest("#DMDirc"));
  193. }
  194. }
  195. }