13742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman// Copyright 2003-2005 Arthur van Hoff, Rick Blair
23742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman// Licensed under Apache License version 2.0
33742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman// Original license LGPL
43742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
53742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanpackage javax.jmdns.impl.tasks.resolver;
63742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
73742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport java.io.IOException;
83742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
93742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport javax.jmdns.ServiceInfo;
103742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport javax.jmdns.impl.DNSOutgoing;
113742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport javax.jmdns.impl.DNSQuestion;
123742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport javax.jmdns.impl.DNSRecord;
133742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport javax.jmdns.impl.JmDNSImpl;
143742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport javax.jmdns.impl.constants.DNSConstants;
153742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport javax.jmdns.impl.constants.DNSRecordClass;
163742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanimport javax.jmdns.impl.constants.DNSRecordType;
173742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
183742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman/**
193742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * The ServiceResolver queries three times consecutively for services of a given type, and then removes itself from the timer.
203742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * <p/>
213742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman * The ServiceResolver will run only if JmDNS is in state ANNOUNCED. REMIND: Prevent having multiple service resolvers for the same type in the timer queue.
223742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman */
233742d9db8b6edb10627b0f89336cca5249f1d15aManuel Romanpublic class ServiceResolver extends DNSResolverTask {
243742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
253742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    private final String _type;
263742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
273742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    public ServiceResolver(JmDNSImpl jmDNSImpl, String type) {
283742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        super(jmDNSImpl);
293742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        this._type = type;
303742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    }
313742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
323742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    /*
333742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * (non-Javadoc)
343742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * @see javax.jmdns.impl.tasks.DNSTask#getName()
353742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     */
363742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    @Override
373742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    public String getName() {
383742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        return "ServiceResolver(" + (this.getDns() != null ? this.getDns().getName() : "") + ")";
393742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    }
403742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
413742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    /*
423742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * (non-Javadoc)
433742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * @see javax.jmdns.impl.tasks.Resolver#addAnswers(javax.jmdns.impl.DNSOutgoing)
443742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     */
453742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    @Override
463742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    protected DNSOutgoing addAnswers(DNSOutgoing out) throws IOException {
473742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        DNSOutgoing newOut = out;
483742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        long now = System.currentTimeMillis();
493742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        for (ServiceInfo info : this.getDns().getServices().values()) {
503742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            newOut = this.addAnswer(newOut, new DNSRecord.Pointer(info.getType(), DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, info.getQualifiedName()), now);
513742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            // newOut = this.addAnswer(newOut, new DNSRecord.Service(info.getQualifiedName(), DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE, DNSConstants.DNS_TTL, info.getPriority(), info.getWeight(), info.getPort(),
523742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman            // this.getDns().getLocalHost().getName()), now);
533742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        }
543742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        return newOut;
553742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    }
563742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
573742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    /*
583742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * (non-Javadoc)
593742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * @see javax.jmdns.impl.tasks.Resolver#addQuestions(javax.jmdns.impl.DNSOutgoing)
603742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     */
613742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    @Override
623742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    protected DNSOutgoing addQuestions(DNSOutgoing out) throws IOException {
633742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        DNSOutgoing newOut = out;
643742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_type, DNSRecordType.TYPE_PTR, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
653742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        // newOut = this.addQuestion(newOut, DNSQuestion.newQuestion(_type, DNSRecordType.TYPE_SRV, DNSRecordClass.CLASS_IN, DNSRecordClass.NOT_UNIQUE));
663742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        return newOut;
673742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    }
683742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman
693742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    /*
703742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * (non-Javadoc)
713742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     * @see javax.jmdns.impl.tasks.Resolver#description()
723742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman     */
733742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    @Override
743742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    protected String description() {
753742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman        return "querying service";
763742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman    }
773742d9db8b6edb10627b0f89336cca5249f1d15aManuel Roman}