From: David ‘Bombe’ Roden Date: Mon, 17 Nov 2014 19:52:12 +0000 (+0100) Subject: Use a connection factory instead of a connection builder. X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=8593a0995d655bebada567ab98a9de2cdf078243;p=xudocci.git Use a connection factory instead of a connection builder. --- diff --git a/src/main/java/net/pterodactylus/irc/ConnectionBuilder.java b/src/main/java/net/pterodactylus/irc/ConnectionBuilder.java deleted file mode 100644 index feefe1f..0000000 --- a/src/main/java/net/pterodactylus/irc/ConnectionBuilder.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * XdccDownloader - ConnectionBuilder.java - Copyright © 2013 David Roden - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package net.pterodactylus.irc; - -import javax.net.SocketFactory; -import javax.net.ssl.SSLSocketFactory; - -import com.google.common.base.Optional; -import com.google.common.eventbus.EventBus; -import com.google.inject.Inject; - -/** - * Builder for {@link Connection}s. - * - * @author David ‘Bombe’ Roden - */ -public class ConnectionBuilder { - - /** The event bus. */ - private final EventBus eventBus; - - /** The hostname to connect to. */ - private Optional hostname = Optional.absent(); - - /** The port number to connect to. */ - private int port = 6666; - - /** Whether to use an SSL connection. */ - private boolean secure = false; - - /** - * Creates a new connection builder. - * - * @param eventBus - * The event bus - */ - @Inject - public ConnectionBuilder(EventBus eventBus) { - this.eventBus = eventBus; - } - - /** - * Configures this builder to use the given hostname for the connection. - * - * @param hostname - * The hostname of the IRC server - * @return This builder - */ - public ConnectionBuilder connect(String hostname) { - this.hostname = Optional.fromNullable(hostname); - return this; - } - - /** - * Configures this builder to use the given port number for the connection. - * - * @param port - * The port number of the IRC server - * @return This builder - */ - public ConnectionBuilder port(int port) { - this.port = port; - return this; - } - - /** - * Configures this builder to use an SSL connection. - * - * @return This builder - */ - public ConnectionBuilder secure() { - this.secure = true; - return this; - } - - /** - * Builds the connection. - * - * @return The created connection - * @throws IllegalStateException - * if the hostname has not been set - */ - public Connection build() throws IllegalStateException { - if (!hostname.isPresent()) { - throw new IllegalStateException("hostname must not be empty"); - } - - SocketFactory socketFactory = secure ? SSLSocketFactory.getDefault() : SocketFactory.getDefault(); - return new Connection(eventBus, socketFactory, hostname.get(), port); - } - -} diff --git a/src/main/java/net/pterodactylus/irc/ConnectionFactory.java b/src/main/java/net/pterodactylus/irc/ConnectionFactory.java new file mode 100644 index 0000000..51993a7 --- /dev/null +++ b/src/main/java/net/pterodactylus/irc/ConnectionFactory.java @@ -0,0 +1,12 @@ +package net.pterodactylus.irc; + +/** + * Creates {@link Connection}s to IRC servers. + * + * @author David ‘Bombe’ Roden + */ +public interface ConnectionFactory { + + Connection createConnection(String hostname, int port); + +} diff --git a/src/main/java/net/pterodactylus/irc/DefaultConnectionFactory.java b/src/main/java/net/pterodactylus/irc/DefaultConnectionFactory.java new file mode 100644 index 0000000..6a37394 --- /dev/null +++ b/src/main/java/net/pterodactylus/irc/DefaultConnectionFactory.java @@ -0,0 +1,25 @@ +package net.pterodactylus.irc; + +import javax.net.SocketFactory; + +import com.google.common.eventbus.EventBus; + +/** + * Default {@link ConnectionFactory} implementation that uses plain sockets to create connections. + * + * @author David ‘Bombe’ Roden + */ +public class DefaultConnectionFactory implements ConnectionFactory { + + private final EventBus eventBus; + + public DefaultConnectionFactory(EventBus eventBus) { + this.eventBus = eventBus; + } + + @Override + public Connection createConnection(String hostname, int port) { + return new Connection(eventBus, SocketFactory.getDefault(), hostname, port); + } + +} diff --git a/src/main/java/net/pterodactylus/xdcc/core/Core.java b/src/main/java/net/pterodactylus/xdcc/core/Core.java index 3414d18..ab9804a 100644 --- a/src/main/java/net/pterodactylus/xdcc/core/Core.java +++ b/src/main/java/net/pterodactylus/xdcc/core/Core.java @@ -39,7 +39,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import net.pterodactylus.irc.Connection; -import net.pterodactylus.irc.ConnectionBuilder; +import net.pterodactylus.irc.ConnectionFactory; import net.pterodactylus.irc.DccReceiver; import net.pterodactylus.irc.event.ChannelJoined; import net.pterodactylus.irc.event.ChannelLeft; @@ -106,6 +106,7 @@ public class Core extends AbstractExecutionThreadService { /** The event bus. */ private final EventBus eventBus; + private final ConnectionFactory connectionFactory; private final ChannelBanManager channelBanManager = new ChannelBanManager(); @@ -147,8 +148,9 @@ public class Core extends AbstractExecutionThreadService { * The directory to move finished files to */ @Inject - public Core(EventBus eventBus, String temporaryDirectory, String finalDirectory) { + public Core(EventBus eventBus, ConnectionFactory connectionFactory, String temporaryDirectory, String finalDirectory) { this.eventBus = eventBus; + this.connectionFactory = connectionFactory; this.temporaryDirectory = temporaryDirectory; this.finalDirectory = finalDirectory; } @@ -428,7 +430,7 @@ public class Core extends AbstractExecutionThreadService { return; } Server server = servers.get((int) (Math.random() * servers.size())); - Connection connection = new ConnectionBuilder(eventBus).connect(server.hostname()).port(server.unencryptedPorts().iterator().next()).build(); + Connection connection = connectionFactory.createConnection(server.hostname(), server.unencryptedPorts().iterator().next()); connection.username(RandomNickname.get()).realName(RandomNickname.get()); networkConnections.put(network, connection); connection.start(); diff --git a/src/main/java/net/pterodactylus/xdcc/main/Main.java b/src/main/java/net/pterodactylus/xdcc/main/Main.java index 3272736..c9b7c5b 100644 --- a/src/main/java/net/pterodactylus/xdcc/main/Main.java +++ b/src/main/java/net/pterodactylus/xdcc/main/Main.java @@ -25,6 +25,8 @@ import java.util.HashSet; import java.util.concurrent.CopyOnWriteArraySet; import java.util.concurrent.Executors; +import net.pterodactylus.irc.ConnectionFactory; +import net.pterodactylus.irc.DefaultConnectionFactory; import net.pterodactylus.xdcc.core.Core; import net.pterodactylus.xdcc.data.Channel; import net.pterodactylus.xdcc.data.Download; @@ -58,8 +60,9 @@ public class Main { EventBus eventBus = new AsyncEventBus(Executors.newSingleThreadExecutor()); Configuration configuration = new ObjectMapper().readValue(new File(parameters.getConfiguration()), Configuration.class); + ConnectionFactory connectionFactory = new DefaultConnectionFactory(eventBus); - Core core = new Core(eventBus, configuration.getTemporaryDirectory(), configuration.getFinalDirectory()); + Core core = new Core(eventBus, connectionFactory, configuration.getTemporaryDirectory(), configuration.getFinalDirectory()); eventBus.register(core); for (Configuration.Network network : configuration.getNetworks()) {