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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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.config.profiles.Profile;
  24. import com.dmdirc.config.profiles.ProfileManager;
  25. import com.dmdirc.events.FrameClosingEvent;
  26. import com.dmdirc.interfaces.Connection;
  27. import com.dmdirc.interfaces.ConnectionManager;
  28. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  29. import com.dmdirc.interfaces.config.IdentityFactory;
  30. import com.dmdirc.parser.common.ChannelJoinRequest;
  31. import com.dmdirc.parser.interfaces.Parser;
  32. import com.dmdirc.ui.WindowManager;
  33. import com.google.common.util.concurrent.ThreadFactoryBuilder;
  34. import java.net.URI;
  35. import java.net.URISyntaxException;
  36. import java.util.ArrayList;
  37. import java.util.Collection;
  38. import java.util.List;
  39. import java.util.Optional;
  40. import java.util.Set;
  41. import java.util.concurrent.CopyOnWriteArraySet;
  42. import java.util.concurrent.Executors;
  43. import java.util.stream.Collectors;
  44. import javax.inject.Inject;
  45. import javax.inject.Singleton;
  46. import org.slf4j.Logger;
  47. import org.slf4j.LoggerFactory;
  48. import net.engio.mbassy.listener.Handler;
  49. import static com.dmdirc.util.LogUtils.USER_ERROR;
  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. private static final Logger LOG = LoggerFactory.getLogger(ServerManager.class);
  57. /** All servers that currently exist. */
  58. private final Set<Server> servers = new CopyOnWriteArraySet<>();
  59. /** The manager to use to find profiles. */
  60. private final ProfileManager profileManager;
  61. /** The identity factory to give to servers. */
  62. private final IdentityFactory identityFactory;
  63. /** Window manager to add new servers to. */
  64. private final WindowManager windowManager;
  65. /** Concrete server factory to use. */
  66. private final ServerFactoryImpl serverFactoryImpl;
  67. /** Event bus for servers. */
  68. private final DMDircMBassador eventBus;
  69. /**
  70. * Creates a new instance of ServerManager.
  71. *
  72. * @param profileManager The manager to use to find profiles.
  73. * @param identityFactory The factory to use to create new identities.
  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 WindowManager windowManager,
  83. final ServerFactoryImpl serverFactory,
  84. final DMDircMBassador eventBus) {
  85. this.profileManager = profileManager;
  86. this.identityFactory = identityFactory;
  87. this.windowManager = windowManager;
  88. this.serverFactoryImpl = serverFactory;
  89. this.eventBus = eventBus;
  90. this.eventBus.subscribe(this);
  91. }
  92. @Override
  93. public Server createServer(final URI uri, final Profile profile) {
  94. final ConfigProviderMigrator configProvider = identityFactory.createMigratableConfig(uri.
  95. getScheme(), "", "", uri.getHost());
  96. final Server server = serverFactoryImpl.getServer(
  97. configProvider,
  98. Executors.newScheduledThreadPool(1,
  99. new ThreadFactoryBuilder().setNameFormat("server-timer-%d").build()),
  100. uri,
  101. profile);
  102. registerServer(server);
  103. windowManager.addWindow(server);
  104. return server;
  105. }
  106. /**
  107. * Registers a new server with the manager.
  108. *
  109. * @param server The server to be registered
  110. */
  111. void registerServer(final Server server) {
  112. servers.add(server);
  113. }
  114. /**
  115. * Unregisters a server from the manager. The request is ignored if the ServerManager is in the
  116. * process of closing all servers.
  117. *
  118. * @param server The server to be unregistered
  119. */
  120. void unregisterServer(final Server server) {
  121. servers.remove(server);
  122. }
  123. @Override
  124. public List<Connection> getConnections() {
  125. return new ArrayList<>(servers);
  126. }
  127. @Override
  128. public void disconnectAll(final String message) {
  129. for (Server server : servers) {
  130. server.disconnect(message);
  131. }
  132. }
  133. @Override
  134. public void closeAll(final String message) {
  135. for (Server server : servers) {
  136. server.disconnect(message);
  137. server.close();
  138. }
  139. }
  140. @Override
  141. public int getConnectionCount() {
  142. return servers.size();
  143. }
  144. @Override
  145. public List<Connection> getConnectionsByNetwork(final String network) {
  146. return servers.stream()
  147. .filter(server -> server.isNetwork(network))
  148. .collect(Collectors.toList());
  149. }
  150. @Override
  151. public Connection connectToAddress(final URI uri) {
  152. return connectToAddress(uri, profileManager.getDefault());
  153. }
  154. @Override
  155. public Connection connectToAddress(final URI uri, final Profile profile) {
  156. final Server server = servers.stream()
  157. .filter(s -> s.compareURI(uri)).findAny()
  158. .orElse(createServer(uri, profile));
  159. final Optional<Parser> parser = server.getParser();
  160. if (server.getState().isDisconnected() || !parser.isPresent()) {
  161. server.connect(uri, profile);
  162. } else {
  163. final Collection<? extends ChannelJoinRequest> joinRequests =
  164. parser.get().extractChannels(uri);
  165. server.getGroupChatManager()
  166. .join(joinRequests.toArray(new ChannelJoinRequest[joinRequests.size()]));
  167. }
  168. return server;
  169. }
  170. @Override
  171. public void joinDevChat() {
  172. final List<Connection> qnetServers = getConnectionsByNetwork("Quakenet");
  173. Connection connectedServer = null;
  174. for (Connection server : qnetServers) {
  175. if (server.getState() == ServerState.CONNECTED) {
  176. connectedServer = server;
  177. if (server.getGroupChatManager().getChannel("#DMDirc").isPresent()) {
  178. server.getGroupChatManager().join(new ChannelJoinRequest("#DMDirc"));
  179. return;
  180. }
  181. }
  182. }
  183. if (connectedServer == null) {
  184. try {
  185. connectToAddress(new URI("irc://irc.quakenet.org/DMDirc"));
  186. } catch (URISyntaxException ex) {
  187. LOG.warn(USER_ERROR, "Unable to construct new server", ex);
  188. }
  189. } else {
  190. connectedServer.getGroupChatManager().join(new ChannelJoinRequest("#DMDirc"));
  191. }
  192. }
  193. @Handler
  194. void handleWindowClosing(final FrameClosingEvent event) {
  195. if (event.getContainer() instanceof Server) {
  196. unregisterServer((Server) event.getContainer());
  197. }
  198. }
  199. }