+++ /dev/null
-/*
- * 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 <http://www.gnu.org/licenses/>.
- */
-
-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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
- */
-public class ConnectionBuilder {
-
- /** The event bus. */
- private final EventBus eventBus;
-
- /** The hostname to connect to. */
- private Optional<String> 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);
- }
-
-}
--- /dev/null
+package net.pterodactylus.irc;
+
+/**
+ * Creates {@link Connection}s to IRC servers.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public interface ConnectionFactory {
+
+ Connection createConnection(String hostname, int port);
+
+}
--- /dev/null
+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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+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);
+ }
+
+}
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;
/** The event bus. */
private final EventBus eventBus;
+ private final ConnectionFactory connectionFactory;
private final ChannelBanManager channelBanManager =
new ChannelBanManager();
* 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;
}
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();
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;
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()) {