1// Copyright 2003-2005 Arthur van Hoff, Rick Blair
2// Licensed under Apache License version 2.0
3// Original license LGPL
4
5package javax.jmdns.impl.tasks.resolver;
6
7import java.io.IOException;
8
9import javax.jmdns.impl.DNSOutgoing;
10import javax.jmdns.impl.DNSQuestion;
11import javax.jmdns.impl.DNSRecord;
12import javax.jmdns.impl.JmDNSImpl;
13import javax.jmdns.impl.JmDNSImpl.ServiceTypeEntry;
14import javax.jmdns.impl.constants.DNSConstants;
15import javax.jmdns.impl.constants.DNSRecordClass;
16import javax.jmdns.impl.constants.DNSRecordType;
17
18/**
19 * Helper class to resolve service types.
20 * <p/>
21 * The TypeResolver queries three times consecutively for service types, and then removes itself from the timer.
22 * <p/>
23 * The TypeResolver will run only if JmDNS is in state ANNOUNCED.
24 */
25public class TypeResolver extends DNSResolverTask {
26
27    /**
28     * @param jmDNSImpl
29     */
30    public TypeResolver(JmDNSImpl jmDNSImpl) {
31        super(jmDNSImpl);
32    }
33
34    /*
35     * (non-Javadoc)
36     * @see javax.jmdns.impl.tasks.DNSTask#getName()
37     */
38    @Override
39    public String getName() {
40        return "TypeResolver(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
41    }
42
43    /*
44     * (non-Javadoc)
45     * @see javax.jmdns.impl.tasks.Resolver#addAnswers(javax.jmdns.impl.DNSOutgoing)
46     */
47    @Override
48    protected DNSOutgoing addAnswers(DNSOutgoing out) throws IOException {
49        DNSOutgoing newOut = out;
50        long now = System.currentTimeMillis();
51        for (String type : this.getDns().getServiceTypes().keySet()) {
52            ServiceTypeEntry typeEntry = this.getDns().getServiceTypes().get(type);
53            newOut = this.addAnswer(newOut, new DNSRecord.Pointer("_services._dns-sd._udp.local.", DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, typeEntry.getType()), now);
54        }
55        return newOut;
56    }
57
58    /*
59     * (non-Javadoc)
60     * @see javax.jmdns.impl.tasks.Resolver#addQuestions(javax.jmdns.impl.DNSOutgoing)
61     */
62    @Override
63    protected DNSOutgoing addQuestions(DNSOutgoing out) throws IOException {
64        return this.addQuestion(out, DNSQuestion.newQuestion("_services._dns-sd._udp.local.", DNSRecordType.TYPE_PTR, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
65    }
66
67    /*
68     * (non-Javadoc)
69     * @see javax.jmdns.impl.tasks.Resolver#description()
70     */
71    @Override
72    protected String description() {
73        return "querying type";
74    }
75}