summaryrefslogtreecommitdiffstats
blob: 39900aae35eb8dfc6ad73b6d7db7d85aed12d6b6 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright (c) 2010-2016 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.disco;

import com.isode.stroke.jid.JID;
import com.isode.stroke.elements.DiscoInfo;
import com.isode.stroke.elements.DiscoItems;
import com.isode.stroke.elements.ErrorPayload;
import com.isode.stroke.disco.GetDiscoInfoRequest;
import com.isode.stroke.disco.GetDiscoItemsRequest;
import com.isode.stroke.queries.IQRouter;
import com.isode.stroke.signals.Signal;
import com.isode.stroke.signals.Slot2;
import com.isode.stroke.signals.Signal2;
import com.isode.stroke.signals.SignalConnection;
import com.isode.stroke.base.NotNull;

import java.util.logging.Logger;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.isode.stroke.base.NotNull;

/**
 * Recursively walk service discovery trees to find all services offered.
 * This stops on any disco item that's not reporting itself as a server.
 */
public class DiscoServiceWalker {

	private JID service_;
	private IQRouter iqRouter_;
	private long maxSteps_;
	private boolean active_;
	private Set<JID> servicesBeingSearched_ = new HashSet<JID>();
	private Set<JID> searchedServices_ = new HashSet<JID>();
	private Set<GetDiscoInfoRequest> pendingDiscoInfoRequests_ = new HashSet<GetDiscoInfoRequest>();
	private Set<GetDiscoItemsRequest> pendingDiscoItemsRequests_ = new HashSet<GetDiscoItemsRequest>();
	private Logger logger_ = Logger.getLogger(this.getClass().getName());
	private SignalConnection onServiceFoundConnection;
	private SignalConnection onWalkAbortedConnection;
	private SignalConnection onWalkCompleteConnection;
	
	/**
	 * The discoInfo.onResponse connections for each GetDiscoInfoRequest
	 */
	private Map<GetDiscoInfoRequest,SignalConnection> onResponseDiscoInfoConnections =
	        new HashMap<GetDiscoInfoRequest,SignalConnection>();
	
	/**
	 * The discoItems.onResponse connections for each GetDiscoItemsRequest
	 */
	private Map<GetDiscoItemsRequest,SignalConnection> onResponseDiscoItemsConnections =
	        new HashMap<GetDiscoItemsRequest,SignalConnection>();

	/** Emitted for each service found. */
	public final Signal2<JID, DiscoInfo> onServiceFound = new Signal2<JID, DiscoInfo>();

	/** Emitted when walking is aborted. */
	public final Signal onWalkAborted = new Signal();

	/** Emitted when walking is complete.*/
	public final Signal onWalkComplete = new Signal();

	/**
	* Parameterized Constructor.
	* @param service, Not Null.
	* @param iqRouter, Not Null.
	*/
	public DiscoServiceWalker(JID service, IQRouter iqRouter) {
		this(service, iqRouter, 200);
	}

	/**
	* Parameterized Constructor.
	* @param service, Not Null.
	* @param iqRouter, Not Null.
	* @param maxSteps.
	*/
	public DiscoServiceWalker(JID service, IQRouter iqRouter, long maxSteps) {
		NotNull.exceptIfNull(service, "service");
		NotNull.exceptIfNull(iqRouter, "iqRouter");
		this.service_ = service;
		this.iqRouter_ = iqRouter;
		this.maxSteps_ = maxSteps;
		this.active_ = false;
	}

	/**
	 * Start the walk.
	 *
	 * Call this exactly once.
	 */
	public void beginWalk() {
		logger_.fine("Starting walk to " + service_ + "\n");
		assert(!active_);
		assert(servicesBeingSearched_.isEmpty());
		active_ = true;
		walkNode(service_);
	}

	/**
	 * End the walk.
	 */
	public void endWalk() {
	    if (active_) {
	        logger_.fine("Ending walk to" + service_ + "\n");
	        for (SignalConnection discoInfoConnection : onResponseDiscoInfoConnections.values()) {
	            discoInfoConnection.disconnect();
	        }
	        onResponseDiscoInfoConnections.clear();
	        for (SignalConnection discoItemsConnection : onResponseDiscoItemsConnections.values()) {
	            discoItemsConnection.disconnect();
	        }
	        onResponseDiscoItemsConnections.clear();
	        active_ = false;
	        onWalkAborted.emit();
	    }		
	}

