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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc;
  18. import com.dmdirc.config.profiles.Profile;
  19. import com.dmdirc.config.profiles.ProfileManager;
  20. import com.dmdirc.events.FrameClosingEvent;
  21. import com.dmdirc.interfaces.Connection;
  22. import com.dmdirc.interfaces.ConnectionManager;
  23. import com.dmdirc.events.eventbus.EventBus;
  24. import com.dmdirc.interfaces.config.ConfigProviderMigrator;
  25. import com.dmdirc.interfaces.config.IdentityFactory;
  26. import com.dmdirc.parser.common.ChannelJoinRequest;
  27. import com.dmdirc.parser.interfaces.Parser;
  28. import com.dmdirc.ui.WindowManager;
  29. import com.google.common.util.concurrent.ThreadFactoryBuilder;
  30. import java.net.URI;
  31. import java.net.URISyntaxException;
  32. import java.util.ArrayList;
  33. import java.util.Collection;
  34. import java.util.List;
  35. import java.util.Optional;
  36. import java.util.Set;
  37. import java.util.concurrent.CopyOnWriteArraySet;
  38. import java.util.concurrent.Executors;
  39. import java.util.stream.Collectors;
  40. import javax.inject.Inject;
  41. import javax.inject.Singleton;
  42. import org.slf4j.Logger;
  43. import org.slf4j.LoggerFactory;
  44. import net.engio.mbassy.listener.Handler;
  45. import static com.dmdirc.util.LogUtils.USER_ERROR;
  46. /**
  47. * The ServerManager maintains a list of all servers, and provides methods to search or iterate over
  48. * them.
  49. */
  50. @Singleton
  51. public class ServerManager implements ConnectionManager {
  52. private static final Logger LOG = LoggerFactory.getLogger(ServerManager.class);
  53. /** All servers that currently exist. */
  54. private final Set<Connection> servers = new CopyOnWriteArraySet<>();
  55. /** The manager to use to find profiles. */
  56. private final ProfileManager profileManager;
  57. /** The identity factory to give to servers. */
  58. private final IdentityFactory identityFactory;
  59. /** Window manager to add new servers to. */
  60. private final WindowManager windowManager;
  61. /** Concrete server factory to use. */
  62. private final ServerFactoryImpl serverFactoryImpl;
  63. /** Event bus for servers. */
  64. private final EventBus eventBus;
  65. /**
  66. * Creates a new instance of ServerManager.
  67. *
  68. * @param profileManager The manager to use to find profiles.
  69. * @param identityFactory The factory to use to create new identities.
  70. * @param windowManager Window manager to add new servers to.
  71. * @param serverFactory The factory to use to create servers.
  72. * @param eventBus The event bus to pass to servers.
  73. */
  74. @Inject
  75. public ServerManager(
  76. final ProfileManager profileManager,
  77. final IdentityFactory identityFactory,
  78. final WindowManager windowManager,
  79. final ServerFactoryImpl serverFactory,
  80. final EventBus eventBus) {
  81. this.profileManager = profileManager;
  82. this.identityFactory = identityFactory;
  83. this.windowManager = windowManager;
  84. this.serverFactoryImpl = serverFactory;
  85. this.eventBus = eventBus;
  86. this.eventBus.subscribe(this);
  87. }
  88. @Override
  89. public Connection createServer(final URI uri, final Profile profile) {
  90. final ConfigProviderMigrator configProvider = identityFactory.createMigratableConfig(uri.
  91. getScheme(), "", "", uri.getHost());
  92. final Connection server = serverFactoryImpl.getServer(
  93. configProvider,
  94. Executors.newScheduledThreadPool(1,
  95. new ThreadFactoryBuilder().setNameFormat("server-timer-%d").build()),
  96. uri,
  97. profile);
  98. registerServer(server);
  99. windowManager.addWindow(server.getWindowModel());
  100. return server;
  101. }
  102. /**
  103. * Registers a new server with the manager.
  104. *
  105. * @param server The server to be registered
  106. */
  107. void registerServer(final Connection server) {
  108. servers.add(server);
  109. }
  110. /**
  111. * Unregisters a server from the manager. The request is ignored if the ServerManager is in the
  112. * process of closing all servers.
  113. *
  114. * @param server The server to be unregistered
  115. */
  116. void unregisterServer(final Server server) {
  117. servers.remove(server);
  118. }
  119. @Override
  120. public List<Connection> getConnections() {
  121. return new ArrayList<>(servers);
  122. }
  123. @Override
  124. public void disconnectAll(final String message) {
  125. for (Connection server : servers) {
  126. server.disconnect(message);
  127. }
  128. }
  129. @Override
  130. public void closeAll(final String message) {
  131. for (Connection server : servers) {
  132. server.disconnect(message);
  133. server.getWindowModel().close();
  134. }
  135. }
  136. @Override
  137. public int getConnectionCount() {
  138. return servers.size();
  139. }
  140. @Override
  141. public List<Connection> getConnectionsByNetwork(final String network) {
  142. return servers.stream()
  143. .filter(server -> server.isNetwork(network))
  144. .collect(Collectors.toList());
  145. }
  146. @Override
  147. public Connection connectToAddress(final URI uri) {
  148. return connectToAddress(uri, profileManager.getDefault());
  149. }
  150. @Override
  151. public Connection connectToAddress(final URI uri, final Profile profile) {
  152. final Connection server = servers.stream()
  153. .filter(s -> s.compareURI(uri)).findAny()
  154. .orElse(createServer(uri, profile));
  155. final Optional<Parser> parser = server.getParser();
  156. if (server.getState().isDisconnected() || !parser.isPresent()) {
  157. server.connect(uri, profile);
  158. } else {
  159. final Collection<? extends ChannelJoinRequest> joinRequests =
  160. parser.get().extractChannels(uri);
  161. server.getGroupChatManager()
  162. .join(joinRequests.toArray(new ChannelJoinRequest[joinRequests.size()]));
  163. }
  164. return server;
  165. }
  166. @Override
  167. public void joinDevChat() {
  168. final List<Connection> qnetServers = getConnectionsByNetwork("Quakenet");
  169. Connection connectedServer = null;
  170. for (Connection server : qnetServers) {
  171. if (server.getState() == ServerState.CONNECTED) {
  172. connectedServer = server;
  173. if (server.getGroupChatManager().getChannel("#DMDirc").isPresent()) {
  174. server.getGroupChatManager().join(new ChannelJoinRequest("#DMDirc"));
  175. return;
  176. }
  177. }
  178. }
  179. if (connectedServer == null) {
  180. try {
  181. connectToAddress(new URI("irc://irc.quakenet.org/DMDirc"));
  182. } catch (URISyntaxException ex) {
  183. LOG.warn(USER_ERROR, "Unable to construct new server", ex);
  184. }
  185. } else {
  186. connectedServer.getGroupChatManager().join(new ChannelJoinRequest("#DMDirc"));
  187. }
  188. }
  189. @Handler
  190. void handleWindowClosing(final FrameClosingEvent event) {
  191. if (event.getSource() instanceof Server) {
  192. unregisterServer((Server) event.getSource());
  193. }
  194. }
  195. }