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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.FrameClosingEvent;
  27. import com.dmdirc.events.UserErrorEvent;
  28. import com.dmdirc.interfaces.CommandController;
  29. import com.dmdirc.interfaces.Connection;
  30. import com.dmdirc.interfaces.ConnectionManager;
  31. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  32. import com.dmdirc.interfaces.config.IdentityFactory;
  33. import com.dmdirc.logger.ErrorLevel;
  34. import com.dmdirc.parser.common.ChannelJoinRequest;
  35. import com.dmdirc.ui.WindowManager;
  36. import com.google.common.util.concurrent.ThreadFactoryBuilder;
  37. import java.net.URI;
  38. import java.net.URISyntaxException;
  39. import java.util.ArrayList;
  40. import java.util.Collection;
  41. import java.util.List;
  42. import java.util.Set;
  43. import java.util.concurrent.CopyOnWriteArraySet;
  44. import java.util.concurrent.Executors;
  45. import java.util.stream.Collectors;
  46. import javax.inject.Inject;
  47. import javax.inject.Provider;
  48. import javax.inject.Singleton;
  49. import net.engio.mbassy.listener.Handler;
  50. /**
  51. * The ServerManager maintains a list of all servers, and provides methods to search or iterate over
  52. * them.
  53. */
  54. @Singleton
  55. public class ServerManager implements ConnectionManager {
  56. /** All servers that currently exist. */
  57. private final Set<Server> servers = new CopyOnWriteArraySet<>();
  58. /** The manager to use to find profiles. */
  59. private final ProfileManager profileManager;
  60. /** A provider of {@link CommandController}s to pass to servers. */
  61. private final Provider<CommandController> commandController;
  62. /** The identity factory to give to servers. */
  63. private final IdentityFactory identityFactory;
  64. /** Window manager to add new servers to. */
  65. private final WindowManager windowManager;
  66. /** Concrete server factory to use. */
  67. private final ServerFactoryImpl serverFactoryImpl;
  68. /** Event bus for servers. */
  69. private final DMDircMBassador eventBus;
  70. /**
  71. * Creates a new instance of ServerManager.
  72. *
  73. * @param profileManager The manager to use to find profiles.
  74. * @param identityFactory The factory to use to create new identities.
  75. * @param commandController A provider of {@link CommandController}s to pass to servers.
  76. * @param windowManager Window manager to add new servers to.
  77. * @param serverFactory The factory to use to create servers.
  78. * @param eventBus The event bus to pass to servers.
  79. */
  80. @Inject
  81. public ServerManager(
  82. final ProfileManager profileManager,
  83. final IdentityFactory identityFactory,
  84. final Provider<CommandController> commandController,
  85. final WindowManager windowManager,
  86. final ServerFactoryImpl serverFactory,
  87. final DMDircMBassador eventBus) {
  88. this.profileManager = profileManager;
  89. this.identityFactory = identityFactory;
  90. this.commandController = commandController;
  91. this.windowManager = windowManager;
  92. this.serverFactoryImpl = serverFactory;
  93. this.eventBus = eventBus;
  94. this.eventBus.subscribe(this);
  95. }
  96. @Override
  97. public Server createServer(final URI uri, final Profile profile) {
  98. final ConfigProviderMigrator configProvider = identityFactory.createMigratableConfig(uri.
  99. getScheme(), "", "", uri.getHost());
  100. final Server server = serverFactoryImpl.getServer(
  101. configProvider,
  102. new ServerCommandParser(configProvider.getConfigProvider(), commandController.get(),
  103. eventBus),
  104. Executors.newScheduledThreadPool(1,
  105. new ThreadFactoryBuilder().setNameFormat("server-timer-%d").build()),
  106. uri,
  107. profile);
  108. registerServer(server);
  109. windowManager.addWindow(server);
  110. return server;
  111. }
  112. /**
  113. * Registers a new server with the manager.
  114. *
  115. * @param server The server to be registered
  116. */
  117. 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. void unregisterServer(final Server server) {
  127. servers.remove(server);
  128. }
  129. @Override
  130. public List<Connection> getConnections() {
  131. return new ArrayList<>(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. return servers.stream()
  153. .filter(server -> server.isNetwork(network))
  154. .collect(Collectors.toList());
  155. }
  156. @Override
  157. public Connection connectToAddress(final URI uri) {
  158. return connectToAddress(uri, profileManager.getDefault());
  159. }
  160. @Override
  161. public Connection connectToAddress(final URI uri, final Profile profile) {
  162. final Server server = servers.stream()
  163. .filter(s -> s.compareURI(uri)).findAny()
  164. .orElse(createServer(uri, profile));
  165. if (server.getState().isDisconnected()) {
  166. server.connect(uri, profile);
  167. } else {
  168. final Collection<? extends ChannelJoinRequest> joinRequests =
  169. server.getParser().get().extractChannels(uri);
  170. server.getGroupChatManager()
  171. .join(joinRequests.toArray(new ChannelJoinRequest[joinRequests.size()]));
  172. }
  173. return server;
  174. }
  175. @Override
  176. public void joinDevChat() {
  177. final List<Connection> qnetServers = getConnectionsByNetwork("Quakenet");
  178. Connection connectedServer = null;
  179. for (Connection server : qnetServers) {
  180. if (server.getState() == ServerState.CONNECTED) {
  181. connectedServer = server;
  182. if (server.getGroupChatManager().getChannel("#DMDirc").isPresent()) {
  183. server.getGroupChatManager().join(new ChannelJoinRequest("#DMDirc"));
  184. return;
  185. }
  186. }
  187. }
  188. if (connectedServer == null) {
  189. try {
  190. connectToAddress(new URI("irc://irc.quakenet.org/DMDirc"));
  191. } catch (URISyntaxException ex) {
  192. eventBus.publishAsync(new UserErrorEvent(ErrorLevel.MEDIUM, ex,
  193. "Unable to construct new server", ""));
  194. }
  195. } else {
  196. connectedServer.getGroupChatManager().join(new ChannelJoinRequest("#DMDirc"));
  197. }
  198. }
  199. @Handler
  200. void handleWindowClosing(final FrameClosingEvent event) {
  201. if (event.getContainer() instanceof Server) {
  202. unregisterServer((Server) event.getContainer());
  203. }
  204. }
  205. }