	public boolean isActive() {
		return active_;
	}

	private void walkNode(JID jid) {
		logger_.fine("Walking node" + jid + "\n");
		servicesBeingSearched_.add(jid);
		searchedServices_.add(jid);
		final GetDiscoInfoRequest discoInfoRequest = GetDiscoInfoRequest.create(jid, iqRouter_);
		SignalConnection connection = discoInfoRequest.onResponse.connect(new Slot2<DiscoInfo, ErrorPayload>() {

			@Override
			public void call(DiscoInfo info, ErrorPayload error) {
				handleDiscoInfoResponse(info, error, discoInfoRequest);
			}
		});
		onResponseDiscoInfoConnections.put(discoInfoRequest, connection);
		pendingDiscoInfoRequests_.add(discoInfoRequest);
		discoInfoRequest.send();
	}

	private void markNodeCompleted(JID jid) {
		logger_.fine("Node completed " + jid + "\n");
		servicesBeingSearched_.remove(jid);
		/* All results are in */
		if (servicesBeingSearched_.isEmpty()) {
			active_ = false;
			onWalkComplete.emit();
		}
		/* Check if we're on a rampage */
		else if (searchedServices_.size() >= maxSteps_) {
			active_ = false;
			onWalkComplete.emit();
		}
	}

	private void handleDiscoInfoResponse(DiscoInfo info, ErrorPayload error, GetDiscoInfoRequest request) {
		/* If we got canceled, don't do anything */
		if (!active_) {
			return;
		}

		logger_.fine("Disco info response from " + request.getReceiver() + "\n");

		
		SignalConnection connection = onResponseDiscoInfoConnections.remove(request);
		connection.disconnect();
		
		pendingDiscoInfoRequests_.remove(request);
		if (error != null) {
			handleDiscoError(request.getReceiver(), error);
			return;
		}

		boolean couldContainServices = false;
		for (DiscoInfo.Identity identity : info.getIdentities()) {
			if (identity.getCategory().equals("server")) {
				couldContainServices = true;
			}
		}
		boolean completed = false;
		if (couldContainServices) {
			final GetDiscoItemsRequest discoItemsRequest = GetDiscoItemsRequest.create(request.getReceiver(), iqRouter_);
			SignalConnection discoItemsConnection = discoItemsRequest.onResponse.connect(new Slot2<DiscoItems, ErrorPayload>() {

				@Override
				public void call(DiscoItems item, ErrorPayload error) {
					handleDiscoItemsResponse(item, error, discoItemsRequest);
				}
			});
			onResponseDiscoItemsConnections.put(discoItemsRequest, discoItemsConnection);
			pendingDiscoItemsRequests_.add(discoItemsRequest);
			discoItemsRequest.send();
		} else {
			completed = true;
		}
		onServiceFound.emit(request.getReceiver(), info);
		if (completed) {
			markNodeCompleted(request.getReceiver());
		}		
	}

	private void handleDiscoItemsResponse(DiscoItems items, ErrorPayload error, GetDiscoItemsRequest request) {
	    /* If we got canceled, don't do anything */
	    if (!active_) {
	        return;
	    }

	    logger_.fine("Received disco items from " + request.getReceiver() + "\n");

	    SignalConnection connection = onResponseDiscoItemsConnections.remove(request);
	    connection.disconnect();

	    pendingDiscoItemsRequests_.remove(request);
	    if (error != null) {
	        handleDiscoError(request.getReceiver(), error);
	        return;
	    }
	    for (DiscoItems.Item item : items.getItems()) {
	        if (item.getNode().isEmpty()) {
	            /* Don't look at noded items. It's possible that this will exclude some services,
	             * but I've never seen one in the wild, and it's an easy fix for not looping.
	             */
	            if(!searchedServices_.contains(item.getJID())) {
	                logger_.fine("Received disco item " + item.getJID() + "\n");
	                walkNode(item.getJID());
	            }
	        }
	    }
	    markNodeCompleted(request.getReceiver());
	}

	private void handleDiscoError(JID jid, ErrorPayload error) {
		logger_.fine("Disco error from " + jid + "\n");
		markNodeCompleted(jid);
	}
}