summaryrefslogtreecommitdiffstats
blob: 795fd7ca69b45adae84bdad5add6e1652556287e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (c) 2010 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.component;

import com.isode.stroke.network.Connection;
import com.isode.stroke.network.JavaConnection;
import com.isode.stroke.network.Timer;
import com.isode.stroke.network.HostAddressPort;
import com.isode.stroke.network.DomainNameResolveError;
import com.isode.stroke.network.DomainNameAddressQuery;
import com.isode.stroke.network.DomainNameResolver;
import com.isode.stroke.network.ConnectionFactory;
import com.isode.stroke.network.TimerFactory;
import com.isode.stroke.network.HostAddress;
import com.isode.stroke.signals.Signal1;
import com.isode.stroke.signals.SignalConnection;
import com.isode.stroke.signals.Slot2;
import com.isode.stroke.signals.Slot1;
import com.isode.stroke.signals.Slot;
import java.util.Vector;
import java.util.Collection;

public class ComponentConnector {

	private String hostname = "";
	private int port;
	private DomainNameResolver resolver;
	private ConnectionFactory connectionFactory;
	private TimerFactory timerFactory;
	private int timeoutMilliseconds;
	private Timer timer;
	private DomainNameAddressQuery addressQuery;
	private Vector<HostAddress> addressQueryResults = new Vector<HostAddress>();
	private Connection currentConnection;
	private SignalConnection onConnectFinishedConnection;
	private SignalConnection onTickConnection;
	private SignalConnection onResultConnection;

	public ComponentConnector(final String hostname, int port, DomainNameResolver resolver, ConnectionFactory connectionFactory, TimerFactory timerFactory) {
		this.hostname = hostname;
		this.port = port;
		this.resolver = resolver;
		this.connectionFactory = connectionFactory;
		this.timerFactory = timerFactory;
		this.timeoutMilliseconds = 0;
	}

	public static ComponentConnector create(final String hostname, int port, DomainNameResolver resolver, ConnectionFactory connectionFactory, TimerFactory timerFactory) {
		return new ComponentConnector(hostname, port, resolver, connectionFactory, timerFactory);
	}

	public void setTimeoutMilliseconds(int milliseconds) {
		timeoutMilliseconds = milliseconds;
	}

	public void start() {
		assert(currentConnection == null);
		assert(timer == null);
		assert(addressQuery == null);
		addressQuery = resolver.createAddressQuery(hostname);
		onResultConnection = addressQuery.onResult.connect(new Slot2<Collection<HostAddress>, DomainNameResolveError>() {
			@Override
			public void call(Collection<HostAddress> c1, DomainNameResolveError d1) {
				handleAddressQueryResult(c1, d1);	
			}
		});
		if (timeoutMilliseconds > 0) {
			timer = timerFactory.createTimer(timeoutMilliseconds);
			onTickConnection = timer.onTick.connect(new Slot() {
				@Override
				public void call() {
					handleTimeout();
				}
			});
			timer.start();
		}
		addressQuery.run();
	}

	public void stop() {
		finish((Connection)null);
	}

	public final Signal1<Connection> onConnectFinished = new Signal1<Connection>();

	private void handleAddressQueryResult(final Collection<HostAddress> addresses, DomainNameResolveError error) {
		addressQuery = null;
		if (error != null || addresses.isEmpty()) {
			finish((Connection)null);
		}
		else {
			addressQueryResults.addAll(addresses);
			tryNextAddress();
		}
	}

	private void tryNextAddress() {
		assert(!addressQueryResults.isEmpty());
		HostAddress address = addressQueryResults.remove(0);
		tryConnect(new HostAddressPort(address, port));
	}

	private void tryConnect(final HostAddressPort target) {
		assert(currentConnection == null);
		currentConnection = connectionFactory.createConnection();
		onConnectFinishedConnection = currentConnection.onConnectFinished.connect(new Slot1<Boolean>() {
			@Override
			public void call(Boolean b) {
				handleConnectionConnectFinished(b);
			}
		});
		currentConnection.connect(target);
	}

	private void handleConnectionConnectFinished(boolean error) {
		onConnectFinishedConnection.disconnect();
		if (error) {
			currentConnection = null;
			if (!addressQueryResults.isEmpty()) {
				tryNextAddress();
			}
			else {
				finish((Connection)null);
			}
		}
		else {
			finish(currentConnection);
		}
	}

	private void finish(Connection connection) {
		if (timer != null) {
			timer.stop();
			onTickConnection.disconnect();
			timer = null;
		}
		if (addressQuery != null) {
			onResultConnection.disconnect();
			addressQuery = null;
		}
		if (currentConnection != null) {
			onConnectFinishedConnection.disconnect();
			currentConnection = null;
		}
		onConnectFinished.emit(connection);
	}

	private void handleTimeout() {
		finish((Connection)null);
	}
}