summaryrefslogtreecommitdiffstats
blob: 37cf1f209a25a467a038cb25eadfe38babc986a9 (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
/*
 * Copyright (c) 2010-2013, Isode Limited, London, England.
 * All rights reserved.
 */
/*
 * Copyright (c) 2010 Remko Tronçon
 * Licensed under the GNU General Public License v3.
 * See Documentation/Licenses/GPLv3.txt for more information.
 */

package com.isode.stroke.network;
import java.util.ArrayList;
import java.util.Collection;

import org.xbill.DNS.Lookup;
import org.xbill.DNS.Record;
import org.xbill.DNS.SRVRecord;
import org.xbill.DNS.TextParseException;
import org.xbill.DNS.Type;

import com.isode.stroke.eventloop.Event.Callback;
import com.isode.stroke.eventloop.EventLoop;
import com.isode.stroke.network.DomainNameServiceQuery;

public class PlatformDomainNameServiceQuery extends DomainNameServiceQuery {
    private final String service;
    private final EventLoop eventLoop;

    public PlatformDomainNameServiceQuery(final String service, final EventLoop eventLoop) {
        this.service = service;
        this.eventLoop = eventLoop;
    }

    private class QueryThread extends Thread {
        @Override
        public void run() {
            final Collection<Result> results = new ArrayList<Result>();
            Lookup request;
            try {
                request = new Lookup(service, Type.SRV);
                final Record[] records = request.run();
                if (records != null) {
                    for (final Record record : records) {
                        /* It's only anticipated that SRVRecords will be
                         * returned, but check first
                         */
                        if (record instanceof SRVRecord) {
                            final SRVRecord srv = (SRVRecord) record;
                            final Result result = new Result(srv.getTarget()
                                .toString(), srv.getPort(), srv.getPriority(),
                                srv.getWeight());
                            results.add(result);
                        }
                    }
                }
            } catch (final TextParseException e) {
                /* Lookup failed because "service" was not a valid DNS name;
                 * leave "results" empty 
                 */
            }

            eventLoop.postEvent(new Callback() {
                @Override
                public void run() {
                    onResult.emit(results);
                }
            });
        }
    }

    @Override
    public void run() {
        final QueryThread thread = new QueryThread();
        thread.setDaemon(true);
        thread.start();
    }
}