aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/jabber/JingleNodesServiceDiscovery.java
blob: 4e527d23e2fb296e73cab8c33807821ff6c0e1f3 (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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.protocol.jabber;

import java.util.*;
import java.util.concurrent.*;

import net.java.sip.communicator.util.Logger;

import org.jitsi.util.*;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.filter.*;
import org.jivesoftware.smack.packet.*;
import org.jivesoftware.smackx.packet.*;
import org.xmpp.jnodes.smack.*;

/**
 * Search for jingle nodes.
 *
 * @author Damian Minkov
 */
public class JingleNodesServiceDiscovery
    implements Runnable
{
    /**
     * Logger of this class
     */
    private static final Logger logger =
        Logger.getLogger(JingleNodesServiceDiscovery.class);

    /**
     * Property containing jingle nodes prefix to search for.
     */
    private static final String JINGLE_NODES_SEARCH_PREFIX_PROP =
        "net.java.sip.communicator.impl.protocol.jabber.JINGLE_NODES_SEARCH_PREFIXES";

    /**
     * Property containing jingle nodes prefix to search for.
     */
    private static final String JINGLE_NODES_SEARCH_PREFIXES_STOP_ON_FIRST_PROP =
        "net.java.sip.communicator.impl.protocol.jabber.JINGLE_NODES_SEARCH_PREFIXES_STOP_ON_FIRST";

    /**
     * Synchronization object to monitor auto discovery.
     */
    private final Object jingleNodesSyncRoot;

    /**
     * The service.
     */
    private final SmackServiceNode service;

    /**
     * The connection, must be connected.
     */
    private final XMPPConnection connection;

    /**
     * Our account.
     */
    private final JabberAccountIDImpl accountID;

    /**
     * Creates discovery
     * @param service the service.
     * @param connection the connected connection.
     * @param accountID our account.
     * @param syncRoot the synchronization object while discovering.
     */
    JingleNodesServiceDiscovery(SmackServiceNode service,
                                XMPPConnection connection,
                                JabberAccountIDImpl accountID,
                                Object syncRoot)
    {
        this.jingleNodesSyncRoot = syncRoot;
        this.service = service;
        this.connection = connection;
        this.accountID = accountID;
    }

    /**
     * The actual discovery.
     */
    public void run()
    {
        synchronized(jingleNodesSyncRoot)
        {
            long start = System.currentTimeMillis();
            if(logger.isInfoEnabled())
            {
                logger.info("Start Jingle Nodes discovery!");
            }

            SmackServiceNode.MappedNodes nodes;

            String searchNodesWithPrefix =
                JabberActivator.getResources()
                    .getSettingsString(JINGLE_NODES_SEARCH_PREFIX_PROP);
            if(searchNodesWithPrefix == null
                || searchNodesWithPrefix.length() == 0)
                searchNodesWithPrefix =
                    JabberActivator.getConfigurationService()
                        .getString(JINGLE_NODES_SEARCH_PREFIX_PROP);

            // if there are no default prefix settings or
            // this option is turned off, just process with default
            // service discovery making list empty.
            if( searchNodesWithPrefix == null
                || searchNodesWithPrefix.length() == 0
                || searchNodesWithPrefix.equalsIgnoreCase("off"))
            {
                searchNodesWithPrefix = "";
            }

            nodes = searchServicesWithPrefix(
                service,
                connection, 6, 3, 20, JingleChannelIQ.UDP,
                accountID.isJingleNodesSearchBuddiesEnabled(),
                accountID.isJingleNodesAutoDiscoveryEnabled(),
                searchNodesWithPrefix);

            if(logger.isInfoEnabled())
            {
                logger.info("Jingle Nodes discovery terminated! ");
                logger.info("Found " + (nodes != null ?
                                        nodes.getRelayEntries().size() : "0") +
                        " Jingle Nodes relay for account: " +
                        accountID.getAccountAddress()
                    + " in " + (System.currentTimeMillis() - start) + " ms.");
            }

            if(nodes != null)
                service.addEntries(nodes);
        }
    }

    /**
     * Searches for services as the prefix list has priority. If it is set
     * return after first found service.
     *
     * @param service the service.
     * @param xmppConnection the connection.
     * @param maxEntries maximum entries to be searched.
     * @param maxDepth the depth while recursively searching.
     * @param maxSearchNodes number of nodes to query
     * @param protocol the protocol
     * @param searchBuddies should we search our buddies in contactlist.
     * @param autoDiscover is auto discover turned on
     * @param prefix the coma separated list of prefixes to be searched first.
     * @return
     */
    private SmackServiceNode.MappedNodes searchServicesWithPrefix(
            SmackServiceNode service,
            XMPPConnection xmppConnection,
            int maxEntries,
            int maxDepth,
            int maxSearchNodes,
            String protocol,
            boolean searchBuddies,
            boolean autoDiscover,
            String prefix)
        {
            if (xmppConnection == null || !xmppConnection.isConnected())
            {
                return null;
            }

            SmackServiceNode.MappedNodes mappedNodes =
                new SmackServiceNode.MappedNodes();
            ConcurrentHashMap<String, String> visited
                = new ConcurrentHashMap<String, String>();

            // Request to our pre-configured trackerEntries
            for(Map.Entry<String, TrackerEntry> entry
                    : service.getTrackerEntries().entrySet())
            {
                SmackServiceNode.deepSearch(
                    xmppConnection,
                    maxEntries,
                    entry.getValue().getJid(),
                    mappedNodes,
                    maxDepth - 1,
                    maxSearchNodes,
                    protocol,
                    visited);
            }

            if(autoDiscover)
            {
                boolean continueSearch =
                    searchDiscoItems(
                        service,
                        xmppConnection,
                        maxEntries,
                        xmppConnection.getServiceName(),
                        mappedNodes,
                        maxDepth - 1,
                        maxSearchNodes,
                        protocol,
                        visited,
                        prefix);

                // option to stop after first found is turned on, lets exit
                if(!continueSearch)
                    return mappedNodes;

                // Request to Server
                SmackServiceNode.deepSearch(
                    xmppConnection,
                    maxEntries,
                    xmppConnection.getHost(),
                    mappedNodes,
                    maxDepth - 1,
                    maxSearchNodes,
                    protocol,
                    visited);

                // Request to Buddies
                if (xmppConnection.getRoster() != null && searchBuddies)
                {
                    for (final RosterEntry re : xmppConnection.getRoster().getEntries())
                    {
                        for (final Iterator<Presence> i
                                 = xmppConnection.getRoster()
                                    .getPresences(re.getUser());
                             i.hasNext();)
                        {
                            final Presence presence = i.next();
                            if (presence.isAvailable())
                            {
                                SmackServiceNode.deepSearch(
                                    xmppConnection,
                                    maxEntries,
                                    presence.getFrom(),
                                    mappedNodes,
                                    maxDepth - 1,
                                    maxSearchNodes,
                                    protocol,
                                    visited);
                            }
                        }
                    }
                }
            }

            return null;
        }

        /**
         * Discover services and query them.
         * @param service the service.
         * @param xmppConnection the connection.
         * @param maxEntries maximum entries to be searched.
         * @param startPoint the start point to search recursively
         * @param mappedNodes nodes found
         * @param maxDepth the depth while recursively searching.
         * @param maxSearchNodes number of nodes to query
         * @param protocol the protocol
         * @param visited nodes already visited
         * @param prefix the coma separated list of prefixes to be searched first.
         * @return
         */
        private static boolean searchDiscoItems(
            SmackServiceNode service,
            XMPPConnection xmppConnection,
            int maxEntries,
            String startPoint,
            SmackServiceNode.MappedNodes mappedNodes,
            int maxDepth,
            int maxSearchNodes,
            String protocol,
            ConcurrentHashMap<String, String> visited,
            String prefix)
        {
            String[] prefixes = prefix.split(",");

            // default is to stop when first one is found
            boolean stopOnFirst = true;

            String stopOnFirstDefaultValue =
                JabberActivator.getResources().getSettingsString(
                    JINGLE_NODES_SEARCH_PREFIXES_STOP_ON_FIRST_PROP);
            if(stopOnFirstDefaultValue != null)
            {
                stopOnFirst = Boolean.parseBoolean(stopOnFirstDefaultValue);
            }
            stopOnFirst = JabberActivator.getConfigurationService().getBoolean(
                JINGLE_NODES_SEARCH_PREFIXES_STOP_ON_FIRST_PROP,
                stopOnFirst);

            final DiscoverItems items = new DiscoverItems();
            items.setTo(startPoint);
            PacketCollector collector =
                xmppConnection.createPacketCollector(
                    new PacketIDFilter(items.getPacketID()));
            xmppConnection.sendPacket(items);
            DiscoverItems result = (DiscoverItems) collector.nextResult(
                Math.round(SmackConfiguration.getPacketReplyTimeout() * 1.5));

            if (result != null)
            {
                // first search priority items
                Iterator<DiscoverItems.Item> i = result.getItems();
                for (DiscoverItems.Item item = i.hasNext() ? i.next() : null;
                     item != null;
                     item = i.hasNext() ? i.next() : null)
                {
                    for(String pref : prefixes)
                    {
                        if( !StringUtils.isNullOrEmpty(pref)
                            && item.getEntityID().startsWith(pref.trim()))
                        {
                            SmackServiceNode.deepSearch(
                                xmppConnection,
                                maxEntries,
                                item.getEntityID(),
                                mappedNodes,
                                maxDepth,
                                maxSearchNodes,
                                protocol,
                                visited);

                            if(stopOnFirst)
                                return false;// stop and don't continue
                        }
                    }
                }

                // now search rest
                i = result.getItems();
                for (DiscoverItems.Item item = i.hasNext() ? i.next() : null;
                     item != null;
                     item = i.hasNext() ? i.next() : null)
                {
                    // we may searched already this node if it starts
                    // with some of the prefixes
                    if(!visited.containsKey(item.getEntityID()))
                        SmackServiceNode.deepSearch(
                            xmppConnection,
                            maxEntries,
                            item.getEntityID(),
                            mappedNodes,
                            maxDepth,
                            maxSearchNodes,
                            protocol,
                            visited);

                    if(stopOnFirst)
                        return false;// stop and don't continue
                }
            }
            collector.cancel();

            // true we should continue searching
            return true;
        }
}