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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
|
package de.pixart.messenger.services;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import de.pixart.messenger.Config;
import de.pixart.messenger.entities.Account;
import de.pixart.messenger.entities.Room;
import de.pixart.messenger.http.HttpConnectionManager;
import de.pixart.messenger.http.services.MuclumbusService;
import de.pixart.messenger.parser.IqParser;
import de.pixart.messenger.xmpp.OnIqPacketReceived;
import de.pixart.messenger.xmpp.XmppConnection;
import de.pixart.messenger.xmpp.stanzas.IqPacket;
import okhttp3.OkHttpClient;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import rocks.xmpp.addr.Jid;
public class ChannelDiscoveryService {
private final XmppConnectionService service;
private MuclumbusService muclumbusService;
private final Cache<String, List<Room>> cache;
ChannelDiscoveryService(XmppConnectionService service) {
this.service = service;
this.cache = CacheBuilder.newBuilder().expireAfterWrite(5, TimeUnit.MINUTES).build();
}
void initializeMuclumbusService() {
final OkHttpClient.Builder builder = new OkHttpClient.Builder();
if (service.useTorToConnect()) {
try {
builder.proxy(HttpConnectionManager.getProxy());
} catch (IOException e) {
throw new RuntimeException("Unable to use Tor proxy", e);
}
}
Retrofit retrofit = new Retrofit.Builder()
.client(builder.build())
.baseUrl(Config.CHANNEL_DISCOVERY)
.addConverterFactory(GsonConverterFactory.create())
.callbackExecutor(Executors.newSingleThreadExecutor())
.build();
this.muclumbusService = retrofit.create(MuclumbusService.class);
}
void cleanCache() {
cache.invalidateAll();
}
void discover(@NonNull final String query, Method method, OnChannelSearchResultsFound onChannelSearchResultsFound) {
List<Room> result = cache.getIfPresent(key(method, query));
if (result != null) {
onChannelSearchResultsFound.onChannelSearchResultsFound(result);
return;
}
if (method == Method.LOCAL_SERVER) {
discoverChannelsLocalServers(query, onChannelSearchResultsFound);
} else {
if (query.isEmpty()) {
discoverChannelsJabberNetwork(onChannelSearchResultsFound);
} else {
discoverChannelsJabberNetwork(query, onChannelSearchResultsFound);
}
}
}
private void discoverChannelsJabberNetwork(OnChannelSearchResultsFound listener) {
Call<MuclumbusService.Rooms> call = muclumbusService.getRooms(1);
try {
call.enqueue(new Callback<MuclumbusService.Rooms>() {
@Override
public void onResponse(@NonNull Call<MuclumbusService.Rooms> call, @NonNull Response<MuclumbusService.Rooms> response) {
final MuclumbusService.Rooms body = response.body();
if (body == null) {
listener.onChannelSearchResultsFound(Collections.emptyList());
logError(response);
return;
}
cache.put(key(Method.JABBER_NETWORK, ""), body.items);
listener.onChannelSearchResultsFound(body.items);
}
@Override
public void onFailure(@NonNull Call<MuclumbusService.Rooms> call, @NonNull Throwable throwable) {
Log.d(Config.LOGTAG, "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY, throwable);
listener.onChannelSearchResultsFound(Collections.emptyList());
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
private void discoverChannelsJabberNetwork(final String query, OnChannelSearchResultsFound listener) {
MuclumbusService.SearchRequest searchRequest = new MuclumbusService.SearchRequest(query);
Call<MuclumbusService.SearchResult> searchResultCall = muclumbusService.search(searchRequest);
searchResultCall.enqueue(new Callback<MuclumbusService.SearchResult>() {
@Override
public void onResponse(@NonNull Call<MuclumbusService.SearchResult> call, @NonNull Response<MuclumbusService.SearchResult> response) {
final MuclumbusService.SearchResult body = response.body();
if (body == null) {
listener.onChannelSearchResultsFound(Collections.emptyList());
logError(response);
return;
}
cache.put(key(Method.JABBER_NETWORK, query), body.result.items);
listener.onChannelSearchResultsFound(body.result.items);
}
@Override
public void onFailure(@NonNull Call<MuclumbusService.SearchResult> call, @NonNull Throwable throwable) {
Log.d(Config.LOGTAG, "Unable to query muclumbus on " + Config.CHANNEL_DISCOVERY, throwable);
listener.onChannelSearchResultsFound(Collections.emptyList());
}
});
}
private void discoverChannelsLocalServers(final String query, final OnChannelSearchResultsFound listener) {
final Map<Jid, Account> localMucService = getLocalMucServices();
Log.d(Config.LOGTAG, "checking with " + localMucService.size() + " muc services");
if (localMucService.size() == 0) {
listener.onChannelSearchResultsFound(Collections.emptyList());
return;
}
if (!query.isEmpty()) {
final List<Room> cached = cache.getIfPresent(key(Method.LOCAL_SERVER, ""));
if (cached != null) {
final List<Room> results = copyMatching(cached, query);
cache.put(key(Method.LOCAL_SERVER, query), results);
listener.onChannelSearchResultsFound(results);
}
}
final AtomicInteger queriesInFlight = new AtomicInteger();
final List<Room> rooms = new ArrayList<>();
for (Map.Entry<Jid, Account> entry : localMucService.entrySet()) {
IqPacket itemsRequest = service.getIqGenerator().queryDiscoItems(entry.getKey());
queriesInFlight.incrementAndGet();
service.sendIqPacket(entry.getValue(), itemsRequest, (account, itemsResponse) -> {
if (itemsResponse.getType() == IqPacket.TYPE.RESULT) {
final List<Jid> items = IqParser.items(itemsResponse);
for (Jid item : items) {
IqPacket infoRequest = service.getIqGenerator().queryDiscoInfo(item);
queriesInFlight.incrementAndGet();
service.sendIqPacket(account, infoRequest, new OnIqPacketReceived() {
@Override
public void onIqPacketReceived(Account account, IqPacket infoResponse) {
if (infoResponse.getType() == IqPacket.TYPE.RESULT) {
final Room room = IqParser.parseRoom(infoResponse);
if (room != null) {
rooms.add(room);
}
if (queriesInFlight.decrementAndGet() <= 0) {
finishDiscoSearch(rooms, query, listener);
}
} else {
queriesInFlight.decrementAndGet();
}
}
});
}
}
if (queriesInFlight.decrementAndGet() <= 0) {
finishDiscoSearch(rooms, query, listener);
}
});
}
}
private void finishDiscoSearch(List<Room> rooms, String query, OnChannelSearchResultsFound listener) {
Collections.sort(rooms);
cache.put(key(Method.LOCAL_SERVER, ""), rooms);
if (query.isEmpty()) {
listener.onChannelSearchResultsFound(rooms);
} else {
List<Room> results = copyMatching(rooms, query);
cache.put(key(Method.LOCAL_SERVER, query), results);
listener.onChannelSearchResultsFound(rooms);
}
}
private static List<Room> copyMatching(List<Room> haystack, String needle) {
ArrayList<Room> result = new ArrayList<>();
for (Room room : haystack) {
if (room.contains(needle)) {
result.add(room);
}
}
return result;
}
private Map<Jid, Account> getLocalMucServices() {
final HashMap<Jid, Account> localMucServices = new HashMap<>();
for (Account account : service.getAccounts()) {
if (account.isEnabled()) {
final XmppConnection xmppConnection = account.getXmppConnection();
if (xmppConnection == null) {
continue;
}
for (final String mucService : xmppConnection.getMucServers()) {
Jid jid = Jid.of(mucService);
if (!localMucServices.containsKey(jid)) {
localMucServices.put(jid, account);
}
}
}
}
return localMucServices;
}
private static String key(Method method, String query) {
return String.format("%s\00%s", method, query);
}
private static void logError(final Response response) {
final ResponseBody errorBody = response.errorBody();
Log.d(Config.LOGTAG, "code from muclumbus=" + response.code());
if (errorBody == null) {
return;
}
try {
Log.d(Config.LOGTAG, "error body=" + errorBody.string());
} catch (IOException e) {
//ignored
}
}
public interface OnChannelSearchResultsFound {
void onChannelSearchResultsFound(List<Room> results);
}
public enum Method {
JABBER_NETWORK,
LOCAL_SERVER
}
}
|