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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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.EventBus;
  29. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  30. import com.dmdirc.interfaces.config.IdentityFactory;
  31. import com.dmdirc.parser.common.ChannelJoinRequest;
  32. import com.dmdirc.parser.interfaces.Parser;
  33. import com.dmdirc.ui.WindowManager;
  34. import com.google.common.util.concurrent.ThreadFactoryBuilder;
  35. import java.net.URI;
  36. import java.net.URISyntaxException;
  37. import java.util.ArrayList;
  38. import java.util.Collection;
  39. import java.util.List;
  40. import java.util.Optional;
  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.Singleton;
  47. import org.slf4j.Logger;
  48. import org.slf4j.LoggerFactory;
  49. import net.engio.mbassy.listener.Handler;
  50. import static com.dmdirc.util.LogUtils.USER_ERROR;
  51. /**
  52. * The ServerManager maintains a list of all servers, and provides methods to search or iterate over
  53. * them.
  54. */
  55. @Singleton
  56. public class ServerManager implements ConnectionManager {
  57. private static final Logger LOG = LoggerFactory.getLogger(ServerManager.class);
  58. /** All servers that currently exist. */
  59. private final Set<Connection> servers = new CopyOnWriteArraySet<>();
  60. /** The manager to use to find profiles. */
  61. private final ProfileManager profileManager;
  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 EventBus 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 windowManager Window manager to add new servers to.
  76. * @param serverFactory The factory to use to create servers.
  77. * @param eventBus The event bus to pass to servers.
  78. */
  79. @Inject
  80. public ServerManager(
  81. final ProfileManager profileManager,
  82. final IdentityFactory identityFactory,
  83. final WindowManager windowManager,
  84. final ServerFactoryImpl serverFactory,
  85. final EventBus eventBus) {
  86. this.profileManager = profileManager;
  87. this.identityFactory = identityFactory;
  88. this.windowManager = windowManager;
  89. this.serverFactoryImpl = serverFactory;
  90. this.eventBus = eventBus;
  91. this.eventBus.subscribe(this);
  92. }
  93. @Override
  94. public Connection createServer(final URI uri, final Profile profile) {
  95. final ConfigProviderMigrator configProvider = identityFactory.createMigratableConfig(uri.
  96. getScheme(), "", "", uri.getHost());
  97. final Connection server = serverFactoryImpl.getServer(
  98. configProvider,
  99. Executors.newScheduledThreadPool(1,
  100. new ThreadFactoryBuilder().setNameFormat("server-timer-%d").build()),
  101. uri,
  102. profile);
  103. registerServer(server);
  104. windowManager.addWindow(server.getWindowModel());
  105. return server;
  106. }
  107. /**
  108. * Registers a new server with the manager.
  109. *
  110. * @param server The server to be registered
  111. */
  112. void registerServer(final Connection server) {
  113. servers.add(server);
  114. }
  115. /**
  116. * Unregisters a server from the manager. The request is ignored if the ServerManager is in the
  117. * process of closing all servers.
  118. *
  119. * @param server The server to be unregistered
  120. */
  121. void unregisterServer(final Server server) {
  122. servers.remove(server);
  123. }
  124. @Override
  125. public List<Connection> getConnections() {
  126. return new ArrayList<>(servers);
  127. }
  128. @Override
  129. public void disconnectAll(final String message) {
  130. for (Connection server : servers) {
  131. server.disconnect(message);
  132. }
  133. }
  134. @Override
  135. public void closeAll(final String message) {
  136. for (Connection server : servers) {
  137. server.disconnect(message);
  138. server.getWindowModel().close();
  139. }
  140. }
  141. @Override
  142. public int getConnectionCount() {
  143. return servers.size();
  144. }
  145. @Override
  146. public List<Connection> getConnectionsByNetwork(final String network) {
  147. return servers.stream()
  148. .filter(server -> server.isNetwork(network))
  149. .collect(Collectors.toList());
  150. }
  151. @Override
  152. public Connection connectToAddress(final URI uri) {
  153. return connectToAddress(uri, profileManager.getDefault());
  154. }
  155. @Override
  156. public Connection connectToAddress(final URI uri, final Profile profile) {
  157. final Connection server = servers.stream()
  158. .filter(s -> s.compareURI(uri)).findAny()
  159. .orElse(createServer(uri, profile));
  160. final Optional<Parser> parser = server.getParser();
  161. if (server.getState().isDisconnected() || !parser.isPresent()) {
  162. server.connect(uri, profile);
  163. } else {
  164. final Collection<? extends ChannelJoinRequest> joinRequests =
  165. parser.get().extractChannels(uri);
  166. server.getGroupChatManager()
  167. .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.getGroupChatManager().getChannel("#DMDirc").isPresent()) {
  179. server.getGroupChatManager().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. LOG.warn(USER_ERROR, "Unable to construct new server", ex);
  189. }
  190. } else {
  191. connectedServer.getGroupChatManager().join(new ChannelJoinRequest("#DMDirc"));
  192. }
  193. }
  194. @Handler
  195. void handleWindowClosing(final FrameClosingEvent event) {
  196. if (event.getSource() instanceof Server) {
  197. unregisterServer((Server) event.getSource());
  198. }
  199. }
  200. }