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
|
//Copyright 2003-2005 Arthur van Hoff, Rick Blair
//Licensed under Apache License version 2.0
//Original license LGPL
package javax.jmdns.impl.tasks;
import java.io.IOException;
import java.util.Iterator;
import java.util.Timer;
import java.util.TimerTask;
//import java.util.logging.Level;
//import java.util.logging.Logger;
import javax.jmdns.impl.DNSConstants;
import javax.jmdns.impl.DNSOutgoing;
import javax.jmdns.impl.DNSQuestion;
import javax.jmdns.impl.DNSRecord;
import javax.jmdns.impl.DNSState;
import javax.jmdns.impl.JmDNSImpl;
import javax.jmdns.impl.ServiceInfoImpl;
/**
* The ServiceResolver queries three times consecutively for services of
* a given type, and then removes itself from the timer.
* <p/>
* 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.
*/
public class ServiceResolver extends TimerTask
{
// static Logger logger = Logger.getLogger(ServiceResolver.class.getName());
/**
*
*/
private final JmDNSImpl jmDNSImpl;
/**
* Counts the number of queries being sent.
*/
int count = 0;
private String type;
public ServiceResolver(JmDNSImpl jmDNSImpl, String type)
{
this.jmDNSImpl = jmDNSImpl;
this.type = type;
}
public void start(Timer timer)
{
timer.schedule(this, DNSConstants.QUERY_WAIT_INTERVAL, DNSConstants.QUERY_WAIT_INTERVAL);
}
public void run()
{
try
{
if (this.jmDNSImpl.getState() == DNSState.ANNOUNCED)
{
if (count++ < 3)
{
// logger.finer("run() JmDNS querying service");
long now = System.currentTimeMillis();
DNSOutgoing out = new DNSOutgoing(DNSConstants.FLAGS_QR_QUERY);
out.addQuestion(new DNSQuestion(type, DNSConstants.TYPE_PTR, DNSConstants.CLASS_IN));
for (Iterator s = this.jmDNSImpl.getServices().values().iterator(); s.hasNext();)
{
final ServiceInfoImpl info = (ServiceInfoImpl) s.next();
try
{
out.addAnswer(new DNSRecord.Pointer(info.getType(), DNSConstants.TYPE_PTR, DNSConstants.CLASS_IN, DNSConstants.DNS_TTL, info.getQualifiedName()), now);
}
catch (IOException ee)
{
break;
}
}
this.jmDNSImpl.send(out);
}
else
{
// After three queries, we can quit.
this.cancel();
}
}
else
{
if (this.jmDNSImpl.getState() == DNSState.CANCELED)
{
this.cancel();
}
}
}
catch (Throwable e)
{
// logger.log(Level.WARNING, "run() exception ", e);
this.jmDNSImpl.recover();
}
}
}
|