summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTarun Gupta <tarun1995gupta@gmail.com>2015-07-27 15:01:45 (GMT)
committerAlex Clayton <alex.clayton@isode.com>2016-01-12 11:31:04 (GMT)
commitb4cf2bb8d7b69d95b4a10d610ad259998d2aee5b (patch)
treeb942953236a7c712eed6ce4a5f261019691c0dca /src/com/isode/stroke/network/ChainedConnector.java
parentc168fcd0c2468ec939b8d164175e9c5776a63147 (diff)
downloadstroke-b4cf2bb8d7b69d95b4a10d610ad259998d2aee5b.zip
stroke-b4cf2bb8d7b69d95b4a10d610ad259998d2aee5b.tar.bz2
Make Networks equivalent with Swiften.
Adds ProxyProviders, DomainNameResolvers and DummyConnection. License: This patch is BSD-licensed, see Documentation/Licenses/BSD-simplified.txt for details. Test-Information: Tests added for ChainedConnector, Connector and HostAddress. Test also added for ComponentConnector, which needed bits of Network. Five assertions are commented in ConnectorTest, which fails and will be updated after review. Change-Id: I8a62841eb2f9c109bc3a94865b7a003b33493e11
Diffstat (limited to 'src/com/isode/stroke/network/ChainedConnector.java')
-rw-r--r--src/com/isode/stroke/network/ChainedConnector.java119
1 files changed, 119 insertions, 0 deletions
diff --git a/src/com/isode/stroke/network/ChainedConnector.java b/src/com/isode/stroke/network/ChainedConnector.java
new file mode 100644
index 0000000..b61133c
--- /dev/null
+++ b/src/com/isode/stroke/network/ChainedConnector.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2011-2015 Isode Limited.
+ * All rights reserved.
+ * See the COPYING file for more information.
+ */
+/*
+ * Copyright (c) 2015 Tarun Gupta.
+ * Licensed under the simplified BSD license.
+ * See Documentation/Licenses/BSD-simplified.txt for more information.
+ */
+
+package com.isode.stroke.network;
+
+import java.util.Vector;
+import java.util.logging.Logger;
+import com.isode.stroke.signals.Signal2;
+import com.isode.stroke.signals.Slot2;
+import com.isode.stroke.signals.SignalConnection;
+import com.isode.stroke.network.Connection;
+
+public class ChainedConnector {
+
+ private String hostname = "";
+ private int port;
+ private String serviceLookupPrefix;
+ private DomainNameResolver resolver;
+ private Vector<ConnectionFactory> connectionFactories = new Vector<ConnectionFactory>();
+ private TimerFactory timerFactory;
+ private int timeoutMilliseconds;
+ private Vector<ConnectionFactory> connectionFactoryQueue = new Vector<ConnectionFactory>();
+ private Connector currentConnector;
+ private com.isode.stroke.base.Error lastError;
+ private Logger logger_ = Logger.getLogger(this.getClass().getName());
+ private SignalConnection onConnectFinishedConnection;
+
+ public ChainedConnector(final String hostname, int port, final String serviceLookupPrefix, DomainNameResolver resolver, final Vector<ConnectionFactory> connectionFactories, TimerFactory timer) {
+ this.hostname = hostname;
+ this.port = port;
+ this.serviceLookupPrefix = serviceLookupPrefix;
+ this.resolver = resolver;
+ this.connectionFactories = connectionFactories;
+ this.timerFactory = timer;
+ this.timeoutMilliseconds = 0;
+ }
+
+ protected void finalize() throws Throwable {
+ try {
+ if (currentConnector != null) {
+ onConnectFinishedConnection.disconnect();
+ currentConnector.stop();
+ currentConnector = null;
+ }
+ }
+ finally {
+ super.finalize();
+ }
+ }
+
+ public void setTimeoutMilliseconds(int milliseconds) {
+ timeoutMilliseconds = milliseconds;
+ }
+
+ public void start() {
+ logger_.fine("Starting queued connector for " + hostname + "\n");
+
+ connectionFactoryQueue = new Vector<ConnectionFactory>(connectionFactories);
+ tryNextConnectionFactory();
+ }
+
+ public void stop() {
+ if (currentConnector != null) {
+ onConnectFinishedConnection.disconnect();
+ currentConnector.stop();
+ currentConnector = null;
+ }
+ finish((Connection)null, (com.isode.stroke.base.Error)null);
+ }
+
+ public final Signal2<Connection, com.isode.stroke.base.Error> onConnectFinished = new Signal2<Connection, com.isode.stroke.base.Error>();
+
+ private void finish(Connection connection, com.isode.stroke.base.Error error) {
+ onConnectFinished.emit(connection, error);
+ }
+
+ private void tryNextConnectionFactory() {
+ assert(currentConnector == null);
+ if (connectionFactoryQueue.isEmpty()) {
+ logger_.fine("No more connection factories\n");
+ finish((Connection)null, lastError);
+ }
+ else {
+ ConnectionFactory connectionFactory = connectionFactoryQueue.firstElement();
+ logger_.fine("Trying next connection factory: " + connectionFactory + "\n");
+ connectionFactoryQueue.remove(connectionFactoryQueue.firstElement());
+ currentConnector = Connector.create(hostname, port, serviceLookupPrefix, resolver, connectionFactory, timerFactory);
+ currentConnector.setTimeoutMilliseconds(timeoutMilliseconds);
+ onConnectFinishedConnection = currentConnector.onConnectFinished.connect(new Slot2<Connection, com.isode.stroke.base.Error>() {
+ @Override
+ public void call(Connection connection, com.isode.stroke.base.Error error) {
+ handleConnectorFinished(connection, error);
+ }
+ });
+ currentConnector.start();
+ }
+ }
+
+ private void handleConnectorFinished(Connection connection, com.isode.stroke.base.Error error) {
+ logger_.fine("Connector finished\n");
+ onConnectFinishedConnection.disconnect();
+ lastError = error;
+ currentConnector = null;
+ if (connection != null) {
+ finish(connection, error);
+ }
+ else {
+ tryNextConnectionFactory();
+ }
+ }
+} \ No newline at end of file